简体   繁体   中英

Trying to create a time-data series but plot is wrong way round

I am new to Python and I am trying to build an interactive line plot using data frame with Pandas and Bokeh. Each time I run the code, the plot shows the wrong way round and the x-axis does not show the dates.

Please can someone help a new learner overcome this?

The code I am using is below:

import numpy as np # linear algebra
import pandas as pd # data processing, CSV file I/O (e.g. pd.read_csv)
import warnings
warnings.filterwarnings('ignore')
from bokeh.plotting import figure, show, output_file, output_notebook
from bokeh.palettes import Spectral11, colorblind, Inferno, BuGn, brewer
from bokeh.models import HoverTool, value, LabelSet, Legend, ColumnDataSource,LinearColorMapper,BasicTicker, PrintfTickFormatter, ColorBar
import datetime

df = pd.read_csv('Pubdata.csv')
data.head()

TOOLS = 'save,pan,box_zoom,reset,wheel_zoom,hover'
p = figure(title="Number of Pubs", y_axis_type="linear", plot_height = 400,
           tools = TOOLS, plot_width = 800)
p.xaxis.axis_label = 'Date'
p.yaxis.axis_label = 'Number')

p.line(x=df.Date, y=df.Number,line_color="red", line_width = 3)
p.select_one(HoverTool).tooltips = [
    ('year', '@x'),
    ('Number of crimes', '@y'),
]

output_file("line_chart.html", title="Line Chart")
show(p)

The plot shows like this:

绘图没有显示正确的方式,我试图绘制每个日期访问的酒吧数量。

Here is a sample of the data:

数据样本

Attempting to form a plot similar to this

Hello and welcome on SO @bellablot.

Bokeh can parse datetime objects, if you set x_axis_type to "datetime" . The rest does bokeh for you.

To fulfill the requirements, you have to make sure your date information is parsed by pd.read_csv() . You can use the parameter parse_dates and infer_datetime_format .

If your DataFrame has the correct form you can apply the steps below.

Example

import pandas as pd

from bokeh.plotting import figure, show, output_notebook
from bokeh.models import HoverTool
output_notebook()

df = pd.DataFrame({
    'Number':list(range(10)),
    'Date': pd.date_range('2022-12-22', periods=10, freq='D')
})


p = figure(
    title="Number of Pubs",
    x_axis_type="datetime",
    plot_height = 350,
    plot_width = 350
)
p.xaxis.axis_label = 'Date'
p.yaxis.axis_label = 'Number'

p.line(x=df.Date, y=df.Number, line_color="red", line_width = 3)
p.add_tools(HoverTool(
    tooltips = [
        ('year', '@x{%Y}'), # use ('date', '@x{%Y-%m-%d}') to show more than the year
        ('Number of crimes', '@y')
    ],
    formatters = {"@x": "datetime"},
))

show(p)

Output

带日期时间 x 轴的线形图和带格式化程序的 Hovertool

Comment

This solution adds one Hovertool with one formatter to show only the year. There are many more options and you can read about it HoverTool.formatters and DatetimeTickFormatter in the bokeh documentation.

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