I am trying to produce a 2d plot of a sparse array with imshow()
and use plt.text()
to overlay it with text boxes. I came up with an additional option, using plt.scatter()
. In the second case, the colored tiles and the text boxes are too small and can not be zoomed. In the first case, the size of the colored tiles, produced by imshow()
and the text boxes have decoherent size and the plot looks fine only if the zoom function of the dialog window is used. This is illustrated by the code below.
import matplotlib.pyplot as plt
import matplotlib.colors as colors
import numpy as np
#https://matplotlib.org/gallery/images_contours_and_fields/image_annotated_heatmap.html
P=[1, 4, 11, 18, 20, 39, 40, 41, 41, 71, 71, 71, 71, 71, 71, 72, 72, 72, 72, 72, 72, 73, 73, 73, 74, 74, 74, 74, 74, 74, 75, 75, 75, 71]
N=[2, 3, 11, 19, 25, 49, 48, 50, 54, 101, 102, 103, 103, 106, 106, 100, 103, 106, 106, 107, 109, 105, 106, 109, 104, 107, 109, 110, 111, 112, 108, 109, 109, 101]
B=np.random.rand(34)
# crate the array to use with imshow()
A=np.zeros((max(N)+1,max(N)+1))
for i,j,k in zip(N,P,B):
A[i,j]=k
def plot_map(N,P,A):
fig, ax = plt.subplots()
plt.imshow(A,norm=colors.LogNorm(),cmap='jet',origin='lower')
plt.colorbar()
for n,p in zip(N,P):
ax.text(p,n, "\n%s\n%s\n%5.1E"%(p,n,A[n,p]),
ha="center", va="center",
bbox=dict(fc="none",boxstyle = "square"))
plt.tight_layout()
plt.show()
# call the plot function
plot_map(N,P,A)
# attempt tow using plt.scatter()
plt.scatter(N,P,c=B,marker='s',s=70,norm=colors.LogNorm(),cmap='jet')
for n,p in zip(N,P):
plt.text(n,p, "\n%s\n%s"%(p,n), size=3,
va="center", ha="center", multialignment="left",
bbox=dict(fc="none",boxstyle = "square"))
plt.colorbar()
plt.show()
I ideally, I would like to produce something like this
What my plot routines produce does not look so good and both the colored tiles and the annotation boxes are discontinuous.
Therefore, I would appreciate your help.