Python plotting colors & labels for an unknown number of lines without loop

走远了吗. 提交于 2020-01-17 06:54:31

问题


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?

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

I have a line of code like this:

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

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


回答1:


You can access the Line2D objects that are created using the return value of plt.plot, and set the legend directly based on that list:

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.

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. 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)
    
  2. 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)
    
  3. Set up the plot using the object-oriented API to begin with, and apply changes only to the Axes that you actually want to modify:

    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)
    

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.



来源:https://stackoverflow.com/questions/43851913/python-plotting-colors-labels-for-an-unknown-number-of-lines-without-loop

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!