【发布时间】:2023-04-06 12:33:01
【问题描述】:
你好!
我正在尝试在 Matplotlib 中创建一个由叶绿素图和条形图组成的图形。为了实现这一点,我将 Geopandas 库与 Pandas 和 Matplotlib 一起使用。我遇到了一个有趣的问题,我在互联网上找不到任何答案。问题来了:
This link leads to an image that replicates the problem.
如上图所示,顶部的地图(由 Geopandas 生成)与底部条形图的宽度不同。图的左侧和右侧有太多空白。我想摆脱这个空白并使地图水平适合分配给它的空间。我还在下面为希望重新创建它的人留下了一个代码示例:
fig = plt.figure(figsize = (25.60,14.40)) #Here, i am setting the overall figure size
ax_1 = fig.add_subplot(2,1,1) #This will be the map
istanbul_districts.plot(ax = ax_1,
edgecolor = "black",
alpha = 1,
color = "Red") #Istanbul_districts is a GeoDataFrame object.
ax_2 = fig.add_subplot(2,1,2)
labels = list(health.loc[:,"district_eng"].value_counts().sort_values(ascending = False).index)
from numpy import arange
bar_positions = arange(len(labels)) + 1
bar_heights = h_inst_per_district_eng.loc[:,"health_count"].values.astype(int)
ax_2.bar(bar_positions,bar_heights,
width = 0.7,
align = "center",
color = "blue") #This is a generic barplot from Matplotlib
我要留下第二张图片,显示上面代码 sn-p 的最终结果:
This link also leads to an image that replicates the problem.
从上面可以清楚地看出,两个子图的坐标轴并没有在同一个位置开始和结束。也许这可能是问题所在?怎样才能使它们的大小相同?
感谢所有提前回答的人!
【问题讨论】:
-
如果图形大小不同,例如
fig = plt.figure(figsize = (10,25)),您会得到什么。在这种情况下,高度 > 宽度。 -
您好@swatchai,将图形调整为 figsize(10,25) 以使条形图真正绘制为代价解决了空白问题。但是,您的建议给了我一些想法,在 (19.20,19.20) 修复 figsize 解决了这个问题。
标签: python matplotlib gis geopandas