matplotlib来自循环的绘图值作为同一绘图上的单条线,基于循环变量分配图例 [英] matplotlib plot values from loop as individual lines on the same plot, assign legend based on loop variable

查看:42
本文介绍了matplotlib来自循环的绘图值作为同一绘图上的单条线,基于循环变量分配图例的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个循环,该循环进行一些计算以获取填充 pcorr_list 的值,然后将该数据绘制为现有图上的一条线.

I have a loop that does some calculations to get the values that populate pcorr_list and then plots this data as a line on an existing plot.

    for event in event_name_list:
        event_string = str(event)
        event_name = 'AGW_' + str(event_string)

        ###some code to calculate pcorr1 : pcorr5

        pcorr_list = [pcorr1, pcorr2, pcorr3, pcorr4, pcorr5]
        plt.plot(turbine_list_pcorr, pcorr_list,'-o', label = event_name)
plt.legend(event_name, loc = 'best')
plt.show()

我想让图例中的图例显示循环中相应 event 的pcorr数据的 event_name .

I want a legend for the plot show event_name for the pcorr data for the corresponding event in the loop.

此刻,图例是循环中的最后一个事件,分为单个字符,与图中的线条无关.

At the moment the legend is the last event in the loop split into individual characters and doesn't relate to the lines in the plot.

plt.plot(turbine_list_pcorr,pcorr_list,'-o',label = str(event_name))返回相同的结果.

推荐答案

@Diziet Asahi评论

As commented by @Diziet Asahi

    for event in event_name_list:
        event_string = str(event)
        event_name = 'AGW_' + str(event_string)

        ###some code to calculate pcorr1 : pcorr5

        pcorr_list = [pcorr1, pcorr2, pcorr3, pcorr4, pcorr5]
        plt.plot(turbine_list_pcorr, pcorr_list,'-o', label = event_name)
plt.legend(loc = 'best')
plt.show()

这篇关于matplotlib来自循环的绘图值作为同一绘图上的单条线,基于循环变量分配图例的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆