参数维度不兼容,请在

2024-09-30 06:26:53 发布

您现在位置:Python中文网/ 问答频道 /正文

我在一张图上画了两条线,我想在中间的区域加上阴影,但是我做不到,我的数据是2个数据帧,有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

关于数据帧的更多信息

^{pr2}$

但我仍然有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.填充(x,minimos,maximos,facecolor='purple')。在


Tags: 数据代码importalphaplotaspltax
2条回答

这里没有理由使用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

使用fill_between:http://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

相关问题 更多 >

    热门问题