剧情不添加图例 [英] plotnine doesn't add legend

查看:114
本文介绍了剧情不添加图例的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用plotnine在同一图中绘制两个图形. 一个图形使用您将在下面看到的数据框中的"b"值,而另一个图形使用"c"中的值.

I'm using plotnine to plot two graphs in the same plot. one graph uses the 'b' values from the dataframe you'll see below, and another one uses the values from 'c'.

我需要做的是显示一个简单的图例图例,在其中看到带有相应颜色的"c"和"b".

All I need is to show a simple legend legend where I see 'c' and 'b' with their according color.

def plot_log_detected():
    df = DataFrame({'x': [1, 2, 3, 4, 5],
                    'b': >>>SOME VALUES DOESNT MATTER<<<,
                    'c': >>>SOME VALUES DOESNT MATTER<<<
                   })
    return ggplot(aes(x='x', y='b'), data=df) + geom_point(size=1) +\
           geom_line(aes(y='b'), color='black') + \
           geom_line(aes(y='c'), color='blue') +  \
           ggtitle("TITLE") + \
           labs(y="Y AXIS", x="X AXIS")

推荐答案

如果您在R中使用ggplot2也不会显示图例:仅当您指定color=时才会显示颜色图例在geom的美学范围内. pythonpythonggplot中的修复"是相同的.您需要整理数据,以便遵循整洁的数据原则.在这种情况下,df$bdf$c列分别包含两条信息:(1)"y"的 value 和(2)"y"的 type .您应该相应地重组数据,以使列名变为:xtype_of_yvalue_of_y.

This won't show a legend if you use ggplot2 in R either: the legend for color will only be represented when you specify color= within an aesthetic for a geom. The "fix" is the same in both python or ggplot for r. You need to organize your data so that you follow tidy data principles. In this case, df$b and df$c columns each contain two pieces of information: (1) value of "y" and (2) type of "y". You should reorganize the data accordingly so that your column names become: x, type_of_y, and value_of_y.

我将通过填写您所介绍的数据集进行说明,然后说明如何将其更改为整齐的格式,然后如何(适当地)应用代码来代表我希望的绘图.

I'll explain by filling in a dataset like you presented, then indicate how we can change it to a tidy format, then how you can (properly) apply the code to represent a plot like I believe you want to to.

基础

这是一个数据集和一个与您的情节类似的情节(同样在r中...所以我希望您可以将其翻译为python):

Here's a dataset and a plot like your plot (again, it's in r... So I hope you can translate into python):

df <- data.frame(
    x=c(1:5), b=c(10, 12, 14, 9, 8), c=c(9, 11, 11, 12, 14))

ggplot(df, aes(x=x)) +
    geom_line(aes(y=b), color='red') +
    geom_line(aes(y=c), color='blue')

没有图例,但是颜色在那里,我们将绘制您期望的图.这里的问题是,当在aes()调用中指定颜色时,ggplot会绘制图例.为了清楚地看到这一点,让我们做同样的图,但是将color=...移到:

No legend, but the colors are there and we plot what you would expect. The problem here is that ggplot draws a legend when you specify color in the aes() call. To see this clearly, let's just do the same plot, but move the color=... inside aes():

ggplot(df, aes(x=x)) +
    geom_line(aes(y=b, color='red')) +
    geom_line(aes(y=c, color='blue'))

好吧...等等.什么?它现在有一个图例(因为我们将color aes()放在了里面),但是实际上颜色是按顺序颠倒的,并且...您会注意到颜色不是红色和蓝色,而是默认的ggplot2带红色"和蓝绿色"颜色.实际上,发生的事情是,我们仅在第一个geom_line调用中指定了绘制正确的数据集,但仅将数据命名为红色".同样,我们将另一个数据集标题"为蓝色". ggplot根据默认调色板决定使用哪种颜色.

Ok that's... wait. What? It has a legend now (because we put color inside aes()), but the colors are actually reversed in order and... you'll notice the colors are not red and blue, but the default "reddish" and "teal" colors of ggplot2. Actually, what happened is that we only specified that in the first geom_line call, we plotted the correct dataset, but we only "titled" the data as "red". Likewise, we "titled" the other dataset "blue". ggplot decided what colors to use based on the default palette.

获取没有整洁数据的图例

如果您不想弄乱数据,实际上有一种方法可以做到这一点,并且可能会得到您可能满意的输出.我们只需在color=中指定要调用该系列的名称.

If you don't want to mess with your data, there is actually a way to do this and probably get an output you might be satisfied with. We just have to indicate in color= the name you want to call that series.

ggplot(df, aes(x=x)) +
    geom_line(aes(y=b, color='b')) +
    geom_line(aes(y=c, color='c'))

仅添加另一个color='blue'以在aes()的内部和外部获得外部蓝色的颜色怎么办?好吧...那是行不通的.例如,如果执行此操作,则结果与显示的原始图相同(没有图例,但颜色值正确),因为aes()在每次geom_line调用中均被有效覆盖:

What about just adding another color='blue' to get a "blue" color outside the aes() as well as inside? Well... that doesn't work. If you do this, for example, the result is identical to the original plot shown (with no legend, but correct color values), since the aes() is effectively overwritten in each geom_line call:

# this doesn't work to keep legend and desired color, the second
# color outside aes() overwrites the one inside aes()
ggplot(df, aes(x=x)) +
    geom_line(aes(y=b, color='b'), color='red') +
    geom_line(aes(y=c, color='c'), color='blue')

整洁的数据方式(正确"方式)

尽管上述方法有效,但它违反了Tidy Data的一般原则以及如何组织您的数据,从而使您可以轻松地以任何方式进行分析.相信我:毫无疑问,这是处理任何数据集以实现多功能分析的最佳实践,并且几乎总是值得以这种方式来组织数据.

While the above method works, it goes against the general principles of Tidy Data and how to organize you data so that it's easy to analyze... in ANY WAY you want to. Trust me: it's definitely the best practice moving forward for working with any dataset for versatility of analysis, and almost always worth the effort to organize your data in that way.

ggplot 想要,您可以将aes()参数指定为数据集中的.这意味着我们应该使每一列都在您的数据集中达到特定目的,例如:

ggplot wants you to specify aes() parameters as columns in your dataset. That means we should make each column serve a specific purpose in your dataset as such:

  • x::这与原始数据集中的x相同.它仅代表x轴值

  • x: This is the same x in the original dataset. It represents only the x-axis value

type_of_y::此列包含值"b"或"c",指示该值应来自哪个数据系列.

type_of_y: this column contains a value of either 'b' or 'c', indicating to which data series the values should be from.

value_of_y::此列包含您将在y上绘制的值.

value_of_y: this column contains the value you would plot on y.

使用dplyr,我们可以非常简单地以这种方式重组数据:

Using dplyr, we can reorganize the data in this way pretty simply:

df <- df %>% gather('type_of_y', 'value_of_y', -x)

给你

   x type_of_y value_of_y
1  1         b         10
2  2         b         12
3  3         b         14
4  4         b          9
5  5         b          8
6  1         c          9
7  2         c         11
8  3         c         11
9  4         c         12
10 5         c         14

然后,您仅需使用一次geom_line调用即可进行绘制,并将color美学效果应用于type_of_y.像这样:

Then you plot accordingly, using only one geom_line call and apply the color aesthetic to type_of_y. Something like this:

ggplot(df, aes(x=x, y=value_of_y)) +
    geom_line(aes(color=type_of_y))

这样,您只需要指定一个geom_line调用即可.在这里看起来可能并没有太大的区别,但是如果原始数据集中有多个列怎么办?以具有"x"的情况为例,然后具有"a","b","c" ..."z"的y值!您必须在对geom_line的单独调用中指定所有这些行!在上述情况下,无论您有多少个不同的y值列……您只有相同的两行代码,并且只有一个对geom_line的调用.有道理?欲了解更多信息,我建议从上面的链接.另外,这篇文章是一本好书.

In this way, you only have to specify one geom_line call. Might not seem too different here, but what if you had multiple columns in your original dataset? Take the case, for example, of having "x", then y values for "a", "b", "c"... "z"! You would have to specify all those lines in separate calls to geom_line! In the case above, no matter how many different y value columns you had... you only have the same two lines of code and only one call to geom_line. Make sense? For more information, I would suggest the link from above. Also, this article is a great read.

然后可以通过添加scale_color_manual并以这种方式指定颜色(还有其他几种方式)来分配特定的颜色-但是,如果您在那里需要帮助,我会在一个单独的问题中提问.另外...不确定python的代码有何不同.同样,您可以通过labs(color="your new legend title") ...更改图例的标题,以及其他主题更改.

You can then assign specific colors by adding scale_color_manual and specifying the colors that way (there's a few other ways too) - but if you need assistance there, I would ask in a separate question. Also... not sure how the code differs for python. Similarly, you can change title of legend via labs(color="your new legend title")... among other theme changes.

我知道它与python中的代码并不完全相同,但这足以让您了解我们在此处如何类似地进行操作.

I know it is not quite the same code in python, but that should be enough for you to figure our how to do it similarly there.

这篇关于剧情不添加图例的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆