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

标签 python python-3.x plotly plotly-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)

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

最佳答案

您尚未指定是为整个地 block 分配灰色配色方案,还是仅为您的线条分配灰色配色方案。但是,为了让我自己轻松一些,我将假设是前者。在那种情况下,我会:

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

示例图:

enter image description here

但是正如@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()

并得到:

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

关于python - Plotly:如何使所有图都变成灰度?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/65586299/

相关文章:

javascript - "Play with this data!"没有出现在我的plotly.js 图中

python - 使用 matplotlib 和 numpy 绘制一行 3 个图,但得到 "IndexError: too many indices for array"

python - Pandas 中没有年份的日期

python - 使用 keras v2.0 未学习 XOR

python - 在 Windows 中请求管理员访问 Python 函数

python - 将表单添加到 Dash/Plotly 应用程序

python - django 测试 - 模型实例未创建

rest - 在 Falcon REST 中解码 JSON 文件上传

python - 使用正则表达式解析财务报表

python - 使用 plotly offline 将图形生成为图像