Python Matplotlib xtick给出了;包含多个元素的数组的真值不明确;
我正在尝试使用matplotlib绘制一些数据。问题是当我使用plt.xtick()函数时,它会给我以下信息Python Matplotlib xtick给出了;包含多个元素的数组的真值不明确;,python,python-3.x,matplotlib,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+范围内r的条形宽度(len(ener))])),xlab,旋转=90)” 将numpy导入为np 从mpl_
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+范围内r的条形宽度(len(ener))])),xlab,旋转=90)”
将numpy导入为np
从mpl_toolkits.mplot3d导入axes3d
将matplotlib.pyplot作为plt导入
将matplotlib.tri导入为tri
从matplotlib.ticker导入MaxNLocator
输入数学
导入操作系统,pdb
路径=[“1leo_5fpps_pwat”、“1leo_5fpps_u8”、“1leo_5fpps_u8_t4”]
氨基酸={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\u打印选项(suppress=True)
副本=0
f、 ax=plt.子批次(3,figsize=(15,10),sharex=True)
对于范围(3)中的i:
xf=np.rand.rand(6000199)
Z=np.平均值(xf,轴=0)
#设置条的宽度
条形宽度=0.25
#设置杆的高度
ener=Z[1::3]
vdw=Z[2::3]
elec=Z[3::3]
过滤指数=np。其中(ener使用
plt.xticks(np.数组([r+范围内r的线宽(len(ener))]),[''.join(x)表示xlab中的x],旋转=90)
这里的问题是
plt.xticks(A,B)
期望B是字符串列表
但是你把B作为一个66 x 2的numpy数组来填充。
因此,我们将这两个元素与
“”.join()
函数,并通过列表理解在66上迭代。如果您可以指向导致此错误的行,这将非常有用。编辑文章以包含给出错误的行号。因此第60行给出错误。似乎xlab是一个具有形状(66,2)的数组。这似乎可行。您可以提供一些解释吗。
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()