Plotly: How to change the colorscheme of a plotly express scatterplot?(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.qualitativeI am trying to work with
plotly, specificallyploty 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
plotlyvisualization?解决方案Generally, changing the color scheme for a plotly express figure is very straight-forward. What's causing the problems here is the fact that
speciesis 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_mapis a perfectly valid, albeit cumbersome approach. I prefer using the keyword argumentcontinuous_colorscalein combination withpx.colors.qualitative.Antique, whereAntiquecan be changed to any of the discrete color schemes available in plotly express. Just rundir(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 examplecolor_continuous_scale=px.colors.sequential.Inferno, and get this plot:What's possibly causing confusion here, is that setting
color='species, and keepingcolor_continuous_scale=px.colors.sequential.Infernowill 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.Infernonot having an effect. This is becausespeciesis a categorical variable with these different values :['setosa', 'versicolor', 'virginica'], socolor_continuous_scaleis simply ignored. Forcolor_continuous_scaleto take effect you'll have to use a numerical value, likesepal_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_colorscalein combination withpx.colors.qualitative这篇关于Plotly:如何更改 plotly express 散点图的颜色方案?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!
本文标题为:Plotly:如何更改 plotly express 散点图的颜色方案?
基础教程推荐
- 使用生成器和迭代器时 Python 多循环失败 2022-01-01
- 将 x 轴刻度更改为自定义字符串 2022-01-01
- 与常规 dict 相比,Python manager.dict() 非常慢 2022-01-01
- numpy float:比算术运算中内置的慢 10 倍? 2022-01-01
- Discord.py 缺少必需的参数 2022-01-01
- 尝试制作WhatsApp机器人 2022-01-01
- 用 Python 编写 Fortran 无格式文件 2022-01-01
- 在 Celery 工作人员中捕获 Heroku SIGTERM 以优雅地关 2022-01-01
- 由Python将MP3转换为MIDI(类型错误:无法加载插件:mtg-Melodia:Melodia) 2022-01-01
- pyserial - 可以从线程 a 写入串行端口,是否阻塞从线程 b 读取? 2022-01-01
