使用 Matplotlib 在两条线之间着色

乌纳尔迪

我想使用 axvspan() 函数来可视化我使用 Pandas DataReader 获得的 DataFrame。但是当我使用以下代码时,我看到一个错误并且子图中没有阴影。我该怎么办?谢谢你。

import matplotlib.pyplot as plt
import pandas_datareader.data as pdr
import pandas as pd
import datetime
start = datetime.datetime (2000,1,1)
end = datetime.datetime (2021,5,1)
df  = pdr.DataReader(['WFRBSB50215', 'WFRBST01134','WFRBST01122', 'WFRBSN09139', 'WFRBSB50189', 'WFRBST01110','WFRBSB50191'],'fred',start, end)
df.columns = ['Share of Total Net Worth Held by the Bottom 50% (1st to 50th Wealth Percentiles)',
              'Share of Total Net Worth Held by the Top 1% (99th to 100th Wealth Percentiles)', 
              'Share of Corporate Equities and Mutual Fund Shares Helb By Top1%(99th to 100th Wealth Percentiles)', 
              'Share of Financial Assets Held by the 90th to 99th Wealth Percentiles',
              'Share of Total Assets Held by the Bottom 50% (1st to 50th Wealth Percentiles)',
              'Share of Real Estate Held by the Top 1% (99th to 100th Wealth Percentiles)',
              'Share of Real Estate Held by the Bottom %50(1st to 50th Wealth Percentiles)'
              ]
ax = df.plot(subplots=True, layout=(7,1), figsize=(15,15), linewidth=3.5, colormap="summer")
ax.axvspan('2007-1-12', '2009-6-1', color='c', alpha=0.5)
ax.axvspan('2019-12-1', '2020-2-1',color= 'orange', alpha=0.5)
plt.xlabel('Date')
ax[0,].set_title('Share of Total Net Worth Held by the Bottom 50%')
ax[0,].set_ylabel('Percent of Aggregate')
ax[1,].set_title('Share of Total Net Worth Held by the Top 1%')
ax[1,].set_ylabel('Percent of Aggregate')
ax[2,].set_title('Share of Corporate Equities and Mutual Fund Shares Helb By Top 1%')
ax[2,].set_ylabel('Percent of Aggregate')
ax[3,].set_title('Share of Financial Assets Held by the 90th to 99th Wealth Percentiles')
ax[3,].set_ylabel('Percent of Aggregate')
ax[4,].set_title('Share of Total Assets Held by the Bottom 50% ')
ax[4,].set_ylabel('Percent of Aggregate')
ax[5,].set_title('Share of Real Estate Held by the Top 1%')
ax[5,].set_ylabel('Percent of Aggregate')
ax[6,].set_title('Share of Real Estate Held by the Bottom %50')
ax[6,].set_ylabel('Percent of Aggregate')
plt.tight_layout()
plt.style.use('seaborn-white')
plt.show()
亨利·埃克

尝试遍历所有子图并添加axvspan到特定的AxesSubplot

axes = df.plot(subplots=True, layout=(7, 1), figsize=(15, 15), linewidth=3.5,
               colormap="summer", ylabel='Percent of Aggregate', xlabel='Date')

for (ax,), col in zip(axes, df.columns):
    ax.axvspan('2007-1-12', '2009-6-1', color='c', alpha=0.5)
    ax.axvspan('2019-12-1', '2020-2-1', color='orange', alpha=0.5)
    ax.set_title(col)

plt.tight_layout()
plt.style.use('seaborn-white')
plt.show()

使用ylabelxlabelkwargs 的一些轻微的代码减少plot并从而df.columns不是手动设置子图标题

阴谋


axvspan. 最简单的方法是为每个添加一个标签,axvspan并在每次迭代结束时制作图例:

axes = df.plot(subplots=True, layout=(7, 1), figsize=(15, 15), linewidth=3.5,
               colormap="summer", ylabel='Percent of Aggregate', xlabel='Date',
               legend=False)

for (ax,), col in zip(axes, df.columns):
    ax.axvspan('2007-1-12', '2009-6-1', color='c', alpha=0.5,
               label='2008 Crisis')
    ax.axvspan('2019-12-1', '2020-2-1', color='orange', alpha=0.5,
               label='Pandemic')
    ax.set_title(col)
    ax.legend()

plt.style.use('seaborn-white')
plt.tight_layout()
plt.show()

在所有子图上绘制带有 axvspan 图例的图 2


或者,可以只为危机制作一个图例:

fig, axes = plt.subplots(nrows=7, figsize=(15, 15))
df.plot(subplots=True, ax=axes, linewidth=3.5,
        colormap="summer", ylabel='Percent of Aggregate', xlabel='Date')

for ax, col in zip(axes, df.columns):
    ax.axvspan('2007-1-12', '2009-6-1', color='c', alpha=0.5,
               label='2008 Crisis')
    ax.axvspan('2019-12-1', '2020-2-1', color='orange', alpha=0.5,
               label='Pandemic')
    ax.set_title(col)

handles, labels = axes[-1].get_legend_handles_labels()

fig.legend(handles[-2:], labels[-2:], title='Crises',
           loc='lower left', ncol=2)

plt.style.use('seaborn-white')
plt.tight_layout()
plt.show()

为危机绘制 3 个单一图例

^ 单个图例位于左下方。

本文收集自互联网,转载请注明来源。

如有侵权,请联系 [email protected] 删除。

编辑于
0

我来说两句

0 条评论
登录 后参与评论

相关文章

在极轴上的两条线之间着色一段(matplotlib)

使用matplotlib在两个轴线之间着色区域

在Matplotlib中绘制两条线之间的夹角的最佳方法

在3D图中的两条线之间着色

如何使用ggplot2在两条线之间绘制密度图?

如何使用python opencv测量同一图像中两条线之间的角度?

使用lineplot用seaborn绘制两条线

仅使用整数计算两条线的交点

Matplotlib:使用离散点填充两条曲线之间

Matplotlib + Seaborn-具有相同颜色的两条线?

在一个子图 matplotlib 中为两条不同的线使用滑块

查找使用houghlines opencv绘制的两条线的交点

使用绘图词典在同一图形上绘制两条线

如何在日内使用烛台在HighStocks中绘制两条线

使用ggplot2在同一图中绘制不同年份的两条线

我可以使用哪个R函数找到两条线的交点?

如何使用ggplot创建两条线和散点图

在 R 中使用 abline 在图形上添加两条线

使用R使用ggplot2画两条线并放下一条线

使用matplotlib如何在两条直方图线之间完成颜色填充?

两条线之间的距离

如何在ggplot2中为两条线之间的区域着色/着色?

使用Angular JS在高图表上用JSON数据绘制两条线的问题-名称未定义

使用 geom_line() 显示来自一个数据帧的两条线

如何使用numpy.linalg.solve给定点坐标找到两条线相交的地方?

使用 ggplot() 叠加两条线图的问题

获得两条等高线 matplotlib 的单个图

如何计算两条线之间的水平距离

查找并标记两条线之间的交点