更改图例上的名字

发布于 2025-01-21 18:54:05 字数 441 浏览 4 评论 0原文

我正在尝试更改这个情节传说中的名字。在大熊猫中使用此代码,我会收到此错误消息(下图)。该代码的第一部分有效,但与它不喜欢的组合部分有关。

plot3 + labs(colour = 'Location area', lables = c('Urban', 'Rural', 'No data'))

错误信息;

NameError: name 'c' is not defined

我正在寻找的是这个情节具有“位置区域”的标题,而1,2和3的标题则被更名为“ Urban”,“乡村”,而没有数据”。 是否需要更多数据/信息

让我知道此

I am trying to change the names on my legend of this plot. Using this code in pandas i get this error message (below). The first part of this code works but its something to do with the combine part that it doesnt like.

plot3 + labs(colour = 'Location area', lables = c('Urban', 'Rural', 'No data'))

error message;

NameError: name 'c' is not defined

What i am looking for is this plot to have the lengend title of 'Location area' and the 1,2 and 3 to be renamed 'Urban', 'Rural' and No data'. Let me know if more data/info is needed for this

enter image description here

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

以可爱出名 2025-01-28 18:54:05

传奇密钥标签是通过标签 scale> scale的参数设置的。但是主要问题是您使用c()是R代码。当您使用plotnine和python时,您必须使用方括号:

使用基于mtcars的简单示例:

from plotnine import ggplot, geom_point, aes, stat_smooth, facet_wrap, scale_color_discrete, labs
from plotnine.data import mtcars

(ggplot(mtcars, aes('wt', 'mpg', color='factor(gear)'))
 + geom_point()
 + stat_smooth(method='lm')
 + scale_color_discrete(labels = ['Urban', 'Rural', 'No data'])
 + facet_wrap('~gear')
 + labs(color = 'Location Area'))

”

The legend key labels are set via the labels argument of the scale. But the main issue is that you use c() which is R code. As you are using plotnine and Python you have to use square brackets:

Using a simple example based on mtcars:

from plotnine import ggplot, geom_point, aes, stat_smooth, facet_wrap, scale_color_discrete, labs
from plotnine.data import mtcars

(ggplot(mtcars, aes('wt', 'mpg', color='factor(gear)'))
 + geom_point()
 + stat_smooth(method='lm')
 + scale_color_discrete(labels = ['Urban', 'Rural', 'No data'])
 + facet_wrap('~gear')
 + labs(color = 'Location Area'))

enter image description here

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文