[英]How to plot multiple map of geopandas dataframe?
I have a list of geometry features which I want to visualize side by side as subplots.我有一个几何特征列表,我想将它们并排显示为子图。 When I type:当我输入:
for i in iaq:
fig, ax = plt.subplots(figsize=(8,5))
df_g2[df_g2['aq_date'] == i].plot(column='zone_id', cmap='Greens', ax=ax, legend=True)
ax.set_title('Analysis :'+ str(i))
plt.show()
A list of 40 maps appear one after another in a list. 40 张地图的列表一个接一个地出现在一个列表中。 But I want to arrange them in a 5*8 row-column arrangement.但我想将它们排列成 5*8 的行列排列。 When I try to give a size of the arrangement like:当我尝试给出这样的安排大小时:
fig, ax = plt.subplots(nrows=8, ncols=5)
fig.set_size_inches(6,4)
for i in iaq:
df_g2[df_g2['aq_date'] == i].plot(column='zone_id', cmap='Greens', ax=ax, legend=True)
ax.set_title('Analysis :'+ str(i))
plt.show()
I get the error message:我收到错误消息:
Please help.请帮忙。
I solved the issue with the official reference .我用官方参考解决了这个问题。 Without the trim_axs()
function here, 'numpy.ndrray' occurs.如果这里没有trim_axs()
函数,就会出现“numpy.ndrray”。
import numpy as np
import matplotlib.pyplot as plt
figsize = (9, 9)
cols = 5
rows = 8
x = np.linspace(0, 10, 500)
y = np.sin(x)
def trim_axs(axs, N):
"""
Reduce *axs* to *N* Axes. All further Axes are removed from the figure.
"""
axs = axs.flat
for ax in axs[N:]:
ax.remove()
return axs[:N]
axs = plt.figure(figsize=figsize, constrained_layout=True).subplots(rows, cols)
axs = trim_axs(axs, cols*rows)
for ax, i in zip(axs, range(1,(cols*rows)+1)):
ax.set_title('Analysis :'+ str(i))
ax.plot(x, y, 'o', ls='-', ms=4)
Since I don't have access to your dataframe, I will use the builtin naturalearth_lowres
to plot an array of selected countries.由于我无权访问您的数据框,因此我将使用内置的naturalearth_lowres
来绘制选定国家/地区的数组。 Read the comments within the code for clarification of important steps.阅读代码中的注释以了解重要步骤。
import geopandas as gpd
import matplotlib.pyplot as plt
# for demo purposes, use the builtin data
world = gpd.read_file(gpd.datasets.get_path('naturalearth_lowres'))
# set number of subplots' (columns, rows) enough to use
cols, rows = 2,3 #num of subplots <= (cols x rows)
# create figure with array of axes
fig, axs = plt.subplots(nrows=rows, ncols=cols)
fig.set_size_inches(6, 10) #set it big enough for all subplots
# select some countries to plot
# number of them is intended to be less than (cols x rows)
# the remaining subplots will be discarded
iaq = ['IND', 'TZA', 'CAN', 'THA', 'BRN']
count = 0
for irow in range(axs.shape[0]):
for icol in range(axs.shape[1]):
#print(icol, irow)
if count<len(iaq):
# plot that country on current axes
world[ world['iso_a3'] == iaq[count] ].plot(ax=axs[irow][icol])
axs[irow][icol].set_title('world:iso_a3: '+iaq[count])
count +=1
else:
# hide extra axes
axs[irow][icol].set_visible(False)
plt.show()
The resulting plot:结果图:
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.