简体   繁体   中英

bokeh line graph for 3 lines

I have a data of various year and months want to display in 3 lines graph based on category and X axis will be (jan,feb.......dec) Y axis sales. I am confused how to do this as I am new to Bokeh and Python can someone help me please?

import pandas as pd
from bokeh.plotting import figure, output_file, show
from bokeh.models import ColumnDataSource
from datetime import datetime
from bokeh.palettes import Spectral3

output_file('output.html')

df = pd.read_csv('sample.csv')
p = figure(x_axis_type="date")
p.line(x=df.date, y=df.sales, line_width=2)

show(p)

This is the data I have.

在此输入图像描述

You have to convert the contents of the Dates column to Dates. Pandas and Bokeh don't know that these values are dates and treat them like strings if you forget to do this. Also, the x_axis_type should be datetime instead of date.

import pandas as pd
from bokeh.plotting import figure, show
from bokeh.palettes import Spectral3

data = {'Date': ['08-11-2016', '08-11-2016', '12-06-2016', '11-08-2015', '11-10-2015', '09-06-2014', '09-02-2014', '09-01-2014', '09-06-2014', '09-03-2014', '09-05-2014', '09-07-2014', '15-04-2017', '05-12-2016', '22-09-2015', '26-12-2015', '02-01-2015', '02-01-2015', '28-10-2016'],
'Sales': [261.96, 731.94, 14.62,957.5775, 22.368, 48.68, 7.28, 907.152, 18.504, 114.9, 1706.184, 911.424, 15.552, 407.976, 68.81, 2.544, 665.88, 55.5, 8.56],
'Category': ['Furniture', 'Furniture', 'Office Supplies', 'Furniture', 'Office Supplies', 'Furniture', 'Office Supplies', 'Technology', 'Office Supplies', 'Office Supplies', 'Furniture', 'Technology', 'Office Supplies', 'Office Supplies', 'Office Supplies', 'Office Supplies', 'Office Supplies', 'Office Supplies', 'Office Supplies']}
df = pd.DataFrame.from_dict(data)
df['Date'] = pd.to_datetime(df['Date'])
df = df.sort_values(by='Date')
categories = df['Category'].unique()

p = figure(x_axis_type="datetime")
for cat, color in zip(categories, Spectral3):
    df_line = df.loc[df['Category'] == cat]    
    p.line(x='Date', y='Sales', line_width=2, source=df_line, legend=cat, color=color)
p.legend.click_policy="hide"
show(p)

在此输入图像描述

(Technology and Office supplies are also plotted, its just difficult to see them due to the short x range/color)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM