首页 > 解决方案 > 带有多个图形的图形中的线型

问题描述

我有一个看起来像这样的数据框:

            a       b           c           d           e
2020-06-01  90955   2814823.0   5422676.0   1135754.0   3716537.0
2020-07-01  100498  3116529.0   5908477.0   1368607.0   3056651.0
2020-08-01  97441   3107987.0   5702994.0   1340020.0   3235909.0
2020-09-01  95916   3073612.0   5803623.0   1275557.0   2951018.0
2020-10-01  94436   3090192.0   5943429.0   1262183.0   2882972.0
2020-11-01  92148   2826019.0   5495001.0   1214844.0   2567679.0

然后我像这样绘制数据框:

ax = df.plot(figsize=(8,3))
ax.autoscale(axis='x',tight=True)
ax.legend(loc=3, bbox_to_anchor=(1.0,0.1));

每列都有不同的颜色,图例在图的一侧(我想要的)。

如何更改每列的线类型?例如,一个是直线,另一个是虚线?

标签: pythonpandasnumpymatplotlibjupyter-notebook

解决方案


传递一个列表格式字符串作为styledf.plot 的kwarg

import pandas as pd
from matplotlib import pyplot as plt

df = pd.DataFrame({'a': {'2020-06-01': 90955, '2020-07-01': 100498,
                         '2020-08-01': 97441, '2020-09-01': 95916,
                         '2020-10-01': 94436, '2020-11-01': 92148},
                   'b': {'2020-06-01': 2814823.0, '2020-07-01': 3116529.0,
                         '2020-08-01': 3107987.0, '2020-09-01': 3073612.0,
                         '2020-10-01': 3090192.0, '2020-11-01': 2826019.0},
                   'c': {'2020-06-01': 5422676.0, '2020-07-01': 5908477.0,
                         '2020-08-01': 5702994.0, '2020-09-01': 5803623.0,
                         '2020-10-01': 5943429.0, '2020-11-01': 5495001.0},
                   'd': {'2020-06-01': 1135754.0, '2020-07-01': 1368607.0,
                         '2020-08-01': 1340020.0, '2020-09-01': 1275557.0,
                         '2020-10-01': 1262183.0, '2020-11-01': 1214844.0},
                   'e': {'2020-06-01': 3716537.0, '2020-07-01': 3056651.0,
                         '2020-08-01': 3235909.0, '2020-09-01': 2951018.0,
                         '2020-10-01': 2882972.0, '2020-11-01': 2567679.0}})

line_styles = ['bs-', '^', '--', 'o-']
ax = df.plot(figsize=(8, 3), style=line_styles)
ax.autoscale(axis='x', tight=True)
ax.legend(loc=3, bbox_to_anchor=(1.0, 0.1))

plt.show()

在此处输入图像描述


推荐阅读