I'm trying to plot a circle that is enclosed by a matrix cell. Easy enough, right? Everything looks great when I run the code for a square or lower-dimensional matrix:
import matplotlib.pyplot as plt
from matplotlib.patches import Circle
import numpy as np
fig, ax = plt.subplots( )
mat = np.random.random((5,5))
ax.matshow(mat)
ax.add_patch( Circle( (2,2) , .5, color='white') )
print( ax.patches[0].get_width() )
plt.show()
which correctly returns the image:
Note how the width of each cell in data coordinates is equal to 1. This is always true for ax.matshow()
. So it makes sense that my circle of diameter 1 seems to perfectly fit within the cell.
However, if I change the number of columns in the matrix to 50 and execute the code:
import matplotlib.pyplot as plt
from matplotlib.patches import Circle
import numpy as np
fig, ax = plt.subplots( dpi=500 )
mat = np.random.random((5,50))
ax.matshow(mat)
ax.add_patch( Circle( (2,2) , .5, color='white') )
print( ax.patches[0].get_width() )
plt.show()
then I get the following plot:
Note that, even though the width of each cell is still equal to 1 in data coordinates, the Patch
object that is plotted (the circle of diameter 1) is clearly bigger than the cell it is supposed to fit within. I can't see any reason why this would happen.