2016-06-20 12 views
0

Das nvd3-Liniendiagramm im folgenden Beispiel verwendet die Python-Liste als Datenquelle. Aber wie man Multiline aus einem Pandas-Datenrahmen plottet, ohne explizit die Spalten anzugeben, d. H. Wie in Pandas Plot: df.plot() df könnte x Spalten enthalten.Mehrzeilendiagramm aus Dataframe mit nvd3

from nvd3 import lineChart 

# Open File for test 
output_file = open('test_lineChart.html', 'w') 
# --------------------------------------- 
type = "lineChart" 
chart = lineChart(name=type, x_is_date=False, x_axis_format="AM_PM") 

xdata = list(range(0, 24)) 
ydata = [0, 0, 1, 1, 0, 0, 0, 0, 1, 0, 0, 4, 3, 3, 5, 7, 5, 3, 16, 6, 9, 15, 4, 12] 
ydata2 = [9, 8, 11, 8, 3, 7, 10, 8, 6, 6, 9, 6, 5, 4, 3, 10, 0, 6, 3, 1, 0, 0, 0, 1] 

kwargs1 = {'color': 'black'} 
kwargs2 = {'color': 'red'} 
extra_serie = {"tooltip": {"y_start": "There is ", "y_end": " calls"}} 
chart.add_serie(y=ydata, x=xdata, name='sine', extra=extra_serie, **kwargs1) 
extra_serie = {"tooltip": {"y_start": "", "y_end": " min"}} 
chart.add_serie(y=ydata2, x=xdata, name='cose', extra=extra_serie, **kwargs2) 

chart.buildhtml() 

output_file.write(chart.htmlcontent) 

# close Html file 
output_file.close() 

Wie aus diesem Datenrahmen plotten nvd3 mit:

df = pd.DataFrame(data) 
df = df.set_index('datetime') 
fig, ax = plt.subplots() 
df.plot(ax=ax, marker='o') 

Antwort

1

IIUC, die chart die Daten als list nimmt, so dass Sie Ihre index und column Daten list wie konvertieren, so müßten (vorausgesetzt, Ihre Namen sind columncol1 und col2 jeweils:

def plot_nvd3(df, ydata='col1', ydata2='col2'): 
    # Open File for test 
    output_file = open('test_lineChart.html', 'w') 
    # --------------------------------------- 
    type = "lineChart" 
    chart = lineChart(name=type, x_is_date=False, x_axis_format="AM_PM") 

    xdata = df.index.tolist() 
    ydata = df[ydata].tolist() 
    ydata2 = df[ydata2].tolist() 

    kwargs1 = {'color': 'black'} 
    kwargs2 = {'color': 'red'} 
    extra_serie = {"tooltip": {"y_start": "There is ", "y_end": " calls"}} 
    chart.add_serie(y=ydata, x=xdata, name='sine', extra=extra_serie, **kwargs1) 
    extra_serie = {"tooltip": {"y_start": "", "y_end": " min"}} 
    chart.add_serie(y=ydata2, x=xdata, name='cose', extra=extra_serie, **kwargs2) 

    chart.buildhtml() 

    output_file.write(chart.htmlcontent) 

    # close Html file 
    output_file.close() 

Verwendung würde durch:

plot_nvd3(df, 'col1', 'col2') 

habe ich nicht überprüft, wie nvd3 Arbeiten mit DateTimeIndex, obwohl, falls Ihr df = df.set_index('datetime') Ergebnisse in einem.