【问题标题】:How to change Plotly express scatterplot continuous colormap?如何更改 Plotly express 散点图连续颜色图?
【发布时间】:2022-03-05 10:57:22
【问题描述】:

我在 plotly express 中绘制了一个散点图作为附图。这里点的大小取决于出现的次数(出现次数越多=点的大小越大)

但我无法更改图形的颜色图。我尝试将连续颜色图关键字用于散点图以及在 matplotlib 中生成颜色图,然后在 plotly 中使用它,但都没有工作。

我正在寻找由黄色(低值)蓝色(中等值)和红色(高值)组成的连续颜色图。

我目前的代码如下:

import pandas as pd
import matplotlib.colors
A= pd.read_csv('example.csv')
dA= A['ML']
dB= A['Radio']
dC= A['Occur']

norm=plt.Normalize(-1,1)
cmap = matplotlib.colors.LinearSegmentedColormap.from_list("", 
["yellow","mediumblue","orangered"])

import plotly.express as px

fig = px.scatter(A, x="ML", y="Radio", color="Occur",color_discrete_sequence= 
["red", "blue", "yellow"]
             ,size='Occur')
fig.update_layout(
font_family="Times New Roman",
legend_title_font_color="green" ,
paper_bgcolor='rgba(0,0,0,0)',
plot_bgcolor='rgba(0,0,0,0)',
xaxis = dict(
    tickmode = 'linear',color= '#000000',
    tick0 = 0,
    dtick = 15
),
yaxis = dict(
    tickmode = 'linear',color= '#000000',
    tick0 = 0,
    dtick = 1
),
font=dict(
    family="Times New Roman",
    size=18,
    color='#000000'
)
)
fig.update_xaxes(showline=True, linewidth=1, linecolor='black', mirror=True)
fig.update_yaxes(showline=True, linewidth=1, linecolor='black', mirror=True)

【问题讨论】:

标签: python plotly plotly-python


【解决方案1】:

连续颜色图规范是“color_continuous_scale”。如果这里指定了任何颜色,它将在情节方面进行处理。如果任意设置颜色范围,则指定范围值和颜色的元组。数据和代码请参考official reference。

import plotly.express as px
df = px.data.iris()

fig = px.scatter(df,
                 x="sepal_width",
                 y="sepal_length",
                 color='petal_length',
                 color_continuous_scale=[(0,'yellow'), (0.5, 'blue'), (1,'red')],
                 template='plotly_white'
                )

fig.show()

【讨论】:

    猜你喜欢
    • 2020-12-04
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2010-11-26
    • 1970-01-01
    • 2018-12-30
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多