matplotlib 如何绘制核密度图,但数据框的最后一列带有虚线?

5hcedyr0  于 2023-04-21  发布在  其他
关注(0)|答案(1)|浏览(142)

我有一个dataframe有过去6周的值。我需要绘制每周间隔的内核密度,但最后一周应该用虚线绘制。
下面是dataframe:最近6周的每周数据:Weekly data for last 6 weeks

Week 1 (10-Mar-2023 to 16-Mar-2023) Week 2 (17-Mar-2023 to 23-Mar-2023) Week 3 (24-Mar-2023 to 30-Mar-2023) Week 4 (31-Mar-2023 to 06-Apr-2023) Week 5 (07-Apr-2023 to 13-Apr-2023) Week 6 (14-Apr-2023 to 20-Apr-2023)
0   167.676448  57.401291   0.000000    32.985973   57.200695   67.172715
1   0.000000    49.341281   0.000000    28.365652   67.744039   58.914095
2   0.000000    56.827458   0.000000    57.531672   64.977708   59.417721
3   0.000000    46.623761   0.000000    31.301324   70.653764   55.513029
4   0.000000    76.401830   0.000000    32.724963   57.221279   56.995764
... ... ... ... ... ... ...
163 59.735175   0.000000    41.558026   57.023258   46.824477   0.000000
164 43.658799   0.000000    49.780197   45.339093   54.183334   0.000000
165 30.453307   0.000000    57.672768   57.958129   40.431805   0.000000
166 57.427614   0.000000    44.916778   36.708974   42.103965   0.000000
167 51.782234   0.000000    39.292057   49.510826   57.774710   0.000000

last5Week_df = weekly_CRF_df[weekly_CRF_df.columns[:-1]]
last5Week_df.plot.density()
lastWeek_df = weekly_CRF_df[weekly_CRF_df.columns[-1:]]
如何添加 lastWeek_df 与以前的图表,但与虚线。
我想要这样的东西:weekly kernel density plot

gopyfrb3

gopyfrb31#

您可以分两步进行绘图,首先绘制初始列,然后绘制最后一列:

ax = last5Week_df.iloc[:, :-1].plot.density(ls='-', legend=False)
last5Week_df.iloc[:, -1].plot.density(ls=':', ax=ax)
ax.figure.legend()

另一种选择是,打印所有并手动更改最后一行的样式:

ax = last5Week_df.plot.density(ls='-', legend=False)
ax.lines[-1].set_linestyle(':')
ax.legend()

输出:

相关问题