2016-06-20 3 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 사용하여이 dataframe에서 플롯하려면 다음과 같이 listindexcolumn 데이터를 변환해야하므로

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

답변

1

IIUC에서, chart가, list로 데이터를한다 (가정 당신의 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') 

내가 확인하지 않은 방법 하나 DateTimeIndex하지만, 경우에 df = df.set_index('datetime') 결과 nvd3 작품 :

사용에 의해 것.