python - Plotly:如何更改散点图散点图的配色方案?

标签 python plotly data-visualization visualization

我正在尝试使用 plotly ,特别是 ploty express 来构建一些可视化对象。

我正在建立的一件事是scatterplot

我下面有一些代码,它产生一个漂亮的散点图:

import plotly.graph_objs as go, pandas as pd, plotly.express as px
df = pd.read_csv('iris.csv')
fig = px.scatter(df, x='sepal_length', y='sepal_width',
              color='species', marker_colorscale=px.colors.sequential.Viridis)
fig.show()

enter image description here

但是,我想尝试更改颜色方案,即为每个物种显示的颜色。

我读过了:
  • https://plotly.com/python/builtin-colorscales/
  • https://plotly.com/python/colorscales/
  • https://plotly.com/python/v3/colorscales/

  • 但是无法获得改变的颜色。

    试:
    fig = px.scatter(df, x='sepal_length', y='sepal_width',
                  color='species', marker_colorscale=px.colors.sequential.Viridis)
    

    产量:
    ---------------------------------------------------------------------------
    TypeError                                 Traceback (most recent call last)
    <ipython-input-6-78a9d58dce23> in <module>
          2 # https://plotly.com/python/line-and-scatter/
          3 fig = px.scatter(df, x='sepal_length', y='sepal_width',
    ----> 4               color='species', marker_colorscale=px.colors.sequential.Viridis)
          5 fig.show()
    
    TypeError: scatter() got an unexpected keyword argument 'marker_colorscale'
    



    试:
    fig = px.scatter(df, x='sepal_length', y='sepal_width',
                  color='species', continuous_colorscale=px.colors.sequential.Viridis)
    

    产量:
    ---------------------------------------------------------------------------
    TypeError                                 Traceback (most recent call last)
    <ipython-input-6-78a9d58dce23> in <module>
          2 # https://plotly.com/python/line-and-scatter/
          3 fig = px.scatter(df, x='sepal_length', y='sepal_width',
    ----> 4               color='species', continuous_colorscale=px.colors.sequential.Viridis)
          5 fig.show()
    
    TypeError: scatter() got an unexpected keyword argument 'continuous_colorscale'
    

    如何更改plotly可视化中使用的颜色?

    最佳答案

    通常,更改用于绘图的图形的配色方案非常简单。在这里导致问题的原因是species分类变量。连续值或数字值实际上更容易些,但我们稍后将进行介绍。

    对于分类值,使用color_discrete_map是一种完全有效的方法,尽管很麻烦。我更喜欢将关键字参数continuous_colorscalepx.colors.qualitative.Antique结合使用,其中Antique可以更改为可在plotly express中使用的任何discrete color schemes。只需运行dir(px.colors.qualitative)即可查看正在运行的可打印版本中可以使用的功能:

    ['Alphabet',
     'Antique',
     'Bold',
     'D3',
     'Dark2',
     'Dark24',
     'G10',......]
    

    代码1:
    import plotly.express as px
    df = px.data.iris()
    fig = px.scatter(df, x="sepal_width", y="sepal_length",
                     color="species", color_discrete_sequence=px.colors.qualitative.Antique)
    
    fig.show()
    

    plotly 1:

    enter image description here

    那么连续变量呢?

    考虑以下代码段:
    import plotly.express as px
    df = px.data.iris()
    fig = px.scatter(df, x="sepal_width", y="sepal_length",
                     color="sepal_length", color_continuous_scale=px.colors.sequential.Viridis)
    
    fig.show()
    

    运行此将生成以下图:

    enter image description here

    您可以将颜色更改为dir(px.colors.sequential)下可用的任何其他主题,例如color_continuous_scale=px.colors.sequential.Inferno,并获得以下图表:

    enter image description here

    在这里可能引起困惑的是,设置color='species并保留color_continuous_scale=px.colors.sequential.Inferno将为您提供以下图表:

    enter image description here

    现在,该图直接跳回使用默认的绘图颜色,而没有,这给您有关color_continuous_scale=px.colors.sequential.Inferno无效的任何警告。
    这是因为species是具有以下不同值的类别变量:['setosa', 'versicolor', 'virginica'],因此color_continuous_scale会被忽略。为了使color_continuous_scale生效,您必须使用一个数值,例如sepal_length = [5.1, 4.9, 4.7, 4.6, 5. , 5.4, ...]
    这使我们回到关于分类值的最初答案:

    Use the keyword argument continuous_colorscale in combination with px.colors.qualitative

    关于python - Plotly:如何更改散点图散点图的配色方案?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/60962274/

    相关文章:

    python - Python 中的类/OO 是一种好方法吗?

    python - 每个 arg 具有多个值的 arg 解析器的最佳实践

    python - 将plot.ly 与 py2exe 一起使用

    javascript - 如何删除 plotly.js x 轴刻度标签/按钮?

    php - 在 PHP 中处理内存数据

    python - 如何将一个类中所有对象共享的变量增加 1?

    python - 转置嵌套生成器

    python - 使用 Python 在 Plotly 中分组图例

    python - 使用 Seaborn 的分布图的部分阴影

    r - 使用 ggplot2 创建类似于 d3.js force layout 的气泡图