参数维度不兼容,填充问题

时间:2017-03-19 21:07:02

标签: python matplotlib

我正在图中绘制2条线,我想在它们之间划分区域,但是我无法实现,我的数据是2个DataFrames,有365个观察值。 我目前的代码看起来像这样。

plt.figure()
plt.plot(minimos, '', maximos, '')
plt.scatter(df6x, df6, s=50, c='r', alpha=0.8)
plt.scatter(df5x, df5, s=50, c='b', alpha=0.8)
plt.legend(['High', 'Low'])

我有这个: enter image description here

有关DataFrames的更多信息

  print(type(maximos),type(minimos), type(x))
  print(maximos.shape, minimos.shape,x.shape)

<class 'numpy.ndarray'> <class 'numpy.ndarray'> <class 'numpy.ndarray'>
 (365, 1) (365, 1) (365, 1)  

但我仍然遇到ValueError问题:参数维度不兼容

import matplotlib as mpl
import matplotlib.pyplot as plt
import numpy as np

 maximos = maximos.values
 minimos = minimos.values
 x = np.arange(0,365,1)
 x = x.reshape(365,1)
 fig = plt.figure()
 plt.plot(x, maximos, c='r', alpha=0.8)
 plt.plot(x, minimos, c='b', alpha=0.8)
 # fill between hgh and low
 ax = fig.gca()
 ax.fill_between(x, minimos, maximos, facecolor='purple')
 plt.legend(['High', 'Low'])
 plt.scatter(df6x, df6, s=50, c='r', alpha=0.8)
 plt.scatter(df5x, df5, s=50, c='b', alpha=0.8)

错误出现在这行代码中ax.fill_between(x,minimos,maximos,facecolor ='purple')。

2 个答案:

答案 0 :(得分:1)

使用fill_betweenhttp://matplotlib.org/examples/pylab_examples/fill_between_demo.html

import matplotlib.pylab as plt
import numpy as np

x = np.arange(1,365,1)
hgh = np.random.uniform(20,30,len(x))
low = np.random.uniform(-10,10, len(x))

fig = plt.figure()
plt.plot(x, hgh, c='r', alpha=0.8)
plt.plot(x, low, c='b', alpha=0.8)

# fill between hgh and low
ax = fig.gca()
ax.fill_between(x, low, hgh, facecolor='gold')

plt.legend(['High', 'Low'])
plt.show()

enter image description here

答案 1 :(得分:1)

此处没有理由使用x = x.reshape(365,1)。正如错误所述,重塑x会使参数维度不兼容。省略该行将使代码工作:

import numpy as np; np.random.seed(42)
import matplotlib.pyplot as plt

maximos = np.cumsum(np.random.rand(365)-0.5)+40
minimos = np.cumsum(np.random.rand(365)-0.5)+0.7

x = np.arange(0,365,1)

fig = plt.figure()
plt.plot(x, maximos, c='r', alpha=0.8)
plt.plot(x, minimos, c='b', alpha=0.8)

plt.fill_between(x, minimos, maximos, facecolor='purple')
plt.legend(['High', 'Low'])

plt.show()

enter image description here