0

I would like to implement this code, which regards the visualization of some data.

In particular, I have to produce 10 plots that have the same structure as the code below. The only difference between them is the range of x.

In plot n.1 x will go be in range(0, 100, 10) ---> in which 0 goes until 9 for the 10 subplots.

In plot n.2 x will go be in range(100, 200, 10) ---> in which 100 goes until 109 for the 10 subplots.

In plot n.3 x will go be in range(200, 300, 10) ---> in which 200 goes until 209 for the 10 subplots.

and so on until: Plot n.10 x will go be in range(900, 1000, 10) ---> in which 900 goes until 909 for the 10 subplots.

fig, ((ax1,ax2, ax3, ax4, ax5), (ax6,ax7, ax8, ax9, ax10)) = plt.subplots(2,5, figsize =(40,20))
axs = [ax1,ax2, ax3, ax4, ax5, ax6,ax7, ax8, ax9, ax10]
for ax in axs:
    ax.grid()
    ax.set_xlabel('Time [s]', fontsize = 22)
    ax.set_ylabel('$C_{vv}(t)$', fontsize = 22)
ax1.title.set_text('$l_0 = 6.00$ $\mu$m')
ax2.title.set_text('$l_0 = 8.67$ $\mu$m')
ax3.title.set_text('$l_0 = 11.33$ $\mu$m')
ax4.title.set_text('$l_0 = 14.00$ $\mu$m')
ax5.title.set_text('$l_0 = 16.67$ $\mu$m')
ax6.title.set_text('$l_0 = 19.33$ $\mu$m')
ax7.title.set_text('$l_0 = 22.00$ $\mu$m')
ax8.title.set_text('$l_0 = 24.67$ $\mu$m')
ax9.title.set_text('$l_0 = 27.33$ $\mu$m')
ax10.title.set_text('$l_0 = 30.00$ $\mu$m')
fig.suptitle('$\lambda = 0.23 $', fontsize = 30)


for x in range(100, 200, 10):
    ax1.plot(VACF[x][:1000,0], VACF[x][:1000,1], alpha=0.5)

for x in range(101, 200, 10):
    ax2.plot(VACF[x][:1000,0], VACF[x][:1000,1], alpha=0.5)

for x in range(102, 200, 10):
    ax3.plot(VACF[x][:1000,0], VACF[x][:1000,1], alpha=0.5)
    
for x in range(103, 200, 10):
    ax4.plot(VACF[x][:1000,0], VACF[x][:1000,1], alpha=0.5)
    
for x in range(104, 200, 10):
    ax5.plot(VACF[x][:1000,0], VACF[x][:1000,1], alpha=0.5)

for x in range(105, 200, 10):
    ax6.plot(VACF[x][:1000,0], VACF[x][:1000,1], alpha=0.5)

for x in range(106, 200, 10):
    ax7.plot(VACF[x][:1000,0], VACF[x][:1000,1], alpha=0.5)

for x in range(107, 200, 10):
    ax8.plot(VACF[x][:1000,0], VACF[x][:1000,1], alpha=0.5)
    
for x in range(108, 200, 10):
    ax9.plot(VACF[x][:1000,0], VACF[x][:1000,1], alpha=0.5)
    
for x in range(109, 200, 10):
    ax10.plot(VACF[x][:1000,0], VACF[x][:1000,1], alpha=1)    

plt.show()

how do I implement and make the code a little bit shorter? Thank you for the help

Trenton McKinney
  • 56,955
  • 33
  • 144
  • 158
user332496
  • 11
  • 2

1 Answers1

0
  • Flatten the axes with .ravel() as shown in this answer.
  • Use zip to combine all the values for each axes and unpack them with a for-loop
  • range(100, 110) represents the the initial value for x in each of the plot loops.
    • range(0, 100, 10) goes from 0 to 90 in steps of 10
    • range(0, 10, 1) goes from 0 to 9 in steps of 1
fig, axes = plt.subplots(2,5, figsize =(40,20))
axes = axes.ravel()

title_txt = [6.00, 8.67, 11.33, 14.00, 16.67, 19.33, 22.00, 24.67, 27.33, 30.00]

for ax, xi, txt in zip(axes, range(100, 110), title_txt):
    for x in range(xi, 200, 10):
        ax.plot(VACF[x][:1000, 0], VACF[x][:1000, 1], alpha=0.5)
    ax.grid()
    ax.set_xlabel('Time [s]', fontsize = 22)
    ax.set_ylabel('$C_{vv}(t)$', fontsize = 22)
    ax.title.set_text(f'$l_0 = {txt}$ $\mu$m')
    
fig.suptitle('$\lambda = 0.23 $', fontsize = 30)

  • The pattern described in the text at the top of the OP can be implemented as follows:
for base in range(0, 1000, 100):
    for x in range(base+0, base+10, 1):

# resulting range for each loop
[0, 1, 2, 3, 4, 5, 6, 7, 8, 9]
[100, 101, 102, 103, 104, 105, 106, 107, 108, 109]
[200, 201, 202, 203, 204, 205, 206, 207, 208, 209]
[300, 301, 302, 303, 304, 305, 306, 307, 308, 309]
[400, 401, 402, 403, 404, 405, 406, 407, 408, 409]
[500, 501, 502, 503, 504, 505, 506, 507, 508, 509]
[600, 601, 602, 603, 604, 605, 606, 607, 608, 609]
[700, 701, 702, 703, 704, 705, 706, 707, 708, 709]
[800, 801, 802, 803, 804, 805, 806, 807, 808, 809]
[900, 901, 902, 903, 904, 905, 906, 907, 908, 909]
  • Substituted into the plot loop
for ax, xi, txt in zip(axes, range(0, 1000, 100), title_txt):
    for x in range(xi+0, xi+10, 1):
        ax.plot(VACF[x][:1000, 0], VACF[x][:1000, 1], alpha=0.5)
    ax.grid()
    ax.set_xlabel('Time [s]', fontsize = 22)
    ax.set_ylabel('$C_{vv}(t)$', fontsize = 22)
    ax.title.set_text(f'$l_0 = {txt}$ $\mu$m')
    
fig.suptitle('$\lambda = 0.23 $', fontsize = 30)
  • In this example, the same set of sine waves with different frequencies is plotted in each subplot

enter image description here

Trenton McKinney
  • 56,955
  • 33
  • 144
  • 158