0

I have a list and a python array like these 2 examples:

example:

Neg = [37.972200755611425, 32.14963079785344]

Pos = array([[15.24373185, 13.66099865, 11.86959384,  9.72792045,  7.12928302, 6.04439412],[14.5235007 , 13.        , 11.1792871 ,  9.14974712,  6.4429435 , 5.04439412]

both Neg and Pos have 2 elements (in this example) therefore I would like to make 2 separate plots (pdf file) for every element. in every plot there would be 2 lines: 1- comes from Pos and is a line plot basically which is made of all the elements in the sub-list. 2- comes from Neg and is a horizontal line on the y-axis.

I am trying to do that in a for loop for all elements at the same time. to do so, I made the following code in python but it does not return what I would like to get. do you know how to fix it ?

for i in range(len(Neg)):
    fig = plt.figure()
    ax = fig.add_subplot(1, 1, 1)
    ax.plot(concentration, Pos[i], label='gg')
    plt.axhline(y=Neg[i], color='b', linestyle='-')
    ax.legend()
    ax.set_xlabel("log2 concentration")
    ax.set_ylabel("log2 raw counts")
    ax.set_ylim(0, 40)
    plt.savefig(f'{i}.pdf')
user7249622
  • 115
  • 1
  • 9

1 Answers1

0

Not quite sure exactly what you want but this code creates two subplots of the data in the way I think you're describing it:

import numpy as np
from matplotlib import pyplot as plt

Neg = [37.972200755611425, 32.14963079785344]
Pos = np.array([[15.24373185, 13.66099865, 11.86959384,  9.72792045,  7.12928302, 6.04439412],[14.5235007 , 13.        , 11.1792871 ,  9.14974712,  6.4429435 , 5.04439412]])

fig = plt.figure()
for i in range(len(Neg)):
    ax = fig.add_subplot(2,1,i+1)
    ax.plot(Pos[i], label='gg')
    plt.axhline(y=Neg[i], color='b', linestyle='-')
    ax.legend()
    ax.set_xlabel("log2 concentration")
    ax.set_ylabel("log2 raw counts")
    ax.set_ylim(0, 40)
    plt.subplots_adjust(hspace=1.0)

    extent = ax.get_window_extent().transformed(fig.dpi_scale_trans.inverted())
    fig.savefig(f'{i}.pdf', bbox_inches=extent.expanded(1.2, 1.9))

Edited the code to save each subplot individually to file by grabbing a specific part of the plot for saving, as used in this question: Save a subplot in matplotlib.

Also included some additional spacing between each subplot by calling subplots_adjust(), so that each subplot can be saved to individual files without any detail from the other subplots being included. This might not be the best way of doing what you want, but I think it will do what you want now.

Alternatively, if you're not set on using subplots, you could always just use a plot per element:

fig = plt.figure()
for i in range(len(Neg)):
    plt.plot(Pos[i], label='gg')
    plt.axhline(y=Neg[i], color='b', linestyle='-')
    plt.legend()
    plt.xlabel("log2 concentration")
    plt.ylabel("log2 raw counts")
    plt.ylim(0, 40)
    fig = plt.gcf()
    fig.savefig(f'{i}.pdf')          
    plt.show()
humbleHacker
  • 437
  • 6
  • 17
  • this code has a problem. it makes 2 pdf files and the problem is the 2nd file which has 2 plots (subplots). and if I use this code for a list with 20 elements, the last file has 20 subplot. but every single file should have only one plot – user7249622 Aug 23 '19 at 14:25
  • Changed the code to save one subplot per file. Try it out. You may need to adjust the extent.expanded() parameters according to your needs – humbleHacker Aug 23 '19 at 16:01
  • yes thanks. the last code right these comments works, but still there is a problem. if plt.show() is in the code that prints all the files like expected. but the problem with this is that every time I have to close the figures. and if I remove plt.show() the plots will be together in the same file. how can I remove plt.show() but get one file per plot? – user7249622 Aug 26 '19 at 14:20
  • Sorry dude, this has got beyond my knowledge. You could try calling `plt.close('all')` in your code or the python console, which closes all windows at once, or try again with editing the subplots code idea at the top of the answer – humbleHacker Aug 26 '19 at 15:49