Python 参数维度不兼容,在两个维度之间填充_时出现问题
我在一张图上画了两条线,我想给中间的区域涂上阴影,但是我无法做到,我的数据是两个数据帧,包含365个观察值。 我当前的代码如下所示Python 参数维度不兼容,在两个维度之间填充_时出现问题,python,matplotlib,Python,Matplotlib,我在一张图上画了两条线,我想给中间的区域涂上阴影,但是我无法做到,我的数据是两个数据帧,包含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']) 我有这个: 有关数据帧的
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'])
我有这个:
有关数据帧的更多信息
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)
错误在这一行代码ax.fill\u between(x,minimos,maximos,facecolor='purple')。使用
fill\u between
:
这里没有理由使用
x=x.reformate(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()
这似乎没问题,但我无法使其正常工作。我得到了一个值错误:参数维度不兼容,知道我为什么得到这个值吗?可能是两个数据帧不共享同一个索引。最好的解决方案将取决于数据,因此共享关于数据帧的更多信息将有所帮助。在任何情况下,共享产生错误的代码对您都是必要的。我已经添加了有关数据帧的更多信息,我可以添加更多吗?如果没有重塑,我得到了这个,(365,1)(365,1)(365,)并且我仍然无法使它工作。您现在仍然需要重塑两个数组minimos和maximos,使其只有一个维度。
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()
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()