Python绘制颜色和颜色未知行数的标签,无循环 [英] Python plotting colors & labels for an unknown number of lines without loop

查看:92
本文介绍了Python绘制颜色和颜色未知行数的标签,无循环的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

通过研究,我找到了答案,其中涉及使用迭代器和循环来绘制n组数据.

Researching this, I have found answers which involved using an iterator and a loop to plot n sets of data.

有没有一种方法可以不使用循环?

Is there a way to do it without using a loop?

我有一个数组odata,其中包含n列.第一列是横坐标.我想剩下的图.

I have an array odata which contains n columns. The first column is the abscissa. I want to plot the rest.

我有一行这样的代码:

plt.plot(odata[:,0],odata[:,1:],'-')

这是情节,但是我想添加自己的颜色和标签.怎么样?

This does the plot, But I want to add my own colors and labels. How?

推荐答案

您可以访问 plt.plot ,然后直接根据以下内容设置传奇该列表:

labels = ['a', 'b', 'c']
lines = plt.plot(odata[:, 0], odata[:, 1:], '-')
plt.legend(lines, labels)

标签和行的数量不一定必须匹配.如果行数较少,则某些标签将不被使用.如果标签较少,则某些行将被取消标签. 此处是文档中的图例指南.

The number of labels and lines does not necessarily have to match. If there are fewer lines, some of the labels will be unused. If there are fewer labels, some of the lines will be unlabeled. Here is the legend guide in the documentation.

要无缝更改颜色顺序,您需要通过 matplotlib.rc ,如文档中示例所示,或使用面向对象的API进行绘图.然后,您可以在个人上使用 set_prop_cycle 轴而不会弄乱全局设置.

To seamlessly change the order of the colors, you would need to set the cycler in the global configuration via matplotlib.rc as in this example from the docs, or use the object-oriented API to do your plotting. Then you can use set_prop_cycle on your individual axes without messing around with the global settings.

这里有三种方法来设置颜色循环仪,以增加个人喜好的顺序.请注意,我仅在此处显示如何设置颜色,但是您还可以控制线型的顺序以及可能的其他属性:

Here are three approaches for setting the color cycler in order of increasing personal preference. Note that I am only showing how to set the color here, but you can also control the sequence of line styles and probably other attributes as well:

  1. 设置全局配置:

  1. Set the global configuration:

import matplotlib as mpl
from matplotlib import cycler
from matplotlib import pyplot as plt

labels = ['a', 'b', 'c']
colors = ['r', 'g', 'b']

mpl.rc('axes', prop_cycle=cycler('color', ['r', 'g', 'b', 'y']))

lines = plt.plot(odata[:, 0], odata[:, 1:], '-')
plt.legend(lines, labels)

  • 设置全局配置,但使用 rc_context 上下文管理器,以使更改有效地位于您的绘图的本地:

  • Set the global configuration, but using the rc_context context manager to make the changes effectively local to your plot:

    import matplotlib as mpl
    from matplotlib import cycler,rc_context
    from matplotlib import pyplot as plt
    
    labels = ['a', 'b', 'c']
    colors = ['r', 'g', 'b']
    
    with rc_context(rc={'axes.prop_cycle': cycler('color', ['r', 'g', 'b', 'y'])}):
        lines = plt.plot(odata[:, 0], odata[:, 1:], '-')
        plt.legend(lines, labels)
    

  • 首先使用面向对象的API设置图,并将更改仅应用于:

    from matplotlib import pyplot as plt
    
    labels = ['a', 'b', 'c']
    colors = ['r', 'g', 'b']
    
    fig, ax = plt.subplots()
    ax.set_prop_cycle('color', colors)
    ax.plot(odata[:, 0], odata[:, 1:], '-')
    ax.legend(labels)
    

  • 我建议将面向对象的API作为一般规则,尤其是在独立脚本中,因为它提供了更大的灵活性,并且在确切地了解将要操作的对象方面也非常清晰.

    I would recommend the object-oriented API as a general rule, especially within standalone scripts because it offers much greater flexibility, and also clarity in terms of knowing exactly what objects will be operated on.

    这篇关于Python绘制颜色和颜色未知行数的标签,无循环的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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