0
votes

I am trying to make great a visualisation of galaxy clusters in the sky, with their relative sizes. So I am making a dot-plot of each galaxy cluster, and then trying to place a circle around each one.

Here is my code so far:

import matplotlib.pyplot as plt
from pylab import *

import csv

with open("/Users/Olly/Documents/FINALVALUES/xPeak.csv", "rU") as x:
    reader = csv.reader(x)
    list1 = list(reader)

with open("/Users/Olly/Documents/FINALVALUES/yPeak.csv", "rU") as y:
    reader = csv.reader(y)
    list2 = list(reader)

with open("/Users/Olly/Documents/FINALVALUES/radius.csv", "rU") as r:
    reader = csv.reader(r)
    list3 = list(reader)

xList = []  
yList = []
rList = []

for i in list1:
    for j in i:
        xList.append(int(j))

for i in list2:
    for j in i:
        yList.append(int(j))

for i in list3:
    for j in i:
        rList.append(int(j))

#At this point I have my 3 lists for plotting

plt.figure(1)
plt.rc('text', usetex = True)
plt.rc('font', family = 'serif')
plt.plot(xList, yList, marker = '.', color = 'k', linestyle = 'None')
plt.title('W1 galaxy cluster field')
plt.xlabel(r'$\vartheta$ (arcmins)')
plt.ylabel(r'$\vartheta$ (arcmins)')                
for i in range(len(xList))
    circle1 = plt.Circle((xList[i], yList[i]), rList[i], color = 'r')
    fig, ax = plt.subplots()
    ax.add_artist(circle1)
plt.show()  

(xList and yList are the (x,y) coordinates of the centre of the galaxy cluster, and rList is the radius of the cluster.)

So I start by initially creating my dot plot. Then I loop over each data point so that I can design a separate circle for each data point.

As far as I am aware, writing 'fig, ax' should cause the circle created in that loop to be added to the pre-defined figure(1). So I don't see why multiple plots are coming up for each circle.

How can I modify this so that I create a circle for all 354 data points and get them all on the same graph?

(Edit: This is unique to other questions on stack insofar as the other questions do not involve loops that result in multiple plots. The issue here is not how to plot a circle - as in the other questions - but rather how to plot many circles when a loop is necessary in the code.)

1
Possible duplicate of matplotlib: add circle to plot - Julien

1 Answers

4
votes

As far as I am aware, writing 'fig, ax' should cause the circle created in that loop to be added to the pre-defined figure(1).

As written, this will actually produce a new figure and axes pair for each index in your xList. Try replacing the line:

plt.figure(1)

with:

fig, ax = plt.subplots()

and modifying the loop to be:

for i in range(len(xList)):
    circle1 = plt.Circle((xList[i], yList[i]), rList[i], color = 'r')
    ax.add_artist(circle1)

this way you avoid creating new figure instances for each index, while maintaining references to the figure object as fig and the axes object as ax. I can't test it with your actual data, but I think the final snippet should be:

fig, ax = plt.subplots()
plt.rc('text', usetex = True)
plt.rc('font', family = 'serif')
plt.plot(xList, yList, marker = '.', color = 'k', linestyle = 'None')
plt.title('W1 galaxy cluster field')
plt.xlabel(r'$\vartheta$ (arcmins)')
plt.ylabel(r'$\vartheta$ (arcmins)')                
for i in range(len(xList))
    circle1 = plt.Circle((xList[i], yList[i]), rList[i], color = 'r')
    ax.add_artist(circle1)
plt.show()

I hope this helps.