我尝试使用for循环绘制一个按列值分组的图,而不知道该列中唯一值的数量
您可以看到下面的示例代码(没有for循环)和所需的输出
我想,每个情节将有不同的颜色和标记(如下所示)
代码如下:
import pandas as pd
from numpy import random
df = pd.DataFrame(data = random.randn(5,4), index = ['A','B','C','D','E'],
columns = ['W','X','Y','Z'])
df['W'] = ['10/01/2018 12:00:00','10/03/2018 13:00:00',
'10/03/2018 12:30:00','10/04/2018 12:05:00',
'10/08/2018 12:00:15']
df['W']=pd.to_datetime(df['W'])
df['Entity'] = ['C201','C201','C201','C202','C202']
print(df.head())
fig, ax = plt.subplots()
df[df['Entity']=="C201"].plot(x="W",y="Y",label='C201',ax=ax,marker='x')
df[df['Entity']=="C202"].plot(x="W",y="Y",label='C202',ax=ax, marker='o')
这是输出:
您可以先找出
df['Entity']
的唯一值,然后循环它们。要为每个实体自动生成新的标记,可以定义一些标记的顺序(比如下面的答案中的5个),这些标记将通过marker=next(marker)
重复完成最小答案
相关问题 更多 >
编程相关推荐