【问题标题】:Creating a Violin Plot and Scatter Plot with a Shared Y-Axis in Plotly在 Plotly 中创建具有共享 Y 轴的小提琴图和散点图
【发布时间】:2017-08-02 18:43:03
【问题描述】:

我正在尝试使用相同数据的两个子图创建一个图形:
1) 占图1/4的小提琴情节
2) 一个散点图,填充了图形的剩余 3/4
这两个图形应该共享 y 轴标签。

我设法用 Matplotlib 创建了这个,但需要一个交互式版本。
如何将 Plotly Violin 子图与 Plotly Scatter 子图组合在一个图中?

到目前为止我所尝试的(RANKS 和 SCORES 是数据):

import plotly.figure_factory as ff
import plotly.graph_objs as go
from plotly import tools

fig = tools.make_subplots(rows=1, cols=2, shared_yaxes=True)
vio = ff.create_violin(SCORES, colors='#604d9e')
scatter_trace = go.Scatter(x = RANKS, y = SCORES, mode = 'markers')

# How do I combine these two subplots?

谢谢!

【问题讨论】:

    标签: python plotly


    【解决方案1】:

    Plotly 的小提琴图是散点图的集合。因此,您可以将每个单独添加到子图中,并将散点图添加到另一个子图中。

    import plotly
    import numpy as np
    
    #get some pseudorandom data
    np.random.seed(seed=42)
    x = np.random.randn(100).tolist()
    y = np.random.randn(100).tolist()
    
    #create a violin plot
    fig_viol = plotly.tools.FigureFactory.create_violin(x, colors='#604d9e')
    
    #create a scatter plot
    fig_scatter = plotly.graph_objs.Scatter(
        x=x,
        y=y,
        mode='markers',
    )
    
    #create a subplot with a shared y-axis
    fig = plotly.tools.make_subplots(rows=1, cols=2, shared_yaxes=True)
    
    #adjust the layout of the subplots
    fig['layout']['xaxis1']['domain'] = [0, 0.25]
    fig['layout']['xaxis2']['domain'] = [0.3, 1]
    fig['layout']['showlegend'] = False
    
    #add the violin plot(s) to the 1st subplot
    for f in fig_viol.data:
        fig.append_trace(f, 1, 1)
    
    #add the scatter plot to the 2nd subplot
    fig.append_trace(fig_scatter, 1, 2)
    
    plotly.offline.plot(fig)
    

    【讨论】:

    • 感谢您描述小提琴情节的组成;了解非常有用!
    猜你喜欢
    • 2023-03-14
    • 1970-01-01
    • 1970-01-01
    • 2020-11-10
    • 2020-06-11
    • 2022-12-14
    • 2019-07-07
    • 2013-08-09
    • 2021-01-09
    相关资源
    最近更新 更多