问题描述
我已经创建了一个状态图,并想删除 x 轴和 y 轴,因为它们没有为该图提供任何值。
map_population_by_county = (
ggplot(map_population_by_county_data)
+ geom_map(aes(fill='Population2018'))
+ geom_label(aes(x = 'Longitude',y = 'Latitude',label='Population2018',size=2))
)
map_population_by_county
我希望添加 + theme(axis.text.x=element_blank())
应该删除轴,并且我应该同样能够修改更多,例如
+ theme(axis.title.x=element_blank(),axis.text.x=element_blank(),axis.ticks.x=element_blank())
但是当我添加 + theme(axis.text.x=element_blank())
时,我收到一个错误
map_population_by_county = (
ggplot(map_population_by_county_data)
+ geom_map(aes(fill='Population2018'))
+ geom_label(aes(x = 'Longitude',size=2))
+ theme(axis.text.x=element_blank())
)
map_population_by_county
File "<ipython-input-23-45e23f30fc6a>",line 5
+ theme(axis.text.x=element_blank())
^
SyntaxError: expression cannot contain assignment,perhaps you meant "=="?
添加双等号“==”似乎不正确,但我尝试了它并收到一个新错误,NameError: name 'axis' is not defined
是否有可能我没有正确导入 plotnine 包?
为了让 geom_point 等工作,我必须在我正在使用的 jupyter 笔记本中专门将这些导入为 from plotnine import ggplot,aes,geom_map,geom_text,geom_label
。使用 import plotnine as p9
或 from plotnine import *
甚至不适用于上面的地图。我试过添加 from plotnine import theme
,但没有用。
解决方法
正如@cookesd 所建议的 - 将 axis.text.x
(R ggplot 公式)替换为 python plotnine 的 axis_text_x
解决了这个问题。
在python中,参数名称需要用_
分隔