pandas 初识(六)-可视化

Pandas 在一张图中绘制多条线

import pandas as pd
import numpy as np
import matplotlib.pyplot as plt
df = pd.DataFrame({
    'color': ['red','red','red','blue','blue','blue'], 
    'x': [0,1,2,3,4,5],'y': [0,1,2,9,16,25]
})

实现

fig, ax = plt.subplots()

for key, group in df.groupby(['color']):
    ax = group(ax=ax, kind='line', x='x', y='y', c=key, label=key)

plt.legend()
plt.show()

也可以

ax = None
for key, group in df.groupby(['color']):
    ax = group(ax=ax, kind='line', x='x', y='y', c=key, label=key)

plt.legend()
plt.show()

 实现二,序列形式

df_r = df[df["color"] == "red"]
df_b = df[df["color"] == "blue"]
plt.title('混合图')  #标题
plt.plot(df_r.x, df_r.y, color='red', label='red')
plt.plot(df_b.x, df_b.y, color='blue', label='blue')
plt.legend()  #显示上面的label
plt.xlabel('index')
plt.ylabel('count')
plt.show()
原文地址:https://www.cnblogs.com/spaceapp/p/11771945.html