How to set xlim and ylim for a subplot
Become part of the top 3% of the developers by applying to Toptal https://topt.al/25cXVn
--
Music by Eric Matyas
https://www.soundimage.org
Track title: Quirky Dreamscape Looping
--
Chapters
00:00 Question
00:32 Accepted answer (Score 344)
01:32 Thank you
--
Full question
https://stackoverflow.com/questions/1585...
Accepted answer links:
[plt.subplot]: http://matplotlib.org/api/pyplot_api.htm...
[axes]: http://matplotlib.org/api/axes_api.html
--
Content licensed under CC BY-SA
https://meta.stackexchange.com/help/lice...
--
Tags
#python #matplotlib #plot #subplot
#avk47
--
Music by Eric Matyas
https://www.soundimage.org
Track title: Quirky Dreamscape Looping
--
Chapters
00:00 Question
00:32 Accepted answer (Score 344)
01:32 Thank you
--
Full question
https://stackoverflow.com/questions/1585...
Accepted answer links:
[plt.subplot]: http://matplotlib.org/api/pyplot_api.htm...
[axes]: http://matplotlib.org/api/axes_api.html
--
Content licensed under CC BY-SA
https://meta.stackexchange.com/help/lice...
--
Tags
#python #matplotlib #plot #subplot
#avk47
ACCEPTED ANSWER
Score 360
You should use the OO interface to matplotlib, rather than the state machine interface. Almost all of the plt.* function are thin wrappers that basically do gca().*.
plt.subplot returns an axes object. Once you have a reference to the axes object you can plot directly to it, change its limits, etc.
import matplotlib.pyplot as plt
ax1 = plt.subplot(131)
ax1.scatter([1, 2], [3, 4])
ax1.set_xlim([0, 5])
ax1.set_ylim([0, 5])
ax2 = plt.subplot(132)
ax2.scatter([1, 2],[3, 4])
ax2.set_xlim([0, 5])
ax2.set_ylim([0, 5])
and so on for as many axes as you want.
or better, wrap it all up in a loop:
import matplotlib.pyplot as plt
DATA_x = ([1, 2],
[2, 3],
[3, 4])
DATA_y = DATA_x[::-1]
XLIMS = [[0, 10]] * 3
YLIMS = [[0, 10]] * 3
for j, (x, y, xlim, ylim) in enumerate(zip(DATA_x, DATA_y, XLIMS, YLIMS)):
ax = plt.subplot(1, 3, j + 1)
ax.scatter(x, y)
ax.set_xlim(xlim)
ax.set_ylim(ylim)