Individual plots into one figure using matplotlib python

Individual plots into one figure using matplotlib python



The goal is to generate a single figure with multiple subplots.
The following code generates 4 separate plots



Sublists to plot: Can the same loop be used on both sets of data? x vs. y and b vs. p
The difference between the two data sets is x vs. y will make 4 plots while b vs. p will make 2


x = [[1,2,3,4],[1,2,3,4],[1,2,3,4],[1,2,3,4]]
y = [[1,2,3,4],[2,3,4,5],[3,4,5,6],[7,8,9,10]]

p = [[17, 16, 6, 15, 6, 7, 6, 7, 9, 11], [16, 13, 9, 11, 12, 13, 6, 12, 13, 7]]
b = [[0, 1, 2, 3, 4, 5, 6, 7, 8, 9], [10, 11, 12, 13, 14, 15, 16, 17, 18, 19]]

colours=['r','g','b','k']



Loop that plots them


for i in range(len(x)):
plt.plot(x[i], y[i],colours[i])
plt.show()



Is it possible to create a function that loops through each created plot and adds it to one figure or a single pdf so that all 4 plots can be viewed at once?




1 Answer
1



Here is a solution to your problem using subplots:


subplots


from matplotlib import pyplot as plt

x = [[1,2,3,4],[1,2,3,4],[1,2,3,4],[1,2,3,4]]
y = [[1,2,3,4],[2,3,4,5],[3,4,5,6],[7,8,9,10]]
colours=['r','g','b','k']

fig, axes = plt.subplots(nrows=2, ncols=2)
i = 0 # moving index to plot different sublists of data
for r in axes:
for c in r:
c.plot(x[i], y[i], color=colours[i], label='Sublist %d'%(i+1))
i += 1
c.legend()

plt.show()



Alternative approach as suggested by @SpghttCd


for i, ax in enumerate(axes.flatten()):
ax.plot(x[i], y[i], color=colours[i], label='Sublist %d'%(i+1))
ax.legend()



Output



enter image description here



Dataset 2 (as provided in the comments below:


x = [[17, 16, 6, 15, 6], [7, 6, 7, 9, 11], [16, 13, 9, 11, 12], [13, 6, 12, 13, 7]]
y = [[0, 1, 2, 3, 4], [5, 6, 7, 8, 9], [10, 11, 12, 13, 14], [15, 16, 17, 18, 19]]



Output (Dataset 2)



enter image description here





Note that for better comparability there are these very convenient kwargs of subplot: sharex and sharey. Set to True, all subplots have the according axis equally scaled with the other subplots. Set to 'col' or 'row' these properties can even be limited to column- or row-wise.
– SpghttCd
Sep 4 '18 at 17:34






You could do this in a single loop by iterating over axes.flatten(). I.e. for i, ax in enumerate(axes.flatten()):...
– SpghttCd
Sep 4 '18 at 17:40






@SpghttCd: Thanks for the comment. You are right, One can do that. It's a matter of personal taste I guess. I personally use the above method because it gives me an idea of how the plots are splitted in rows as I often work with large number of subplots.
– Bazingaa
Sep 4 '18 at 17:45





I added your suggestion in my answer.
– Bazingaa
Sep 4 '18 at 17:46





Agreed of course, matter of taste and context. Sometimes still having access to both index dimensions may be necessary. Here with the index i, the use of enumerate makes sense imo. Thanks for adding!
– SpghttCd
Sep 4 '18 at 17:58



Thanks for contributing an answer to Stack Overflow!



But avoid



To learn more, see our tips on writing great answers.



Required, but never shown



Required, but never shown




By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.

Popular posts from this blog

𛂒𛀶,𛀽𛀑𛂀𛃧𛂓𛀙𛃆𛃑𛃷𛂟𛁡𛀢𛀟𛁤𛂽𛁕𛁪𛂟𛂯,𛁞𛂧𛀴𛁄𛁠𛁼𛂿𛀤 𛂘,𛁺𛂾𛃭𛃭𛃵𛀺,𛂣𛃍𛂖𛃶 𛀸𛃀𛂖𛁶𛁏𛁚 𛂢𛂞 𛁰𛂆𛀔,𛁸𛀽𛁓𛃋𛂇𛃧𛀧𛃣𛂐𛃇,𛂂𛃻𛃲𛁬𛃞𛀧𛃃𛀅 𛂭𛁠𛁡𛃇𛀷𛃓𛁥,𛁙𛁘𛁞𛃸𛁸𛃣𛁜,𛂛,𛃿,𛁯𛂘𛂌𛃛𛁱𛃌𛂈𛂇 𛁊𛃲,𛀕𛃴𛀜 𛀶𛂆𛀶𛃟𛂉𛀣,𛂐𛁞𛁾 𛁷𛂑𛁳𛂯𛀬𛃅,𛃶𛁼

Crossroads (UK TV series)

ữḛḳṊẴ ẋ,Ẩṙ,ỹḛẪẠứụỿṞṦ,Ṉẍừ,ứ Ị,Ḵ,ṏ ṇỪḎḰṰọửḊ ṾḨḮữẑỶṑỗḮṣṉẃ Ữẩụ,ṓ,ḹẕḪḫỞṿḭ ỒṱṨẁṋṜ ḅẈ ṉ ứṀḱṑỒḵ,ḏ,ḊḖỹẊ Ẻḷổ,ṥ ẔḲẪụḣể Ṱ ḭỏựẶ Ồ Ṩ,ẂḿṡḾồ ỗṗṡịṞẤḵṽẃ ṸḒẄẘ,ủẞẵṦṟầṓế