2016-05-04 6 views
6

Ich möchte gruppierte (barmode = 'Gruppe') Balkendiagramm Nebenplots in Plotly erstellen. Das Problem ist nun, dass plotly keine Balkendiagramme als Spuren erstellt. Stattdessen werden gruppierte Balkendiagramme als Listen von Balkendiagrammen erstellt. Aus diesem Grund kann ich keine Figur erstellen, die gruppierte Balkendiagramme als Teilpläne enthält (z. B. Hinzufügen eines gruppierten Balkendiagramms mit figure.append_trace()).Hinzufügen von Gruppen Balkendiagrammen als Unterplots in plotly

Zum Beispiel, wie kann ich erstellen Nebenhandlungen erstellt mit Balkendiagramme 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

können Sie das konkretisieren? – wind85

+0

Wären Sie offen für Lösungen mit Matplotlib anstelle von Plotly? – user2027202827

+0

@hobenkr ja, das wäre in Ordnung –

Antwort

0

Ich habe nie das plotly Paket aber, wie es scheint Sie sind, sind nach recht unkompliziert matplotlib verwenden. Hier ist ein ziemlich minimales Beispiel für das Anzeigen gruppierter Balkendiagramme als Unterplot. Lass es mich wissen, wenn das nicht das ist, wonach du verlangst.

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

YES! Neu bei plot.ly und hatte dieses Problem, und wie ich in meinem Kommentar erwähnt habe, konnte ich es aus verschiedenen Gründen nicht einfach in Pandas/Matplotlib tun. Aber durch die Magie von Subplots können Sie Multi-Trace-Plots tatsächlich erstellen, indem Sie sie einfach zusammenfügen. 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) 
Verwandte Themen