Geopandas 无法同时删除轴和设置标题

问题描述 投票:0回答:1

我有一个 geopandas 数据框,我正在绘制 chromopheth 地图,没有问题。然而,当我想自定义地图时,通过删除轴并设置标题,我似乎只能做其中之一。

ax = world.plot(column = 'Inflation, consumer prices (annual %)',figsize = (15, 12), 
                      legend = True, legend_kwds={'shrink': 0.3},
                      missing_kwds = {'color' : 'lightgrey'}).set_title('title')

工作并设置标题。如果我尝试删除轴;

ax = world.plot(column = 'Inflation, consumer prices (annual %)',figsize = (15, 12), 
                      legend = True, legend_kwds={'shrink': 0.3},
                      missing_kwds = {'color' : 'lightgrey'}).set_axis_off()

也有效。但是如果我尝试,

ax = world.plot(column = 'Inflation, consumer prices (annual %)',figsize = (15, 12), 
                      legend = True, legend_kwds={'shrink': 0.3},
                      missing_kwds = {'color' : 'lightgrey'}).set_title('title').set_axis_off()

我收到错误消息,告诉我“AttributeError:'Text'对象没有属性'set_axis_off'”。如果我交换它们,请先关闭轴然后设置标题;

ax = world.plot(column = 'Inflation, consumer prices (annual %)',figsize = (15, 12), 
                      legend = True, legend_kwds={'shrink': 0.3},
                      missing_kwds = {'color' : 'lightgrey'}).set_axis_off().set_title('title')

它告诉我“AttributeError:'NoneType'对象没有属性'set_title'”。

然后我尝试先做一个,然后通过;

ax = world.plot(column = 'Inflation, consumer prices (annual %)',figsize = (15, 12), 
                      legend = True, legend_kwds={'shrink': 0.3},
                      missing_kwds = {'color' : 'lightgrey'}).set_axis_off()
ax.set_title('title')

但这引发了同样的错误。

我猜测这与我的变量是什么有关,如果我在一个增强之后加上另一个,它会如何认为我正在增强前一个参数?但我对 geopandas 相当陌生,所以任何帮助将不胜感激!

python pandas matplotlib geopandas
1个回答
3
投票

不要将变量指定为 set_title 和 set_axis_off,因为它不返回轴。你可以这样做。

ax = world.plot(column = 'Inflation, consumer prices (annual %)',figsize = (15, 12), 
                      legend = True, legend_kwds={'shrink': 0.3},
                      missing_kwds = {'color' : 'lightgrey'})
ax.set_axis_off()
ax.set_title('title')
plt.show()

在这里看到.set_title()返回一个文本。

© www.soinside.com 2019 - 2024. All rights reserved.