How to implement automatic color change in matplotlib with subplots?

三世轮回 提交于 2019-12-11 19:49:31

问题


I am drawing charts with matplotlib. When i am drawing it at the same chart with this code:

def draw0(x, ys, labels):


    plt.suptitle("big title")

    i =0
    for y in ys:
        plt.plot(x, y, label=labels[i])
        plt.scatter(x, y)  # dots
        plt.xticks(range(1, max(x) + 1))
        plt.grid(True)
        i+=1

    plt.figlegend(loc="upper left")

    plt.show()

    return

x = [1,2,3,4,5]
y1 = [1,3,5,7,9]
y2 = [10,30,50,70,90]
y3 = [0.1,0.3,0.5,0.7,0.9]

draw0(x, [y1, y2, y3], ["chart1", "chart2", "chart3"])

everything works fine. charts in one window But i need each chart to be at the separate subplot.

I am trying to do it like this:

def draw11(x, ys, labels):

    plt.figure()

    plt.suptitle("big title")

    i =0
    for y in ys:
        if i == 0:
            ax = plt.subplot(len(ys),1, i+1)
        else:
            plt.subplot(len(ys), 1, i + 1, sharex=ax)
        plt.plot(x, y, label=labels[i])
        plt.scatter(x, y)  # dots
        plt.xticks(range(1, max(x) + 1))
        plt.grid(True)
        i+=1

    plt.figlegend(loc="upper left")

    plt.show()

    return

I am getting this.

charts in sublots

Issue is that all charts have the same color. And legend is useless. How i can add automatic color management for all sublots? I'd like to have not the same colors there. Like subplot1.chart1 = color1, subplo1.chart2 = color2, sublot2.chart1 = color3, not color1.


回答1:


Matplotlib has a built-in property cycler, which by default has 10 colors in it to cycle over. However those are cycled per axes. If you want to cycle over subplots you would need to use the cycler and get a new color from it for each subplot.

import matplotlib.pyplot as plt
colors = plt.rcParams["axes.prop_cycle"]()

def draw11(x, ys, labels):
    fig, axes = plt.subplots(nrows=len(ys), sharex=True)
    fig.suptitle("big title")

    for ax, y, label in zip(axes.flat, ys, labels):
        # Get the next color from the cycler
        c = next(colors)["color"]

        ax.plot(x, y, label=label, color=c)
        ax.scatter(x, y, color=c)  # dots
        ax.set_xticks(range(1, max(x) + 1))
        ax.grid(True)

    fig.legend(loc="upper left")
    plt.show()


x = [1,2,3,4,5]
y1 = [1,3,5,7,9]
y2 = [10,30,50,70,90]
y3 = [0.1,0.3,0.5,0.7,0.9]

draw11(x, [y1, y2, y3], ["chart1", "chart2", "chart3"])




回答2:


Just have a list of colors and select them for each plt.plot and plt.scatter.

colors = ['orange', 'cyan', 'green']
i =0
for y in ys:
        if i == 0:
            ax = plt.subplot(len(ys),1, i+1)
        else:
            plt.subplot(len(ys), 1, i + 1, sharex=ax)
        plt.plot(x, y, label=labels[i], c=colors[i])
        plt.scatter(x, y, c=colors[i])  # dots
        plt.xticks(range(1, max(x) + 1))
        plt.grid(True)
        i+=1


来源:https://stackoverflow.com/questions/53521396/how-to-implement-automatic-color-change-in-matplotlib-with-subplots

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