如何在 `python` 中使用 `lets-plot` 指定 `scale_color_manual()`。

huangapple go评论67阅读模式
英文:

How to specify `scale_color_manual()` in `python`, using `lets-plot`

问题

import pandas as pd
from lets_plot import *
LetsPlot.setup_html()

data = pd.read_csv('https://raw.githubusercontent.com/JetBrains/lets-plot-docs/master/data/mpg2.csv')

ggplot(data, aes(x="engine horsepower", y="miles per gallon")) + geom_point(aes(color="origin of car"))

In R, using ggplot2, I would manually set the colors by writing:

... + scale_color_manual(values = c("US" = "red", "Asia" = "green", "Europe" = "blue")

How can I do the same in Python with lets-plot?


<details>
<summary>英文:</summary>

My code is:

import pandas as pd
from lets_plot import *
LetsPlot.setup_html()

data = pd.read_csv('https://raw.githubusercontent.com/JetBrains/lets-plot-docs/master/data/mpg2.csv')

ggplot(data, aes(x="engine horsepower", y="miles per gallon")) + geom_point(aes(color="origin of car"))


And the output is:
[![enter image description here](https://i.stack.imgur.com/8D6qN.png)](https://i.stack.imgur.com/8D6qN.png)

In `R`, using `ggplot2`, I would manually set the colors by writing:

... + scale_color_manual(values = c("US" = "red", "Asia" = "green", "Europe" = "blue")


How can I do the same in `Python` with `lets-plot`?

`lets-plot` reference manual for this function doesn&#39;t seem to help: [here](https://lets-plot.org/pages/api/lets_plot.scale_color_manual.html?highlight=scale_color_manual#lets_plot.scale_color_manual)

</details>


# 答案1
**得分**: 2

以下是您要翻译的内容:

基本上,`lets-plot` 中的操作与 R 类似。使用 pandas Series,您可以创建类似 R 中的命名向量:

```python
import pandas as pd
from lets_plot import *
LetsPlot.setup_html()

data = pd.read_csv('https://raw.githubusercontent.com/JetBrains/lets-plot-docs/master/data/mpg2.csv')

ggplot(data, aes(x="engine horsepower", y="miles per gallon")) + \
    geom_point(aes(color="origin of car")) + \
    scale_color_manual(values=pd.Series(["red", "green", "blue"], 
                                      index=['US', 'Asia', 'Europe']))

如何在 `python` 中使用 `lets-plot` 指定 `scale_color_manual()`。

英文:

Basically it's the same in lets-plot. Using a pandas Series you could create a named vector similar to R:

import pandas as pd
from lets_plot import *
LetsPlot.setup_html()

data = pd.read_csv(&#39;https://raw.githubusercontent.com/JetBrains/lets-plot-docs/master/data/mpg2.csv&#39;)

ggplot(data, aes(x=&quot;engine horsepower&quot;, y=&quot;miles per gallon&quot;)) + \
    geom_point(aes(color=&quot;origin of car&quot;)) + \
    scale_color_manual(values = pd.Series([&quot;red&quot;, &quot;green&quot;, &quot;blue&quot;], 
                                          index=[&#39;US&#39;, &#39;Asia&#39;, &#39;Europe&#39;]))

如何在 `python` 中使用 `lets-plot` 指定 `scale_color_manual()`。

huangapple
  • 本文由 发表于 2023年3月21日 02:41:04
  • 转载请务必保留本文链接:https://go.coder-hub.com/75794126-2.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定