Plotly:如何更改 plotly express 散点图的颜色方案?

问题描述

我正在尝试使用

但是,我想尝试更改颜色方案,即为每个物种呈现的颜色.

我已阅读:

  • 那么连续变量呢?

    考虑以下代码段:

    将 plotly.express 导入为 pxdf = px.data.iris()fig = px.scatter(df, x="sepal_width", y="sepal_length",color="sepal_length", color_continuous_scale=px.colors.sequential.Viridis)图.show()

    运行它会产生这个情节:

    您可以将颜色更改为 dir(px.colors.sequential) 下可用的任何其他主题,例如 color_continuous_scale=px.colors.sequential.Inferno,以及得到这个情节:

    这里可能引起混淆的是,设置 color='species 并保持 color_continuous_scale=px.colors.sequential.Inferno 会给你这个情节:p>

    该图现在直接跳回到使用默认的绘图颜色,没有给你任何关于 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, ...]

    这让我们回到了我最初对分类值的回答:

    <块引用>

    结合使用关键字参数continuous_colorscalepx.colors.qualitative

    I am trying to work with plotly, specifically ploty express, to build a few visualizations.

    One of the things I am building is a scatterplot

    I have some code below, that produces a nice 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()
    

    However, I want to try and change the colorscheme, i.e., the colors presented for each species.

    I have read:

    • https://plotly.com/python/builtin-colorscales/
    • https://plotly.com/python/colorscales/
    • https://plotly.com/python/v3/colorscales/

    But can not get the colors to change.

    Trying:

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

    yields:

    ---------------------------------------------------------------------------
    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'
    

    Trying

    Trying:

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

    yields:

    ---------------------------------------------------------------------------
    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'
    

    How can I change the colors used in a plotly visualization?

    解决方案

    Generally, changing the color scheme for a plotly express figure is very straight-forward. What's causing the problems here is the fact that species is a categorical variable. Continuous or numerical values are actually easier, but we'll get to that in a bit.

    For categorical values, using color_discrete_map is a perfectly valid, albeit cumbersome approach. I prefer using the keyword argument continuous_colorscale in combination with px.colors.qualitative.Antique, where Antique can be changed to any of the discrete color schemes available in plotly express. Just run dir(px.colors.qualitative) to see what are available to you in the plotly version you are running:

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

    Code 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()
    

    Plot 1:

    So what about continuous variables?

    Consider the following snippet:

    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()
    

    Running this will produce this plot:

    You can change the colors to any other theme available under dir(px.colors.sequential), for example color_continuous_scale=px.colors.sequential.Inferno, and get this plot:

    What's possibly causing confusion here, is that setting color='species, and keeping color_continuous_scale=px.colors.sequential.Inferno will give you this plot:

    The figure now jumps straight back to using the default plotly colors, without giving you any warning about color_continuous_scale=px.colors.sequential.Inferno not having an effect. This is because species is a categorical variable with these different values : ['setosa', 'versicolor', 'virginica'], so color_continuous_scale is simply ignored. For color_continuous_scale to take effect you'll have to use a numerical value, like sepal_length = [5.1, 4.9, 4.7, 4.6, 5. , 5.4, ...]

    And this brings us right back to my initial answer for categorical values:

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

相关文章