用python的matplotlib绘图,如何使用fill_between函数,使折线图在直线40以上的填充为绿色?
说明:x,y是整形数组plt.plot(x,y)axhline(40,linestyle='--',linewidth=2,color='red')plt.fill_be...
说明:x,y是整形数组
plt.plot(x, y)
axhline(40,linestyle='--',linewidth=2, color='red')
plt.fill_between(x, y, 40, where=(y>=40), facecolor='green')
程序出错,错误是:
Traceback (most recent call last):
File "2.py", line 59, in <module>
plt.fill_between(x, y, 40, where=(y<=40), facecolor='green', edgecolor='green')
File "F:\Python27\lib\site-packages\matplotlib\pyplot.py", line 2278, in fill_between
ret = ax.fill_between(x, y1, y2, where, interpolate, **kwargs)
File "F:\Python27\lib\site-packages\matplotlib\axes.py", line 6436, in fill_between
raise ValueError("Argument dimensions are incompatible")
ValueError: Argument dimensions are incompatible
>Exit code: 1ValueError: Argument dimensions are incompatible
# -*- coding: utf-8 -*-
import matplotlib.pyplot as plt
from matplotlib.pyplot import plot,savefig,figure, show
from pylab import *
import matplotlib.mlab as mlab
import matplotlib.colors as colors
x=[1,2,3,4,5]
y=[15,23,40,38,65]
plt.plot(x,y)
axhline(40,linestyle='--',linewidth=2, color='red')
plt.fill_between(x, y, 40, where=(y>=40), facecolor='green', edgecolor='green')
show()
说明:如果我把plt.fill_between()这个函数注释掉,程序就没有错误,就可以运行;但是如果加上,就会出现上述错误 展开
plt.plot(x, y)
axhline(40,linestyle='--',linewidth=2, color='red')
plt.fill_between(x, y, 40, where=(y>=40), facecolor='green')
程序出错,错误是:
Traceback (most recent call last):
File "2.py", line 59, in <module>
plt.fill_between(x, y, 40, where=(y<=40), facecolor='green', edgecolor='green')
File "F:\Python27\lib\site-packages\matplotlib\pyplot.py", line 2278, in fill_between
ret = ax.fill_between(x, y1, y2, where, interpolate, **kwargs)
File "F:\Python27\lib\site-packages\matplotlib\axes.py", line 6436, in fill_between
raise ValueError("Argument dimensions are incompatible")
ValueError: Argument dimensions are incompatible
>Exit code: 1ValueError: Argument dimensions are incompatible
# -*- coding: utf-8 -*-
import matplotlib.pyplot as plt
from matplotlib.pyplot import plot,savefig,figure, show
from pylab import *
import matplotlib.mlab as mlab
import matplotlib.colors as colors
x=[1,2,3,4,5]
y=[15,23,40,38,65]
plt.plot(x,y)
axhline(40,linestyle='--',linewidth=2, color='red')
plt.fill_between(x, y, 40, where=(y>=40), facecolor='green', edgecolor='green')
show()
说明:如果我把plt.fill_between()这个函数注释掉,程序就没有错误,就可以运行;但是如果加上,就会出现上述错误 展开
1个回答
展开全部
你这个dimensions的错误。是因为x,y的类型。
你仔细看一下numpy,先初始化好。就没有这个问题了。
x,y的数组长度不一致。
你仔细看一下numpy,先初始化好。就没有这个问题了。
x,y的数组长度不一致。
更多追问追答
追问
x,y数组的长度是一致的。numpy怎么初始化呢?我把程序发上去了~~谢谢
追答
没有看到程序啊。我通常不记得,用到时再学。我给你找一下我的例子
import numpy as np
import pylab as pl
from StringIO import StringIO
import matplotlib.pyplot as plt
import matplotlib.mlab as mlab
import random
x1=np.arange(startx,endx,1) #np.arange(len(keys))
y1=np.array([v for v,i in tmp[1:selected_number]])
好象还有zero以及one之类的用法。 保证array里的list长度相同的。
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询