Issue
I have a master plot (imshow heatmap) with a grid. Inside each of the grids, i want to plot a histogram (without axis). So in summary, i want to have my master heat map with another plot in each of the grid spaces.
I have removed some of the complexities from above and simplified it. 1 - simple plot instad of imshow 2 - instead of histogram in ever grid, i just want it within a specific box
from matplotlib import pyplot as plt
import numpy as np
fig, ax1 = plt.subplots()
ax1.plot([0, 1, 2, 3, 4, 5, 6],[1, 4, 6, 2, 1, 5, 2], c="red", lw=4, label="outer plot")
# assume that i want my plot to lie between 3 and 4 for both the x and y axis.
ax1.axhline(3,0.5,0.65)
ax1.axhline(4,0.5,0.65)
ax1.axvline(3,0.41,0.59)
ax1.axvline(4,0.41,0.59)
ax2 = fig.add_axes([0.6, .25, 1/6, 1/5])
ax2.hist(np.random.normal(size=100), bins=20)
ax2.axis('off')
plt.show()
from matplotlib import pyplot as plt
import numpy as np
plt.plot([0, 1, 2, 3, 4, 5, 6],[1, 4, 6, 2, 1, 5, 2], c="red", lw=4, label="outer plot")
plt.hlines(3,3,4)
plt.hlines(4,3,4)
plt.vlines(3,3,4)
plt.vlines(4,3,4)
ax2 = plt.gca().inset_axes([3,3,1,1], transform=ax1.transData)
ax2.hist(np.random.normal(size=100), bins=20)
ax1.legend(loc='upper left')
ax2.axis('off')
plt.show()
I was able to do something close but add_axes takes the location inputs as a % and i dont know how to account for the space of padding and axis labels. Similarly, using inset axis did not align my plot. Either way, I was not able to make the plot line up nicely within my box. I will be repeating this for multiple grids, so this part has to be programatic, not manual.
Solution
It's possible that you have things a bit confused between your two attempts and between the explicit and implicit APIs: you are trying to use ax1.transData
in your inset_axes
code, but you never defined ax1
for that figure. I tidied up your code like this
from matplotlib import pyplot as plt
import numpy as np
fig, ax1 = plt.subplots()
ax1.plot([0, 1, 2, 3, 4, 5, 6],[1, 4, 6, 2, 1, 5, 2], c="red", lw=4, label="outer plot")
# assume that i want my plot to lie between 3 and 4 for both the x and y axis.
ax1.axhline(3,0.5,0.65)
ax1.axhline(4,0.5,0.65)
ax1.axvline(3,0.41,0.59)
ax1.axvline(4,0.41,0.59)
ax2 = ax1.inset_axes([3,3,1,1], transform=ax1.transData)
ax2.hist(np.random.normal(size=100), bins=20)
ax1.legend(loc='upper left')
ax2.axis('off')
plt.show()
And got this:
Answered By - RuthC
0 comments:
Post a Comment
Note: Only a member of this blog may post a comment.