0

I am plotting some data. One plot below the other. The top plot has two series with shared x-axis but different scales on the y-axis. The bottom plot has two series with the same x and y axis scales.

Here is an image of when I wish to produce:

enter image description here

The top plot is how I wish it to look. I am however, struggling to get data to plot on my second plot. My code looks as follows:

fig, axs = plt.subplots(2, 1, figsize=(11.69, 8.27))

color1 = 'red'
color2 = 'blue'
axs[0].set_title("Energy of an Alpha Particle Against Distance Through a {} Target".format(Sy_t), fontsize=15)
axs[0].set_ylabel("Alpha Energy (MeV)", fontsize=10, color=color1)
axs[0].set_xlabel("Distance travelled through target (cm)", fontsize=10)
axs[0].tick_params(axis='y', colors=color1)
axs[0].grid(lw=0.2)
axs[0].plot(dc['sum'], dc.index, marker='+', lw=0.2, color=color1)
axs[1] = axs[0].twinx()

#ax2.set_title("Stopping Power Against Distance through a {} Target".format(Sy_t), fontsize=15)
axs[1].set_ylabel("dE/dx (MeV/cm)", fontsize=10, color=color2)
axs[1].set_xlabel("Distance travelled through target (cm)", fontsize=10)
axs[1].plot(dc['sum'], dc['dydsum'], marker='+', lw=0.2, color=color2)
axs[1].tick_params(axis='y', colors=color2)
axs[1].grid(lw=0.2)
fig.tight_layout()

axs[2].set_title('Cross-Section Plots overall and for the product')
axs[2].set_ylabel('Cross-Section (mb)')
axs[2].set_xlabel('Energy (MeV)')
axs[2].plot(Sig_sum)
axs[2].plot(Sig_prod)
plt.show()

An error I get is:

IndexError: index 2 is out of bounds for axis 0 with size 2 

The data for the top plot is from a dataframe. The two series on the bottom have the same x and y scale with one of the dateframes shown below:

1        0.001591
2        0.773360
3       28.536766
4      150.651937
5      329.797010
6      492.450824
7      608.765402
8      697.143340
9      772.593010
10     842.011451
11     900.617395
12     947.129704
13     984.270901
14    1015.312625
15    1041.436808
16    1062.700328
17    1079.105564
18    1091.244022
19    1100.138834
20    1107.206041
21    1113.259507
22    1118.579315
23    1123.164236
24    1127.014592
25    1129.558439
26    1130.390331
27    1129.917985
28    1128.069117
29    1125.184650
30    1121.497063
31    1117.341899
32    1112.556194
33    1108.158215
34    1103.083775
35    1097.872010
36    1092.889581
37    1087.439353
38    1081.922461
39    1076.163363
40    1070.421916

When I try plotting the lower plot on it's own it graphically looks as below: enter image description here

Allentro
  • 406
  • 2
  • 13

1 Answers1

1

You probably would benefit from some breaking down of what happens. At the start, you call

fig, axs = plt.subplots(2, 1)

which splits the figure into two subplots (the axes.Axes class, technically). axs is then an array that contains the two axes objects.

Later on, you do

axs[1] = axs[0].twinx()

which does two things: it creates a third axes.Axes object (on top of the top subplot) and changes the value of axs[1] to refer to that object instead of the bottom subplot. You then have no (easy) way to access the bottom subplot (and trying to access an out-of-bounds index of axs makes the error immediate).

Possible fix:

fig, axs = plt.subplots(2, 1, ...)
topleftax = axs[0]
toprightax = axs[0].twinx()
bottomax = axs[1]
# and replace in the code that comes thereafter:
# axs[0] -> topleftax
# axs[1] -> toprightax
# axs[2] -> bottomax
Leporello
  • 638
  • 4
  • 12