值错误:形状与比较条形图不匹配

时间:2020-03-15 18:58:11

标签: python python-2.7 matplotlib valueerror

我正在尝试制作包含6条柱形图的条形图,其中包括根据我在这些说明https://pythonspot.com/matplotlib-bar-chart/中找到的代码进行的2组3条比较的结果。

import numpy as np
import matplotlib.pyplot as plt

# Angles data to plot DPPC, DOPC
n_groups = 3
layer = (68.397, 67.946)
With_c6100 = (70.556, 68.939)
With_c6pc = (71.361, 70.901)

# create plot
fig, ax = plt.subplots()
index = np.arange(n_groups)
bar_width = 0.35
opacity = 0.8

rects1 = plt.bar(index, layer, bar_width,
alpha=opacity,
color='b',
label='Bilayer')

rects2 = plt.bar(index + bar_width, With_c6100, bar_width,
alpha=opacity,
color='g',
label='with C6100 micelle')

rects3 = plt.bar(index + bar_width, With_c6pc, bar_width,
alpha=opacity,
color='r',
label='with C6PC micelle')

plt.xlabel('Lipid System')
plt.ylabel('P-N-Z Angle Mean')
plt.title('P-N-Z angle comparison')
plt.xticks(index + bar_width, ('DPPC', 'DOPC'))
plt.legend()

plt.tight_layout()
plt.show()

但是我得到这个错误

Traceback (most recent call last):
  File "/Users/RayleighTaylor/Physics project/Plots/pnz comparison.py", line 22, in <module>
    label='Bilayer')
  File "/Users/RayleighTaylor/miniconda2/lib/python2.7/site-packages/matplotlib/pyplot.py", line 2775, in bar
    ret = ax.bar(*args, **kwargs)
  File "/Users/RayleighTaylor/miniconda2/lib/python2.7/site-packages/matplotlib/__init__.py", line 1867, in inner
    return func(ax, *args, **kwargs)
  File "/Users/RayleighTaylor/miniconda2/lib/python2.7/site-packages/matplotlib/axes/_axes.py", line 2238, in bar
    np.atleast_1d(x), height, width, y, linewidth)
  File "/Users/RayleighTaylor/miniconda2/lib/python2.7/site-packages/numpy/lib/stride_tricks.py", line 259, in broadcast_arrays
    shape = _broadcast_shape(*args)
  File "/Users/RayleighTaylor/miniconda2/lib/python2.7/site-packages/numpy/lib/stride_tricks.py", line 193, in _broadcast_shape
    b = np.broadcast(*args[:32])
ValueError: shape mismatch: objects cannot be broadcast to a single shape

该网站上的代码运行正常,我所做的只是将n_groups更改为3,并相应地添加了数据和标签,我不知道可能是什么问题。

0 个答案:

没有答案