问题描述
我想在一个国家的地图(一张图片)上绘制一些散点图.这个想法是要描绘出绘图所在区域的数据可视化.
因此,这就是我如何绘制
我继续绘制散点图,如下所示:
将 numpy 导入为 np导入matplotlib.pyplot作为pltfig=plt.figure(figsize=(10,15))im = plt.imread("usa-states-map.jpg")implot = plt.imshow(im, extent=[0, 200, 0, 150])#左上方的圆圈theta = np.linspace(0,2 * np.pi,50)faux_radius = 15z=np.cos(theta)*faux_radius + 45t=np.sin(theta)*faux_radius + 130plt.plot(z,t)# 中间区域的一个圆圈theta=np.linspace(0,3*np.pi,50)faux_radius = 15z=np.cos(theta)*faux_radius + 100t=np.sin(theta)*faux_radius + 80plt.plot(z,t)# 散点图 1ax1 = plt.subplot(2,2,1)ax1.scatter(x_1_a, y_1_a, 标记="s")ax1.scatter(x_1_b, y_1_b, 标记=o")#散点图2ax1 = plt.subplot(2,2,2)ax1.scatter(x_2_a, y_2_a, 标记="s")ax1.scatter(x_2_a, y_2_b, 标记=o")
但它产生的输出不显示背景图像,只绘制散点图:
[]
我什至尝试使用 zorder
它应该告诉 matplotlib 哪个图应该在顶部,哪个在底部,但无济于事 - 它产生与上面相同的输出:
implot = plt.imshow(im, extent=[0, 200, 0, 150], zorder=1).........#散点图1ax1 = plt.subplot(2,2,1)ax1.scatter(x_1_a,y_1_a,marker ="s",zorder = 2)ax1.scatter(x_1_b,y_1_b,marker ="o",zorder = 2)# 散点图 2ax1 = plt.subplot(2,2,2)ax1.scatter(x_2_a,y_2_a,marker ="s",zorder = 3)ax1.scatter(x_2_a, y_2_b, 标记="o", zorder=3)
我该如何解决这个问题以获得想要的结果?实际上,我什至不需要在地图上出现 2 个圆圈 - 这些只是为了说明我想绘制 2 个散点图的位置.谢谢.
我能够使用评论中的 plt.axes
建议解决问题:
from mpl_toolkits.axes_grid.inset_locator import inset_axes导入matplotlib.pyplot作为plt将numpy导入为np无花果= plt.figure(figsize =(10,15),facecolor ='white')ax = fig.add_axes([0,0,1,1])ax.axis('off')im = plt.imread("usa-states-map.jpg")implot = plt.imshow(im)plt.xticks([])plt.yticks([])# 这是左上角区域主轴上的插入轴a = plt.axes([.2, .6, .2, .1], facecolor='w')plt.scatter(x_1_a, y_1_a, 标记=s")plt.scatter(x_1_b, y_1_b, 标记=o")plt.legend(['%.2f %%'%(100 * len(x_1_a)/(len(x_1_a)+ len(y_1_a))),'%.2f %%'%(100 * len(y_1_a)/(len(x_1_a)+ len(y_1_a)))],loc ='右上角');#这是中间区域主轴上方的插入轴a = plt.axes([.45, .45, .2, .1], facecolor='w')plt.scatter(x_2_a, y_2_a, 标记=s")plt.scatter(x_2_b, y_2_b, 标记=o")plt.legend(['%.2f %%'%(100 * len(x_2_b)/(len(x_2_b)+ len(y_2_b))),'%.2f %%'%(100 * len(y_2_b)/(len(x_2_b)+len(y_2_b)))], loc='右上角');plt.show()
I want to plot some scatter plots over the map of a country (an image). The idea is to depict the data visualization of the area at which the plot is plotted.
So, this is how I plot the image of the map of USA, where the circles I have drawn towards the top left and the middle are where I would like to display my scatter plots:
import numpy as np
import matplotlib.pyplot as plt
fig=plt.figure(figsize=(10,15))
im = plt.imread("usa-states-map.jpg")
implot = plt.imshow(im, extent=[0, 200, 0, 150])
# A circle in the upper left region
theta=np.linspace(0,2*np.pi,50)
faux_radius = 15
z=np.cos(theta)*faux_radius + 45
t=np.sin(theta)*faux_radius + 130
plt.plot(z,t)
# A circle in the middle region
theta=np.linspace(0,3*np.pi,50)
faux_radius = 15
z=np.cos(theta)*faux_radius + 100
t=np.sin(theta)*faux_radius + 80
plt.plot(z,t)
This plots the image like so:
I proceed to plot the scatter plots like so:
import numpy as np
import matplotlib.pyplot as plt
fig=plt.figure(figsize=(10,15))
im = plt.imread("usa-states-map.jpg")
implot = plt.imshow(im, extent=[0, 200, 0, 150])
# A circle in the upper left region
theta=np.linspace(0,2*np.pi,50)
faux_radius = 15
z=np.cos(theta)*faux_radius + 45
t=np.sin(theta)*faux_radius + 130
plt.plot(z,t)
# A circle in the middle region
theta=np.linspace(0,3*np.pi,50)
faux_radius = 15
z=np.cos(theta)*faux_radius + 100
t=np.sin(theta)*faux_radius + 80
plt.plot(z,t)
# Scatter plot 1
ax1 = plt.subplot(2,2,1)
ax1.scatter(x_1_a, y_1_a, marker="s")
ax1.scatter(x_1_b, y_1_b, marker="o")
# Scatter plot 2
ax1 = plt.subplot(2,2,2)
ax1.scatter(x_2_a, y_2_a, marker="s")
ax1.scatter(x_2_a, y_2_b, marker="o")
But the output it produces does not display the background image, and only plots the scatter plots:
[]
I even tried using zorder
which is supposed to tell matplotlib which plot should come on top and which on bottom, but to no avail - it produces the same output as above:
implot = plt.imshow(im, extent=[0, 200, 0, 150], zorder=1)
...
...
...
# Scatter plot 1
ax1 = plt.subplot(2,2,1)
ax1.scatter(x_1_a, y_1_a, marker="s", zorder=2)
ax1.scatter(x_1_b, y_1_b, marker="o", zorder=2)
# Scatter plot 2
ax1 = plt.subplot(2,2,2)
ax1.scatter(x_2_a, y_2_a, marker="s", zorder=3)
ax1.scatter(x_2_a, y_2_b, marker="o", zorder=3)
How do I fix this to get the desired result? I don't even need the 2 circles to be present on the map actually - those were just to illustrate where I would like to plot the 2 scatter plots. Thanks.
I was able to solve the problem using the plt.axes
suggestion in the comments:
from mpl_toolkits.axes_grid.inset_locator import inset_axes
import matplotlib.pyplot as plt
import numpy as np
fig = plt.figure(figsize=(10, 15),facecolor='white')
ax = fig.add_axes([0, 0, 1, 1])
ax.axis('off')
im = plt.imread("usa-states-map.jpg")
implot = plt.imshow(im)
plt.xticks([])
plt.yticks([])
# this is an inset axes over the main axes for the top left region
a = plt.axes([.2, .6, .2, .1], facecolor='w')
plt.scatter(x_1_a, y_1_a, marker="s")
plt.scatter(x_1_b, y_1_b, marker="o")
plt.legend(['%.2f%%' %(100*len(x_1_a)/(len(x_1_a)+len(y_1_a))), '%.2f%%' %(100*len(y_1_a)/(len(x_1_a)+len(y_1_a)))], loc='upper right');
# this is an inset axes over the main axes for the middle region
a = plt.axes([.45, .45, .2, .1], facecolor='w')
plt.scatter(x_2_a, y_2_a, marker="s")
plt.scatter(x_2_b, y_2_b, marker="o")
plt.legend(['%.2f%%' %(100*len(x_2_b)/(len(x_2_b)+len(y_2_b))), '%.2f%%' %(100*len(y_2_b)/(len(x_2_b)+len(y_2_b)))], loc='upper right');
plt.show()
这篇关于如何使用matplotlib在背景图像上绘制多个子图?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!