【问题标题】:Issue using qualitative brewer palettes in plotnine在 plotnine 中使用定性 brewer 调色板的问题
【发布时间】:2018-03-28 10:06:11
【问题描述】:

我想通过 plotnine 使用 brewer 定性调色板,但我收到错误:

ValueError: Invalid color map name 'Set1' for type 'Sequential'.
Valid names are: ['Blues', 'BuGn', 'BuPu', 'GnBu', 'Greens', 'Greys', 'OrRd', 'Oranges', 'PuBu', 'PuBuGn', 'PuRd', 'Purples', 'RdPu', 'Reds', 'YlGn', 'YlGnBu', 'YlOrBr', 'YlOrRd']

可重现的例子

 from plotnine import *
 import pandas as pd

 df = pd.DataFrame({'a': [0,1,2,3,4], 'b': [0,-2,10,6,8], 'c': ['a', 'b',  'a', 'a', 'b']})

 (ggplot(df, aes(x = 'a', y = 'b', color = 'factor(c)')) +
geom_line() +
scale_color_brewer(palette = 'Set1'))

但是,在 R 中的 ggplot2 中,您可以执行相同的操作并获得正确的绘图

 library(ggplot2)

 df = data.frame(a = c(0,1,2,3,4), b = c(0,-2,10,6,8), c = c('a', 'b', 'a', 'a', 'b'))

 ggplot(df, aes(x = a, y = b, color = factor(c))) +
 geom_line() +
 scale_color_brewer(palette = "Set1")

【问题讨论】:

  • 在错误中说“Set1”无效,您是否尝试过其中一个有效的(如“Blues”)?作为scale_color_brewer(palette = 'Blues'))
  • 是的,Blues 有效,但它是顺序音阶。我想使用定性的。
  • 错误消息显示ValueError: Invalid color map name 'Set1' for type 'Sequential' - 如果scale_color_brewer() 期望Sequential 然后忘记其他值。
  • 关键是它不应该期待Sequential。请查看我编辑的问题并添加了一个ggplot2 代码,该代码产生了我的预期。
  • 您是否尝试过如ropensci.github.io/plotly/ggplot2/scale_brewer.html 中所述传递type 参数?

标签: python plotnine


【解决方案1】:

您必须包含“类型”参数

scale_fill_brewer(type="qual", palette="Set1")

【讨论】:

    猜你喜欢
    • 2018-04-03
    • 1970-01-01
    • 1970-01-01
    • 2019-02-17
    • 2012-01-22
    • 1970-01-01
    • 2017-05-17
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多