Issue
Consider that I have a pre-existing figure like:
from matplotlib import pyplot as plt
fig, axes = plt.subplots(nrows=2, ncols=3)
How can I add a 5th subplot that would take both rows in a third column?
I know it's easy to create such a figure from the start using, for example, mosaics like
plt.figure().subplot_mosaic([[1, 2, 3], [4, 5, 3]])
which produces what I want:
But the figure comes from an existing application, and if I were to replicate the same figure manually it'd take a huge amount of code. So really I'd need a way to do it after the first 4 subplots are already built and plotted.
Can this be done? (Preferably with constrained_layout
active, although I'm sure this would be asking too much.)
PS: I tried the solutions here and here, but I couldn't make them work for what I want.
Solution
You can add new axes by firstly adjusting current plots, then putting new axes at a specified position.
import matplotlib.pyplot as plt
fig, axes = plt.subplots(nrows=2, ncols=3)
plt.subplots_adjust(right=0.7 )
ax2 = fig.add_axes((0.75, 0.1, 0.2, 0.8))
plt.show()
Here I set the right border of an existing graph at 0.7 in relative units, then put new axis within a rectangle: (left, bottom, width, height) in relative units. Use ax2.plot() for adding data on new axes. It messes up layout, but I don't know how to counter that. If it's opened in terminal, however, looks ok in fullscreen (picrelated)
Answered By - Ruslan Nakibov
0 comments:
Post a Comment
Note: Only a member of this blog may post a comment.