简体   繁体   中英

Python matplotlib set color of graphs

I want to draw a stairstep diagram with pyplot using plt.step() . I pass my y-values as an array of np.array . Now i want to color the different graphs. How can i pass a set of graph-colors, which are applied to my different graphs in the diagram?

yvalues = [arrayA, arrayB, arrayC]    
ycolors = ["colorA", "colorB", "colorC"]
plt.step(xvalues, yvalues, ycolors)

I only know how to do it value by value:

plt.step(xvalues, arrayA, "colorA", arrayB, "colorB", arrayC, "colorC")

It can be done with a for loop

import itertools
import numpy as np
import matplotlib.pyplot as plt

# your x data
x = np.arange(10)

# your y data
data = np.arange(1,4)[:,None] * np.arange(10)[None,:]

# your colors
color = ['k','r','g']

fig,ax = plt.subplots(1)

for y,c in itertools.izip(data,color):
    ax.step(x,y,c)

plt.show()

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM