2017-03-12 57 views
2

我試圖創建具有相同數據的兩個次要情節的人物創建具有共享Y軸小提琴打印和散點圖:
1)小提琴曲線圖,佔據了數字
的1/4 2)填充圖中其餘3/4的散點圖
這兩個數字應共享y軸標籤。在Plotly

我設法用Matplotlib創建這個,但需要交互式版本。
如何將Plotly Violin子圖與Plotly Scatter子圖組合在一個圖中?

我已經試過thusfar(職級和分數的數據):

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? 

謝謝!

回答

1

Plotly的小提琴情節是散點圖的集合。因此可以將每個分別添加到一個子圖中,並將散點圖添加到另一個子圖中。

enter image description here

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) 
+0

感謝您描述小提琴陰謀的組成;知道的非常有用! – chasingtheinfinite