Skip to content Skip to sidebar Skip to footer

Plotting Shapes In Matplotlib Through A Loop

plotting beginner here... I'm trying to get Matplotlib to add circles to a figure in a loop such that it runs 'N' times. N is given by user input and for my application likely won'

Solution 1:

The pseudocode is already quite good. There is no need for a list. Just loop up till N and each time add the circle to the axes.

import matplotlib.pyplot as plt
import numpy as np

fig, ax = plt.subplots(figsize=(6,2))
ax.set_aspect("equal")
N = 10

circle_list = []
circle = []

circle_size = np.random.rand(N)/5.+0.1
circle_x = np.arange(N)+1

for i in range(N):
    c = plt.Circle((circle_x[i],0), circle_size[i])
    ax.add_artist(c)

plt.xlim([0,N+1])
plt.ylim([-1,1])

plt.savefig(__file__+".png")    
plt.show()

enter image description here

Solution 2:

I had a friend in the office help me, here's our solution

for i, c in zip(xrange(len(circle_x)), color_lst):
    circle = plt.Circle((circle_x[i], 0), circle_size[i], color=c, alpha=0.9)
    ax.add_artist(circle)

We also added a different colour for each one, so you need a list of colours too.

Post a Comment for "Plotting Shapes In Matplotlib Through A Loop"