Subplots Not Working Properly

12 views
Skip to first unread message

Samuel Grima

unread,
Dec 29, 2022, 6:51:03 AM12/29/22
to pynbody-users
Hi, 

I am trying to obtain a plot with 3 different subplots next to one another. The class used is pynbody.plot.stars.render() as I am trying to plot side-on images of the stars in a galaxy, where every subplot is a different age range. I am trying to assign the subplots by using the 'axes' keyword argument within the brackets, however no plot is being produced. 

I tried using the example given in one of the tutorials in the Pynbody documentation (specifically, I used sph.velocity_image() where instead of 'axes' it takes 'subplots') and this worked, so the problem must be with the 'axes' argument.

Here is my code snippet:

fig = plt.figure()
plt1 = plt.subplot(131)
pynbody.plot.stars.render(young, axes=plt1)
plt2 = plt.subplot(132)
pynbody.plot.stars.render(intermediate, axes=plt2)
plt3 = plt.subplot(133)
pynbody.plot.stars.render(old, axes=plt3)

fig.suptitle(f"Images of stars with different ages for Simualtion {i}")
fig.savefig(f"Task 2 - Question 2 - sim{i}ages.pdf")

P.S. young, intermediate and old are filtered simulations of the stars according to their age.

Any suggestions as to why this is happening, please? Any help is greatly appreciated.

Thanks.

Kind regards,
Samuel

Eliza Diggins

unread,
Dec 30, 2022, 12:07:39 PM12/30/22
to pynbody-users
Hey Samuel,

It would be most helpful if you could provide a code snippet which replicates the unwanted behavior for us to dig into. 

Without a replicable test case, my advice is limited, but one thing to try is to initialize your subplots through the figure itself. Thus, instead of calling something like

fig = plt.figure()
plt1 = plt.subplot(131)  # <-- subplot initialized directly from matplotlib.pyplot

try

fig = plt.figure()
plt1 = fig.add_subplot(131) #<--- initialize as a subclass of fig.

Again, this is only a guess! If that doesn't work, get a replicable piece of code and I'll do some more digging for you!

Best,
Eliza Diggins
Reply all
Reply to author
Forward
0 new messages