python - Matplotlib xtick 给出 "The truth value of an array with more than one element is ambiguous"

标签 python python-3.x matplotlib

我正在尝试使用 matplotlib 绘制一些数据。问题是当我使用 plt.xtick() 函数时,它给了我以下信息。

ValueError: The truth value of an array with more than one element is ambiguous. Use a.any() or a.all()

下面是第 60 行的代码错误“plt.xticks(np.array([r + barWidth for r in range(len(ener))]), xlab, rotation=90)”

import numpy as np
from mpl_toolkits.mplot3d import axes3d
import matplotlib.pyplot as plt
import matplotlib.tri as tri
from matplotlib.ticker import MaxNLocator
import math
import os, pdb

paths=["1leo_5fpps_pwat", "1leo_5fpps_u8", "1leo_5fpps_u8_t4"]
aminoacids = {1:"Ser1", 2:"trp2", 3:"thr3", 4:"trp4", 5:"glu5", 6:"gly6", 7:"asn7", 8:"lys8", 9:"trp9", 10:"thr10", 11:"trp11", 12:"lys12"}
np.set_printoptions(suppress=True)
replica=0
f, ax = plt.subplots(3, figsize=(15,10), sharex=True)
for i in range(3):
    xf=np.random.rand(6000, 199)
    Z = np.mean(xf, axis=0)

    # set width of bar
    barWidth = 0.25

    # set height of bar
    ener = Z[1::3]
    vdw = Z[2::3]
    elec = Z[3::3]

    filtered_index=np.where(ener<25)
    ener = ener[filtered_index]
    vdw = vdw[filtered_index]
    elec = elec[filtered_index]


    ax[i].set_title(paths[i])
    # print(Z.shape, ener.shape, vdw.shape, elec.shape)
    # print("Z\n", Z)
    print("vdw\n", vdw)
    print("electrostatic\n", elec)
    print("Total\n", ener)
    print("sum\n",vdw+elec)


    # Set position of bar on X axis
    r1 = np.arange(len(ener))
    r2 = [x + barWidth for x in r1]
    r3 = [x + barWidth for x in r2]

    # Make the plot
    ax[i].bar(r1, ener, color='red', width=barWidth, edgecolor='white', label='ener')
    ax[i].bar(r2, vdw, color='green', width=barWidth, edgecolor='white', label='vdw')
    ax[i].bar(r3, elec, color='blue', width=barWidth, edgecolor='white', label='elec')
    ax[i].legend()
    xlab = np.array([(i,j) for i in range(1,13) for j in range(i+1,13)])
    xlab = xlab[filtered_index]
    xlab = np.array([[aminoacids[i], aminoacids[j]] for i,j in xlab])

pdb.set_trace()
f.suptitle('Interaction energy of Trp-zipper replica {}'.format(replica), fontsize=16)
f.text(0.5, 0.02, 'Native contacts', ha='center', fontsize=16)
f.text(0.08, 0.5, "Energy (kcal/mol)", va='center', rotation='vertical', fontsize=16)
f.subplots_adjust(hspace=0.25)
plt.xticks(np.array([r + barWidth for r in range(len(ener))]), xlab, rotation=90)
plt.setp([a.get_xticklabels() for a in f.axes[:-2]], visible=False)
plt.ion()
plt.show()
# plt.savefig("{}/plots/inter_ener_trpzip_{}.png".format(os.environ["root"], replica), bbox_inches='tight')
# plt.pause(5.0)
# plt.show()

几个月前这是有效的,但现在它给了我错误。我不知道为什么 xtick 会报上面的错误。

我已阅读有关此错误的信息 here当有一些基于数组的决策语句时会发生此错误,那么为什么 xtick 会产生此错误。

最佳答案

使用

plt.xticks(np.array([r + barWidth for r in range(len(ener))]),[ ''.join(x) for x in xlab] , rotation=90)

这里的问题是

plt.xticks(A,B) 

期望 B 是一个字符串列表。

但是您将 B 作为 66 x 2 的 numpy 数组提供。 所以我们将 2 个元素与

''.join()

使用列表理解功能并迭代 66。

关于python - Matplotlib xtick 给出 "The truth value of an array with more than one element is ambiguous",我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56987646/

相关文章:

python - 如何调整字符串x轴的 'tick frequency'

python-2.7 - 在Python中以(x,y)坐标绘制箭头

python - 安装 pyserial Mac OS 10.10?

python - 寻找整个 Pandas 数据框的中位数

python - 我怎样才能从字典中调用 __missing__

python-3.x - 除了在 Jupyter Notebook 中,如何查看 Bokeh/Holoview 图?

matplotlib imshow 编辑 x 轴

python - 在 Python 中找到最匹配的 block /补丁

python - 是否有一个函数可以检查字符串中的字符是否是字母表中的字母? ( swift )

python - 如何删除 python-igraph 中的边缘属性