【问题标题】:Overlapping colorscales in plotly在情节中重叠色阶
【发布时间】:2020-10-26 15:25:34
【问题描述】:

我绘制了一个带有 2 个子图的图形,每个子图都有不同的比例。一切都正确绘制,除了色标都绘制在右侧并且完全重叠 - 它们不可读。我不知道如何定位/重新定位各个子图比例。我在下面包含了我的代码。谢谢。

import pandas as pd
import plotly.graph_objects as go
from plotly.subplots import make_subplots

df = pd.read_csv(entry)
custColorscale = [[0, 'green'], [0.5, 'red'], [1, 'rgb(50, 50, 50)']]

fig = make_subplots(
    rows=1, cols=2, subplot_titles=('one', 'two'))

fig.add_trace(
    go.Scatter(x=df['tO'],
               y=df['t1'],
               mode='markers',
               marker=dict(colorscale=custColorscale,
                           cmin=0, cmax=2,
                           size=6, color=df['Var1'],
                           showscale=True),
               text=df['Var2']),
    1, 1)

fig.add_trace(
    go.Scatter(x=df['tO'],
               y=df['t1'],
               mode='markers',
               marker=dict(
        size=6, color=df['Var2'],
        showscale=True),
        text=df['Var2']),
    1, 2)

fig.update_layout(height=700, width=1900,
                  title='Raw data')
fig.update_layout(coloraxis=dict(
    colorscale='Bluered_r'))
fig.write_html(fig, file='raw plots.html', auto_open=True)

【问题讨论】:

  • 我添加了导入的模块并删除了pio。由于 4.0 版仅离线。

标签: python plotly subplot


【解决方案1】:

查看 Plotly 文档,您会发现 this,它提供了一些关于如何解决问题的提示。滚动到“标记”属性,您会发现它具有称为“颜色条”的子属性。颜色栏又具有多个选项,可以帮助您按照您想要的方式设置绘图。特别是您会发现颜色条的“x”、“y”和“len”属性非常有用。您可以使用它们来定位秤。

这个问题也与this 有关,但对于等高线图 - 您正在制作一个散点图,这就是为什么应该搜索散点图参考。

下面显示了一个最小工作示例 (MWE),但带有一个玩具数据集。

## make necessary imports
import numpy as np
from plotly.subplots import make_subplots
import plotly.graph_objects as go
import pandas as pd

## make a fake dataset with pandas

d = {'t0': [i for i in np.arange(0.,10.,1.)], 't1': [i for i in 
    np.arange(10.,20.,1.)],'Var1': [i for i in np.arange(20.,30.,1.)],'Var2': 
   [i for i in   np.arange(30.,40.,1.)] }

df = pd.DataFrame(data=d) #the dataset is made to mock the example code you provided

对于您的情节,您有以下内容:

# make subplots

 custColorscale = [[0, 'green'], [0.5, 'red'], [1, 'rgb(50, 50, 50)']]
 fig = make_subplots(
 rows=1, cols=2, subplot_titles=('one', 'two'),horizontal_spacing = 0.4)
 
 # plot 1
 fig.add_trace(
    go.Scatter(x=df['t0'],
           y=df['t1'],
           mode='markers',
           marker=dict(colorscale=custColorscale,
                       cmin=0, cmax=2,
                       size=6, color=df['Var1'],
                       showscale=True,colorbar=dict(len=1.05, x=0.35 
                  ,y=0.49)), text=df['Var2']), 1, 1)
## plot 2
fig.add_trace(
   go.Scatter(x=df['t0'],
           y=df['t1'],
           mode='markers',
           marker=dict(
    size=6, color=df['Var2'],
    showscale=True,colorbar=dict(len=1.05,  x=1.2  , y=0.49)),
    text=df['Var2']), 
    1, 2 )
# show plots
fig.update_layout(height=500, width=700,
              title='Raw data')
fig.update_layout(coloraxis=dict(
           colorscale='Bluered_r'))

fig.show()

唯一的补充是:

  1. 标记的颜色条属性。

  2. 为第一个比例留出空间的水平间距。

随意使用这些属性。

我希望这会有所帮助!

最好的问候。

【讨论】:

  • 非常感谢微笑。那是一种享受。 xy 位置值需要玩,但很顺利。
  • @JoFleming 欢迎您。我很高兴它有所帮助。最好的问候。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2022-07-21
  • 2020-10-25
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2020-11-11
相关资源
最近更新 更多