Matplotlib plot Pandas df,如果有条件,则在索引值之间填充
Matplotlib plot Pandas df, fill between index values if condition
我有一个 Pandas df:
ro laws ro ordos
January 468 579
February 216 231
March 1005 276
April 1044 250
May 962 276
June 999 568
July 1360 298
August 56 934
September 202 289
October 901 324
November 1056 412
December 1121 525
我使用 matplotlib 绘制它,它按预期工作。我想在两条线之间填充,但仅当 x 轴为 'January'
、'February'
、'March'
.
时
我试过了:
ax.fill_between(plotMonths.index, plotMonths['ro laws'], plotMonths['ro ordos'], where=(plotMonths.index in ['January', "February", 'March']),
facecolor='lightskyblue',
alpha=0.2)
但它抛出错误:
ValueError: The truth value of an array with more than one element is ambiguous. Use a.any() or a.all()
我试过了:
d = plotMonths.index.values.tolist()
ax.fill_between(d, plotMonths['ro laws'], plotMonths['ro ordos'], where=(d in ['January', "February", 'March']),
facecolor='lightskyblue',
alpha=0.2)
但它什么也没做,情节未填充。
我该如何解决这个问题?
谢谢。
Python in
关键字检查可迭代对象中的元素,但您正在使用它来检查数组 (plotMonths.index
) 是否在可迭代对象中 ['January', "February", 'March']
.
向量化条件的正确方法是使用内置方法 .isin()
.
所以,你应该使用:
ax.fill_between(x = plotMonths.index,
y1 = plotMonths['ro laws'],
y2 = plotMonths['ro ordos'],
where = plotMonths.index.isin(['January', "February", 'March']),
facecolor = 'lightskyblue',
alpha = 0.2)
只需切分您的数据。例如前三个月 = [0:3],像这样:
import matplotlib.pyplot as plt
ax.plot(plotMonths.index, plotMonths['ro laws'])
ax.plot(plotMonths.index, plotMonths['ro ordos'])
ax.fill_between(plotMonths.index[0:3], plotMonths['ro laws'][0:3])
ax.fill_between(plotMonths.index[0:3], plotMonths['ro ordos'][0:3])
plt.show()
它只会填充您想要的子集。
这是一个 image 结果。
我有一个 Pandas df:
ro laws ro ordos
January 468 579
February 216 231
March 1005 276
April 1044 250
May 962 276
June 999 568
July 1360 298
August 56 934
September 202 289
October 901 324
November 1056 412
December 1121 525
我使用 matplotlib 绘制它,它按预期工作。我想在两条线之间填充,但仅当 x 轴为 'January'
、'February'
、'March'
.
我试过了:
ax.fill_between(plotMonths.index, plotMonths['ro laws'], plotMonths['ro ordos'], where=(plotMonths.index in ['January', "February", 'March']),
facecolor='lightskyblue',
alpha=0.2)
但它抛出错误:
ValueError: The truth value of an array with more than one element is ambiguous. Use a.any() or a.all()
我试过了:
d = plotMonths.index.values.tolist()
ax.fill_between(d, plotMonths['ro laws'], plotMonths['ro ordos'], where=(d in ['January', "February", 'March']),
facecolor='lightskyblue',
alpha=0.2)
但它什么也没做,情节未填充。
我该如何解决这个问题?
谢谢。
Python in
关键字检查可迭代对象中的元素,但您正在使用它来检查数组 (plotMonths.index
) 是否在可迭代对象中 ['January', "February", 'March']
.
向量化条件的正确方法是使用内置方法 .isin()
.
所以,你应该使用:
ax.fill_between(x = plotMonths.index,
y1 = plotMonths['ro laws'],
y2 = plotMonths['ro ordos'],
where = plotMonths.index.isin(['January', "February", 'March']),
facecolor = 'lightskyblue',
alpha = 0.2)
只需切分您的数据。例如前三个月 = [0:3],像这样:
import matplotlib.pyplot as plt
ax.plot(plotMonths.index, plotMonths['ro laws'])
ax.plot(plotMonths.index, plotMonths['ro ordos'])
ax.fill_between(plotMonths.index[0:3], plotMonths['ro laws'][0:3])
ax.fill_between(plotMonths.index[0:3], plotMonths['ro ordos'][0:3])
plt.show()
它只会填充您想要的子集。 这是一个 image 结果。