Python 地块内单线的接入和变化特征

Python 地块内单线的接入和变化特征,python,pandas,matplotlib,plot,dataframe,Python,Pandas,Matplotlib,Plot,Dataframe,使用pandas dataframes的内置功能进行绘图,例如,未堆叠的面积图,如下所示: df = pd.DataFrame(np.random.randn(11, 3)+3, columns=['r', 'g', 'b']) df.plot(kind='area', stacked=False, alpha=0.75) 产生类似这样的结果: 如何事后仅更改一条线的样式,例如更改其颜色、线宽和不透明度级别等?如果捕获由pandas.plot()返回的轴,则如下所示: ax = df.plo

使用pandas dataframes的内置功能进行绘图,例如,未堆叠的面积图,如下所示:

df = pd.DataFrame(np.random.randn(11, 3)+3, columns=['r', 'g', 'b'])
df.plot(kind='area', stacked=False, alpha=0.75)
产生类似这样的结果:


如何事后仅更改一条线的样式,例如更改其颜色、线宽和不透明度级别等?

如果捕获由
pandas.plot()返回的
轴,则如下所示:

ax = df.plot(kind='area', stacked=False, alpha=0.75)
ax.collections[0].set_color('color_name')
然后,您可以访问属性,如
,并设置参数,如
颜色
(有关可用参数的详细信息):

对于您的扩展问题,可以通过如下方式修改该区域:

更改
集合
的索引可以更新特定项目。由于这些是可重用的,您还可以迭代
集合
,并执行以下几项操作:

for line in ax.lines:
    line.set_kwarg(foo)

正如@StefanJansen所指出的,您可以通过从给定的
轴访问
来编辑行的
颜色

您还可以修改其他属性,如:

ax.lines[0].set_linewidth(2)         # set linewidth to 2
ax.lines[0].set_linestyle('dashed')  # other options: 'solid', 'dashdot` or `dotted`
ax.lines[0].set_alpha(0.5)           # Change the transparency
ax.lines[0].set_marker('o')          # Add a circle marker at each data point
ax.lines[0].set_markersize(2)        # change the marker size. an alias is set_ms()
ax.lines[0].set_markerfacecolor      # or set_mfc()
ax.lines[0].set_markeredgecolor      # or set_mec()
要更改曲线下的区域,需要访问存储在
轴中的
集合
。这里有用的属性是
color
alpha

ax.collections[0].set_color('yellow')
ax.collections[0].set_alpha(0.3)

显然,在这些示例中,您可以更改索引
0
,以修改其他
/
集合

,这些行非常有效。是否也有类似的方法来操作相应行下的相应区域?您也可以使用
fill_-between
,如下所示:@iMo51:是,从
轴访问
集合
。请看我的答案,谢谢你的回答。非常有用。添加以下代码完成了所有操作:ax=df.plot(kind='area',stacked=False)ax.lines[0]。set_color('yellow')ax.collections[0]。set_color('yellow'),但是,当我想用ax.legend()更新图例时,现在的显示方式有所不同,好像所有的线都是直线而不是区域。有什么提示吗?
ax.collections[0].set_color('yellow')
ax.collections[0].set_alpha(0.3)