在plotly express中使用n种颜色创建离散颜色贴图

2024-09-30 01:23:50 发布

您现在位置:Python中文网/ 问答频道 /正文

我想为Python中的plotly express绘图创建一个带有n颜色的colormap,它应该从一种颜色淡入另一种颜色。 所有默认颜色贴图只有10个离散值,但我正在寻找一个带有n>;10个离散值

>>> px.colors.sequential.Plasma_r

['#f0f921',
 '#fdca26',
 '#fb9f3a',
 '#ed7953',
 '#d8576b',
 '#bd3786',
 '#9c179e',
 '#7201a8',
 '#46039f',
 '#0d0887']

有没有办法将连续贴图拆分为n个部分


Tags: gt绘图颜色plotlycolorsplasmaexpresspx
1条回答
网友
1楼 · 发布于 2024-09-30 01:23:50

如果你不介意rgb颜色,那么^{}是一种选择。下面是一个在'rgb(0, 0, 0)''rgb(255, 255, 255)'之间的灰度中有15种颜色的示例:

 n_colors('rgb(0, 0, 0)', 'rgb(255, 255, 255)', 15, colortype='rgb')

enter image description here

这里有一个例子,在蓝色'rgb(0, 0, 255)'和红色'rgb(255, 0, 0)'之间有25种颜色:

n_colors('rgb(0, 0, 255)', 'rgb(255, 0, 0)', 25, colortype = 'rgb')

enter image description here

完整代码:

import numpy as np
import pandas as pd
import plotly.graph_objects as go
import plotly.express as px
import datetime
from plotly.colors import n_colors

pd.set_option('display.max_rows', None)
pd.options.plotting.backend = "plotly"


# greys15 = n_colors('rgb(0, 0, 0)', 'rgb(255, 255, 255)', 15, colortype='rgb')
redVSblue = n_colors('rgb(0, 0, 255)', 'rgb(255, 0, 0)', 25, colortype = 'rgb')

fig = go.Figure()

# for i, c in enumerate(greys15):
for i, c in enumerate(redVSblue):
    fig.add_bar(x=[i], y = [i+1], marker_color = c, showlegend = False)
f = fig.full_figure_for_development(warn=False)
fig.show()

相关问题 更多 >

    热门问题