2016-06-20 6 views
0

以下の例のnvd3折れ線グラフは、python listをデータソースとして使用しています。しかし、パンダのプロットのように列を明示的に記述することなく、パンダのデータフレームから複数行をプロットする方法:df.plot() dfにはxの列が含まれる可能性があります。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() 

どうnvd3を使用して、このデータフレームからプロットに:

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

答えて

1

IIUC、chartlistとしてデータを取るので、あなたはそのようlistにごindexcolumnデータを変換しなければならない(と仮定あなたのcolumn名前はそれぞれcol1col2次のとおりです。

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() 

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

私がチェックしていないか1でDateTimeIndex、しかし、ケースであなたのdf = df.set_index('datetime')結果とnvd3作品:

使用法によってでしょう。

関連する問題