0

I am trying to plot the graph for 25 samples, I am able to plot it individually but I am unable to plot them all in single plot. I need 8 samples plot with line and different colors, 8 samples with dotted lines and different colors and also the remaining with dashed lines with different colors. I would like to plot all of them in one graph.

Thank you.

import matplotlib.pyplot as plt
xs = []
ys = []
for i in range(25):
    subregion_average = np.mean(read_data['sub_region'][i], axis =(0,1))
    wavelength = read_data['wavelength'][i]
    xs.append(wavelength)
    ys.append(subregion_average)
for i in range(len(ys)):
    colours=['r','g','b','k','r','c','m','y']
    if i<=7:
      ax.plot(xs[i], ys[i], 'k--', linewidth = 2)
    else if 8<=i<=15:
      ax.plot(xs[i], ys[i], 'k:', linewidth = 2)
    else:
      ax.plot(xs[i], ys[i], 'k', linewidth = 2)
      
    ax.set_xlim(min(wavelength), max(wavelength))
    ax.tick_params(labelsize = 12)
    ax.set_title('Average Spectrum Calibrated sample', fontsize = 14, weight = 'bold')
    ax.set_ylabel('Reflectance', fontsize = 14)
    ax.set_xlabel('Wavelength in nm', fontsize = 14)
    ax.grid(True)

############

SOLUTION: I got the solution for it, it might not be high level but this is how I want the logic and got all graphs in single plot. Thanks for the inputs.

colours =  ['r','g','b','k','r','c','m','y','r','g','b','k','r','c','m','y','r','g','b','k','r','c','m','y','g','k']
for i in range(len(ys)):
    if i<=7:
       plt.plot(xs[i], ys[i], 'k--', c=colours[i], linewidth = 2)
    elif 8<=i<=15:
       plt.plot(xs[i], ys[i], 'k:', c=colours[i], linewidth = 2)
    else:
       plt.plot(xs[i], ys[i], 'k', c=colours[i], linewidth = 2) 
    plt.xlim(min(wavelength), max(wavelength))
    plt.tick_params(labelsize = 12)
    plt.title('Average Spectrum Calibrated sample', fontsize = 14, weight = 'bold')
    plt.ylabel('Reflectance', fontsize = 14)
    plt.xlabel('Wavelength in nm', fontsize = 14)
    plt.grid(True)
    plt.gcf().set_size_inches(12,12)
    plt.savefig("Spectrum_Plots",dpi=500)

3
  • You can use a cycler to create different combinations of colors/line styles: stackoverflow.com/a/66259659/8881141 Commented Feb 25, 2021 at 10:57
  • You create a new figure every iteration. Commented Feb 25, 2021 at 11:02
  • You should first create the grid of subplots with the layout you want by using plt.subplots(nrows, ncols). Then you can loop through the array of Axes that you can zip with the lists of corresponding variables to plot, colors and line styles with zip(axs.flat, xs, ys, colors, linestyles) as shown here. Or you could use an indexing approach using enumerate(axs.flat) as shown here, or a cycler as suggested by Mr. T. Commented Feb 25, 2021 at 11:26

0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.