2016-05-04 24 views
6

Desidero creare raggruppamenti secondari raggruppati (barmode = 'group') in grafico. Ora il problema è che la trama non crea grafici a barre come tracce. Invece i grafici a barre raggruppati vengono creati come elenchi di tracce Bar. Per questo motivo, non so come creare una figura che contenga diagrammi a barre raggruppati come sottotrame (ad esempio, aggiungere un grafico a barre raggruppato utilizzando figure.append_trace()).Aggiunta di grafici a barre di gruppo come sottotrame nella trama

Per esempio, come posso creare sottotrame utilizzando grafici a barre creati in this sample:

import plotly.plotly as py 
import plotly.graph_objs as go 
trace1 = go.Bar(
    x=['giraffes', 'orangutans', 'monkeys'], 
    y=[20, 14, 23], 
    name='SF Zoo' 
) 
trace2 = go.Bar(
    x=['giraffes', 'orangutans', 'monkeys'], 
    y=[12, 18, 29], 
    name='LA Zoo' 
) 
data = [trace1, trace2] 
layout = go.Layout(
    barmode='group' 
) 
fig = go.Figure(data=data, layout=layout) 
plot_url = py.plot(fig, filename='grouped-bar') 
+0

si può essere più specifico? – wind85

+0

Sareste aperti alle soluzioni usando matplotlib invece di trama? – user2027202827

+0

@hobenkr sì, andrebbe bene –

risposta

0

Non ho mai usato il pacchetto plotly ma ciò che sembra siete dopo è piuttosto semplice utilizzando matplotlib. Ecco un esempio piuttosto minimale di mostrare grafici a barre raggruppati come sottotrama. Fammi sapere se questo non è esattamente quello che stavi chiedendo.

import numpy as np 
import matplotlib.pyplot as plt 

# First subplot 
plt.subplot(2, 1, 1) 

x = np.linspace(0, 10) 
y = np.sin(np.pi * x) 

plt.plot(x, y) 

# Second subplot 
plt.subplot(2, 1, 2) 
titles = ('Canada', 'US', 'England', 'Other') 
y_pos = np.arange(len(titles)) 
width = 0.2 
bar_height1 = [6,5,7,2] 
bar_height2 = [x+1 for x in bar_height1] 

plt.bar(y_pos, bar_height1, width, align='center', alpha=0.8, color='r') 
plt.bar(y_pos+width, bar_height2, width, align='center', alpha=0.8, color='b') 

plt.xticks(y_pos + width/2, titles) 

# Show the plots 
plt.show() 

Matplotlib plot

1

SI! Nuovo per plot.ly e aveva questo problema, e come accennato nel mio commento, non potevo semplicemente farlo in pandas/matplotlib per vari motivi. Ma attraverso la magia delle sottotrame, puoi in effetti ricreare trame multi-traccia semplicemente suddividendole insieme. enter image description here

import plotly.plotly as py 
import plotly.graph_objs as go 
trace1 = Bar(
    x=['giraffes', 'orangutans', 'monkeys'], 
    y=[20, 14, 23], 
    name='SF Zoo' 
) 
trace2 = Bar(
    x=['giraffes', 'orangutans', 'monkeys'], 
    y=[12, 18, 29], 
    name='LA Zoo' 
) 
trace3 = Scatter(
    x=['giraffes', 'orangutans', 'monkeys'] 
    ,y=[33,20,17] 
    ,name='subplots ftw' 
) 


fig = tools.make_subplots(rows=2, cols=1, shared_xaxes=True) 

fig.append_trace(trace3, 1,1) 
fig.append_trace(trace1, 2, 1) 
fig.append_trace(trace2,2,1) 


fig['layout'].update(height=600, width=600) 
iplot(fig) 
Problemi correlati