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
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)
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.
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