【问题标题】:Plotting pandas dataframe with string labels用字符串标签绘制熊猫数据框
【发布时间】:2016-06-02 17:15:17
【问题描述】:

我有一个包含多个字段的 pandas 数据框。重要的是:

In[191]: tasks[['start','end','appId','index']]
Out[189]: 
             start               end                           appId  index
2576 1464262540102.000 1464262541204.000  application_1464258584784_0012      1
2577 1464262540098.000 1464262541208.000  application_1464258584784_0012      0
2579 1464262540104.000 1464262541194.000  application_1464258584784_0012      3
2583 1464262540107.000 1464262541287.000  application_1464258584784_0012      6
2599 1464262540125.000 1464262541214.000  application_1464258584784_0012     26
2600 1464262541191.000 1464262541655.000  application_1464258584784_0012     28
.
.
.
2701 1464262562172.000 1464262591147.000  application_1464258584784_0013     14
2718 1464262578901.000 1464262588156.000  application_1464258584784_0013     28
2727 1464262591145.000 1464262602085.000  application_1464258584784_0013     40

我想为从坐标 (x1=start,y1=index),(x2=end,y1=index) 出发的每一行绘制一条线。每行将有不同的颜色,具体取决于字符串 appId 的值。这一切都是在我在时间序列图中的子图中完成的。我在这里发布代码,但重要的是 tasks.iterrows() 部分,您可以忽略其余部分。

def plot_stage_in_host(dfm,dfg,appId,stageId,parameters,host):
    [s,e] = time_interval_for_app(dfm, appId,stageId, host)
    time_series = create_time_series_host(dfg, host, parameters, s,e)
    fig,p1 = plt.subplots()
    p2 = p1.twinx()
    for para in parameters:          
        p1.plot(time_series.loc[time_series['parameter']==para].time,time_series.loc[time_series['parameter']==para].value,label=para)
    p1.legend()
    p1.set_xlabel("Time")
    p1.set_ylabel(ylabel='%')
    p1.set(ylim=(-1,1))
    p2.set_ylabel("TASK INDEX")
    tasks = dfm.loc[(dfm["hostname"]==host) & (dfm["start"]>s) & (dfm["end"]<e) & (dfm["end"]!=0)] #& (dfm["appId"]==appId) & (dfm["stageId"]==stageId)]
    apps = tasks.appId.unique()
    norm = colors.Normalize(0,len(apps))
    scalar_map = cm.ScalarMappable(norm=norm, cmap='hsv')
    for _,row in tasks.iterrows():
        color = scalar_map.to_rgba(np.where(apps == row['appId'])[0][0])
        p2.plot([row['start'],row['end']],[row['index'],row['index']],lw=4 ,c=color)
    p2.legend(apps,loc='lower right')
    p2.show()

这是我得到的结果。

显然没有考虑标签,图例对所有行显示相同的颜色。如何正确标记它们并显示图例?

【问题讨论】:

    标签: python pandas matplotlib


    【解决方案1】:

    问题在于,每次使用label= 参数在for 循环中绘制图形时,您都在分配标签。尝试删除它并给 p2.lengend() 一个字符串列表作为参数,代表您要显示的标签。

    p2.legend(['label1', 'label2'])
    

    如果您想为每一行指定不同的颜色,请尝试以下操作:

    import matplotlib.pyplot as plt
    import numpy as np
    xdata = [1, 2, 3, 4, 5]
    ydata = [[np.random.randint(0, 6) for i in range(5)],
            [np.random.randint(0, 6) for i in range(5)],
            [np.random.randint(0, 6) for i in range(5)]]
    colors = ['r', 'g', 'b']  # can be hex colors as well
    legend_names = ['a', 'b', 'c']
    for c, y in zip(colors, ydata):
        plt.plot(xdata, y, c=c)
    plt.legend(legend_names)
    plt.show()
    

    它给出以下结果:

    希望这会有所帮助!

    【讨论】:

    • !Image。还是同样的问题。我更感兴趣的是为每条线获得不同的颜色,而不是图例本身。
    • 谢谢!。我设法通过颜色图做到了一点不同,这与您的解决方案非常相似。但是我现在在绘制图例时遇到问题,因为没有标签。如何绘制图例?
    • @Brandon 我添加了代码来绘制图例。只需按照它们将被绘制的顺序给出标签名称。
    • 这行不通,因为可能有几行颜色相同,每种颜色代表一个应用程序。颜色的数量也不是固定的,因为应用程序的数量可能会有所不同。我已经编辑了最初的问题,添加了代码和我得到的结果。我快到了,但我无法为每个应用获取带有颜色的图例。
    • 如果我对您的理解正确,这个问题和答案可能会解决您的问题。 stackoverflow.com/questions/26337493/…
    猜你喜欢
    • 1970-01-01
    • 2017-05-28
    • 2022-01-08
    • 2017-02-24
    • 1970-01-01
    • 2016-01-07
    • 2016-04-10
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多