Reputation: 139
I am beginner developer and I encountered a problem.
I tried to plot using matplotlib in python with different colors. I depicted the problem I have as simple as possible below.
list = [5,4,8]
x = [1,2,3,4]
y = [1,2,3,4]
for i in range(len(list)):
plt.plot((x[i], x[i+1]), (y[i], y[i+1]), color = ?list?)
As you notice, there are three different straight lines. I want to color them with different colors. Ultimately, I want to convert the above list into a color map or a cmap which are available to apply for matplotlib.plot.
Sorry for my humble english. I hope experts in stackoverflow help me for this problem.
Upvotes: 1
Views: 823
Reputation: 10789
First of all, mind that list
is a bad variable name because it conflicts with the function list()
ant the type list
in python.
Second, iterating over a range(len(variable))
is ok but less pythonic than iterating over enumerate(variable)
.
As for the color part, you can use HTML color names, hex codes, or rgb values as valid colors.
colors = ['blue', 'red', 'green', 'orange']
x = [1,2,3,4]
y = [1,2,3,4]
for i, c in enumerate(colors):
plt.plot((x[i], x[i]+1), (y[i], y[i]+1), color=c)
plt.show()
Which output this
Upvotes: 1
Reputation: 145
You should avoid naming a variable list
, as this is the Python command to make a list.
You could pick colors from the list of named colors:
color_list = ['tomato', 'gold', 'orchid']
for i in range(len(color_list)):
plt.plot((x[i], x[i+1]), (y[i], y[i+1]), color=color_list[i])
Upvotes: 1