Plotly:如何使所有图都变成灰度?

Plotly: How to make all plots grayscale?

我正在使用 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)

我能够获得所需的地块。但是,我希望能够获得我所有的灰度图。我看到 none 的 Plotly 默认主题是这种类型。无论如何我可以做到这一点吗?

您尚未指定是为整个地块分配灰色配色方案,还是仅为您的线条分配灰色配色方案。但只是为了 让事情对我自己来说更容易,我将假设前者。那样的话,我会:

  1. 对未直接连接到数据集的图形元素使用 template = 'plotly_white',并且
  2. 使用 n_colors(lowcolor, highcolor, n_colors, colortype='tuple').
  3. 为所有线条分配灰度

示例图:

但是正如@S3DEV 提到的那样,使用 greys 调色板也是一种可行的方法,可以通过以下方式访问:

# 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()

并得到:

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

...这个:

或者这个:

如果你也想尝试一下,这里有这些数字的完整设置:

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()