使用matplotlib绘制字典列表

时间:2018-11-07 00:24:17

标签: python list matplotlib

List =
[{'Month': '1', 'Store': 'A', 'Sales': '100'},
 {'Month': '2', 'Store': 'A', 'Sales': '50'},
 {'Month': '3', 'Store': 'A', 'Sales': '200'},
 {'Month': '1', 'Store': 'B', 'Sales': '300'},
 {'Month': '2', 'Store': 'B', 'Sales': '200'},
 {'Month': '3', 'Store': 'B', 'Sales': '250'}]

我确实知道如何绘制基本线。

但是如何将两个数据集合并在一起呢?

喜欢这个Expected result

1 个答案:

答案 0 :(得分:0)

这可以做到。将事物放在大熊猫中可以简化此操作-同样,绘制多条线,然后所有图形都将显示在同一张图表上。

import pandas as pd
import matplotlib.pyplot as plt

df = pd.DataFrame(your_data)
df[['Month', 'Sales']] = df[['Month', 'Sales']].apply(pd.to_numeric, errors='coerce')
a = df[df.Store == 'A']
b = df[df.Store == 'B']
fig = plt.figure(figsize=(10,6))
ax = fig.add_subplot(111)
a.plot('Month', 'Sales', ax=ax)
b.plot('Month', 'Sales', ax=ax)
ax.grid(True)
fig.set_facecolor('white')

enter image description here