Plotly:如何使所有打印为灰度?

2024-10-04 03:16:35 发布

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

我使用Plotly在Python中生成一些线条图。使用如下示例代码:

from plotly import offline as plot, subplots as subplot, graph_objects as go
  
fig = subplot.make_subplots(rows=2, cols=1, shared_xaxes=True, vertical_spacing=0.01)
trace1 = go.Scatter(x = [1, 2, 3], y = [1, 2, 3])
trace2 = go.Scatter(x = [1, 2, 3], y = [4, 5, 6])
fig.append_trace(trace1, 1, 1)
fig.append_trace(trace2, 2, 1)

config_test_plot = {'displaylogo': False, 'displayModeBar': False, 'scrollZoom': True}
test_plot_html = plot.plot(fig, output_type='div', include_plotlyjs=False, config= config_test_plot)

我能够得到所需的绘图。然而,我希望能够得到所有的灰度图。我发现所有的默认主题都不是这种类型的。还有什么我可以做的吗


Tags: testconfigfalsetruegoplotasfig
1条回答
网友
1楼 · 发布于 2024-10-04 03:16:35

您尚未指定是为整个绘图指定灰色方案,还是仅为线条指定灰色方案。但是为了让事情变得简单,我将假设前者。在这种情况下,我将:

  1. 对未直接连接到数据集的地物元素使用template = 'plotly_white',并且
  2. 使用n_colors(lowcolor, highcolor, n_colors, colortype='tuple')为所有线指定灰度

示例图:

enter image description here

但正如@S3DEV所提到的,使用灰色调色板也是一种可行的方法,这可以通过以下方式实现:

# In:
px.colors.sequential.Greys

# Out:
# ['rgb(255,255,255)',
# 'rgb(240,240,240)',
# 'rgb(217,217,217)',
# 'rgb(189,189,189)',
# 'rgb(150,150,150)',
# 'rgb(115,115,115)',
# 'rgb(82,82,82)',
# 'rgb(37,37,37)',
# 'rgb(0,0,0)']

这将非常适合您的用例,只有有限的行数。在这种情况下,您可以使用以下设置:

from plotly import offline as plot, subplots as subplot, graph_objects as go 
from itertools import cycle
fig = subplot.make_subplots(rows=2, cols=1, shared_xaxes=True, vertical_spacing=0.01)
trace1 = go.Scatter(x = [1, 2, 3], y = [1, 2, 3])
trace2 = go.Scatter(x = [1, 2, 3], y = [4, 5, 6])
fig.append_trace(trace1, 1, 1)
fig.append_trace(trace2, 2, 1)

colors = cycle(list(set(px.colors.sequential.Greys)))

f = fig.full_figure_for_development(warn=False)
for d in fig.data:
    d.line.color = next(colors)
fig.show()

并获得:

enter image description here

我想这就是你想要的。但是这里一个相当大的缺点是px.colors.sequential.Greys中的颜色数量是有限的,我不得不使用一个循环来分配数据的行颜色。和n_colors(lowcolor, highcolor, n_colors, colortype='tuple')允许您定义起始颜色、结束颜色以及在它们之间缩放的许多颜色,以形成所有线条的完整比例。这也可以让你根据自己的喜好调整颜色的亮度。所以你可以得到这个:

enter image description here

……这:

enter image description here

或者这个:

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"

# data sample
nperiods = 200
np.random.seed(123)
cols = 'abcdefghijkl'
df = pd.DataFrame(np.random.randint(-10, 12, size=(nperiods, len(cols))),
                  columns=list(cols))
datelist = pd.date_range(datetime.datetime(2020, 1, 1).strftime('%Y-%m-%d'),periods=nperiods).tolist()
df['dates'] = datelist 
df = df.set_index(['dates'])
df.index = pd.to_datetime(df.index)
df.iloc[0] =1000
df = df.cumsum()#.reset_index()

greys_all = n_colors('rgb(0, 0, 0)', 'rgb(255, 255, 255)', len(cols)+1, colortype='rgb')
greys_dark = n_colors('rgb(0, 0, 0)', 'rgb(200, 200, 200)', len(cols)+1, colortype='rgb')
greys_light = n_colors('rgb(200, 200, 200)', 'rgb(255, 255, 255)', len(cols)+1, colortype='rgb')
greys = n_colors('rgb(100, 100, 100)', 'rgb(255, 255, 255)', len(cols)+1, colortype='rgb')
fig = df.plot(title = 'Greys_light', template='plotly_white', color_discrete_sequence=greys_light)
fig.update_layout(template='plotly_white')
fig.show()

相关问题 更多 >