python - 在底图上绘制子图
问题描述
我有一张英国地图和 121 个地点,每个地点都有 3 个值。我想在 121 个位置中的每一个位置绘制三个值的小条形图。
目前,这些值被绘制为markersize
属性,如下所示:
Trypophobic_Plot
使用以下代码制作:(大量简化)
fig, ax = plt.subplots()
ax = Basemap(llcrnrlat= 46.351611,
llcrnrlon= -11.543011,
urcrnrlat= 60.417133,
urcrnrlon= 6.2743413,
resolution='i', projection='tmerc', lon_0=-2, lat_0=49)
#Draw Coastlines, colours etc.
(removed because not important to this example)
################################################
# Plot Network edge map
(not important to this example)
################################################
# Plot SYNOP Station Points
lats = [59.5329945921, 58.9499976059, 57.3669959942...]
lons = [-1.63299557989, -2.90000295719, -7.40000487601...]
for method in range (0,3):
for i in range(0, len(lons)):
x,y = ax(lons[i], lats[i])
ax.plot(x, y, 'o', color=(1,1,1,0), \
markersize= ..., \
markeredgecolor=..., \
markeredgewidth=7)
################################################
# Other plot features
plt.text(10000, 10000,'Max: = ' ... + \
'\nMin: = ' ..., \
fontsize=32, zorder=75.)
plt.title('site_scores' , fontsize=30)
plt.show()
但是,我想要一个条形图。我解决这个问题的方法是为 121 个位置中的每一个创建一个子图。这可能效率低下,因此如果您认为有更好的方法,请提出另一种方法。
我尝试过的事情:
我开始研究在纬度/经度和图表的实轴之间进行转换,这有点令人困惑。有display
、axes
、data
和Figure
。我无法在这里应用变换操作数:https ://matplotlib.org/users/transforms_tutorial.html到ax
坐标上。您可以在上面的代码中看到我是如何制作圆圈的,但我不知道将它们切换到什么坐标系。
然后我想我会尝试添加一个我通常做的轴操作系统,看看它出现在哪里。像这样:
ax3 = fig.add_axes([0.5,0.5, 0.2, 0.2])
但这会导致绘图大小出现错误:
ValueError: Image size of 5690009x6001228 pixels is too large. It must be less than 2^16 in each direction.
这就是我现在要做的。我想要一个小条图来显示这些圆圈的大小。
解决方案
下面的代码用于inset_axes
在地图上的每个位置绘制每个条形图。如果将figure
' 的大小设置得更大,则条形图应该不会那么杂乱。
from mpl_toolkits.basemap import Basemap
from mpl_toolkits.axes_grid1.inset_locator import inset_axes
import matplotlib.pyplot as plt
import numpy as np
import matplotlib.patches as mpatches
# prep values for map extents and more
llcrnrlat = 46.351611
llcrnrlon = -11.543011
urcrnrlat = 60.417133
urcrnrlon = 6.2743413
mid_lon = (urcrnrlon+llcrnrlon)/2.0
hr_lon = (urcrnrlon-llcrnrlon)/2.0
mid_lat = (urcrnrlat+llcrnrlat)/2.0
hr_lat = (urcrnrlat-llcrnrlat)/2.0
# function to create inset axes and plot bar chart on it
# this is good for 3 items bar chart
def build_bar(mapx, mapy, ax, width, xvals=['a','b','c'], yvals=[1,4,2], fcolors=['r','y','b']):
ax_h = inset_axes(ax, width=width, \
height=width, \
loc=3, \
bbox_to_anchor=(mapx, mapy), \
bbox_transform=ax.transData, \
borderpad=0, \
axes_kwargs={'alpha': 0.35, 'visible': True})
for x,y,c in zip(xvals, yvals, fcolors):
ax_h.bar(x, y, label=str(x), fc=c)
#ax.xticks(range(len(xvals)), xvals, fontsize=10, rotation=30)
ax_h.axis('off')
return ax_h
fig, ax = plt.subplots(figsize=(10, 9)) # bigger is better
bm = Basemap(llcrnrlat= llcrnrlat,
llcrnrlon= llcrnrlon,
urcrnrlat= urcrnrlat,
urcrnrlon= urcrnrlon,
ax = ax,
resolution='i', projection='tmerc', lon_0=-2, lat_0=49)
bm.fillcontinents(color='lightyellow', zorder=0)
bm.drawcoastlines(color='gray', linewidth=0.3, zorder=2)
plt.title('site_scores', fontsize=20)
# ======================
# make-up some locations
# ----------------------
n = 50 # you may use 121 here
lon1s = mid_lon + hr_lon*(np.random.random_sample(n)-0.5)
lat1s = mid_lat + hr_lat*(np.random.random_sample(n)-0.5)
# make-up list of 3-values data for the locations above
# -----------------------------------------------------
bar_data = np.random.randint(1,5,[n,3]) # list of 3 items lists
# create a barchart at each location in (lon1s,lat1s)
# ---------------------------------------------------
bar_width = 0.1 # inch
colors = ['r','y','b']
for ix, lon1, lat1 in zip(list(range(n)), lon1s, lat1s):
x1, y1 = bm(lon1, lat1) # get data coordinates for plotting
bax = build_bar(x1, y1, ax, 0.2, xvals=['a','b','c'], \
yvals=bar_data[ix], \
fcolors=colors)
# create legend (of the 3 classes)
patch0 = mpatches.Patch(color=colors[0], label='The 0-data')
patch1 = mpatches.Patch(color=colors[1], label='The 1-data')
patch2 = mpatches.Patch(color=colors[2], label='The 2-data')
ax.legend(handles=[patch0,patch1,patch2], loc=1)
plt.show()
情节将与此类似:
推荐阅读
- cordova - Cordova:媒体插件录制音频空文件
- sql-server - 获取同一个表中通过外键连接的所有行
- wordpress - 统计 WordPress 网站的访问次数
- python - 如何覆盖列表并将列表附加到字典
- javascript - material-ui-dropzone,DropzoneAreaBase 不显示添加的文件预览
- r - 从 Shiny 应用程序生成可下载的报告
- javascript - JavaScript:根据对象属性中的空间拆分数组中的对象
- sql - 加密 SQL Server 中视图列(非表)中的数据
- python - 有人知道如何一次下载一个文件吗
- powershell - 多个文件名称中的Powershell偏移数,由常量表示