更改散点图的颜色方案

问题描述 投票:0回答:2

我正在尝试使用plotly,特别是plotly,以建立一些可视化。

我正在构建的东西之一是ploty express

我下面有一些代码,会产生一个不错的散点图:

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) 可视化中使用的颜色?

python plotly data-visualization visualization
2个回答
0
投票

您可以使用一种称为--------------------------------------------------------------------------- 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对的决定,其中color_discrete_map是颜色的值,k,v是颜色的方案。例如:

k

v


0
投票

您可以使用color_discrete_map:

fig = px.scatter(df, x='sepal_length', y='sepal_width',
              color='species', color_discrete_map={'setosa': 'lightcyan', 
                                                   'versicolor': 'royalblue', 'virginica': 'darkblue'})
© www.soinside.com 2019 - 2024. All rights reserved.