David Schroeder
David Schroeder

Reputation: 27

matplotlib plot filled with different layers by loop, colouring

I produce multiple plots containing each 5 subplots, generated in a for loop. How can I define the coloring of the subplots? Do I need something like a Matrix with numbers and colors and use it somehow like Matrix[z] instead of the Color?

 fig = plt.figure()
 ax = fig.add_subplot(111)
  for z in Var
    ax.plot(x, y, color='black', alpha=0.5 , label=labelString)

Upvotes: 0

Views: 607

Answers (1)

Sheldore
Sheldore

Reputation: 39042

It is unclear what you exactly mean. But if you mean plotting 5 different curves in the same plot, each in different color, this is one way you can do it. This allows you to choose colors as you want. In case you do not specify colors manually like in the code below, python will assign colors automatically. In that case you just have to write ax.plot(x, y, label=r'y=%dx$^2$' %(i+1))

import numpy as np
import matplotlib.pyplot as plt
fig = plt.figure(figsize=(8, 5))
ax = fig.add_subplot(111)
colors = ['r', 'g', 'b', 'k', 'y']
x = np.linspace(0, 5, 100)
for i in range(5):
    y = (i+1)*x**2
    ax.plot(x, y, color=colors[i], label=r'y=%dx$^2$' %(i+1))

plt.legend(fontsize=16)  

Output

enter image description here

Upvotes: 1

Related Questions