I'm having trouble understanding how datetime works in Bokeh.
My code is outputting milliseconds on the x axis, which seems to be the default if the axis type is 'datetime', but I would like to have it separated by every 5 years.
How can I accomplish this in Bokeh? The dates are before 1950 so I can't use a timestamp. When I exclude the datetime axis type, it displays every single value as a tick on the x axis. When I try to change the inputted data as a datetime object, Bokeh rejects it.
I've read other posts on the subject, as well as Bokeh's documentation on
DatetimeTickFormatter, which doesn't seem to be working for me either.
Datetime axis in Bokeh
Bokeh FixedTicker with Custom Datetime/Timestamp values
https://docs.bokeh.org/en/latest/docs/reference/models/formatters.html?highlight=formatters#module-bokeh.models.formatters
All my code for the plot is below:
r = requests.get('https://raw.githubusercontent.com/FreeCodeCamp/ProjectReferenceData/master/GDP-data.json')
data = json.loads(r.text)
chart_data = {
'date': [a[0] for a in data['data']], #this is the problem data
'gdp': [a[1] for a in data['data']],
}
source = ColumnDataSource(chart_data)
hover = HoverTool(
tooltips=[
('GDP', '#gdp{$0,0.00}'),
('Date', '#date{%Y - %B}'),
],
formatters={
'date' : 'datetime',
}
)
bar = figure(x_range=chart_data['date'],
x_axis_type='datetime',
plot_height=750,
plot_width=1000,
tools='wheel_zoom, reset, save',
title=data['source_name'])
bar.vbar(x='date', top='gdp', width=0.9, source=source)
bar.add_tools(hover)
#visual settings
bar.xaxis.formatter = DatetimeTickFormatter(years = ['%Y'])
bar.xaxis.major_label_orientation = 'vertical'
bar.xaxis.minor_tick_line_color = None
bar.xgrid.grid_line_color = None
bar.yaxis.formatter = NumeralTickFormatter(format='$0,0.00')
bar.ygrid.grid_line_color = None
show(bar)
Thank you for the help, this has been driving me crazy!
Thanks to help from bigreddot. My issue was that my plot was acting as a Categorical range due to me setting the x_range property, instead of defaulting to datetime.
Related
I tried to write these codes to display the dataseries plot, but no data was not displayed.
I dont know where is the problem exactly.
data=pd.read_csv('weather.csv')[['STA','Date','Precip','MaxTemp','MinTemp','MeanTemp','Snowfall']].dropna()
data = data[data['Precip'] != 'T']
data['Precip'].astype(float)
data['STA']=data['STA'].astype("string")
data['Date']=pd.to_datetime(data['Date'])
stations=list(set(data['STA']))
stations.sort()
select_inital=select.value
colors = list(Category20_16)
colors.sort()
subset=data[data['STA']==select_inital]
initial_values= list(set(subset['STA']))
for i, j in enumerate(initial_values):
subset=data[data['STA']==j]
d=subset[['Date','Precip']]
d.sort_values('Date')
x=d['Date']
y=d['Precip']
d = ColumnDataSource(d)
p = figure(plot_width=700, plot_height=700, x_range=(0,200), title='Weather Evolution',x_axis_label='Date', y_axis_label='Precip',x_axis_type='datetime')
p.line(x,y, legend_label="Evolution", line_width=2)
show(p)
This is just guessing but I believe the problem is, that you are trying to set limits to the x_range. Bokeh is evaluating dates as milliseconds from 1970-01-01 00:00 and your x_range=(0,200) is also interpreted as millisecond. This means the visible area is very small and starts at January 1st 1970. You could use the defaults by bokeh instead.
Minimal example
This is your code for the figure except I removed the x_range.
import pandas as pd
from bokeh.plotting import figure, show, output_notebook
output_notebook()
x = pd.date_range('2022-12-01', '2022-12-24', freq='D')
y = list(range(1,25))
p = figure(
plot_width=700,
plot_height=700,
# x_range=(0,200),
title='Weather Evolution',
x_axis_label='Date',
y_axis_label='Precip',
x_axis_type='datetime'
)
p.line(x,y, legend_label="Evolution", line_width=2)
show(p)
Bokeh default x_range
x_range by user
Comment
If you want to set the x_range for a axis with type "datetime" you can pass timestamp objects to it.
Valid are among other things (e.g. float)
# datetime
from datetime import datetime
x_range=(datetime(2022,12, 7),datetime(2022,12, 10))
# pandas
import pandas as pd
x_range=(pd.Timestamp('2022-12-07'),pd.Timestamp('2022-12-10'))
I am having last 1 year time series financial data. I removed weekends as no data is present.
I created plotly viz, 2w,1m range selector buttons are not showing correct time range.
While YTD,6m,all showing correct data.
2w button showing 2 week forward days but there is no data, i want to see last two week and 1m data.
Reproducable Code-
# !pip install investpy
import pandas as pd
import investpy
import datetime
import plotly.graph_objects as go
import plotly.figure_factory as ff
import plotly.express as px
today = datetime.now() #Today Datetime
today_fut = today.strftime("%Y,%m,%d") #Converting today date to string format "%Y,%m,%d"
today_fut = datetime. strptime(today_fut, '%Y,%m,%d').date() #Converting today's date to "date" format for NSE Tools library
today = today.strftime("%d/%m/%Y") #Converting today date to string format "%d/%m/%Y"
one_year= datetime.today() - timedelta(days=370) #Subrtracting todays date with 370 Days
one_year = one_year.strftime("%d/%m/%Y") #Converting into string format "%d/%m/%Y"
df = investpy.get_index_historical_data(index="Nifty 50",country="India",from_date=str(one_year),to_date= str(today))
print("Investpy NF Dataframe",df.tail())
bnf = investpy.get_index_historical_data(index="Nifty Bank",country="India",from_date=str(one_year),to_date= str(today))
print("Investpy BNF Dataframe",bnf.tail())
fig = go.Figure(data = [ go.Scatter(x = df.index,y = df['Close'],line=dict(color = 'Steelblue',width=2),mode='lines+markers',name = 'NIFTY'),
go.Scatter(x = bnf.index,y = bnf['Close'],line=dict(color = 'yellowgreen',width=2),mode='lines+markers',name = 'BANK NIFTY'),
])
fig.update_layout(
title='NF and BNF',template = 'plotly_dark',xaxis_tickformat = ' %d %B (%a)<br> %Y',
yaxis_title='NF & BNF',yaxis_tickformat= "000",yaxis_side = 'right',xaxis_title='Date',legend = dict(bgcolor = 'rgba(0,0,0,0)'))
layout = go.Layout(showlegend=True)
##https://plotly.com/python/legend/
fig.update_layout(legend=dict(
yanchor="top",
y=0.99,
xanchor="left",
x=0.01 ))
#hide weekends
fig.update_xaxes( rangeslider_visible=True, rangebreaks=[
dict(bounds=["sat", "mon"]) ])
##https://plotly.com/python/legend/
fig.update_layout(legend=dict(
orientation="h",
yanchor="bottom",
y=1.02,
xanchor="right",
x=1
))
config = dict({'scrollZoom': False})
fig.update_layout(
xaxis=dict(rangeselector=dict(buttons=list([
dict(count=14,label="2w",step="day",stepmode="todate"),
dict(count=1,label="1m",step="month",stepmode="backward"),
dict(count=3,label="3m",step="month",stepmode="backward"),
dict(count=6,label="6m",step="month",stepmode="backward"),
dict(count=1,label="YTD",step="year",stepmode="todate"),
dict(step="all") ])),rangeslider=dict(visible=True),type="date"))
fig.update_layout(
xaxis_rangeselector_font_color='white',
xaxis_rangeselector_activecolor='red',
xaxis_rangeselector_bgcolor='green',
)
fig.show()
pio.write_html(fig, file='wrong_range_selecor_for-2w&1m_button.html', auto_open=True)
Pls let me know , if there is any bug in plotly range selector or I am doing something wrong in xaxis=dict(rangeselector=dict(buttons=list([xaxis=dict(rangeselector=dict(buttons=list([ code.
Snap- When I click on 2w but it show future dates, where no data is present in dataframe.
1m button snap-
Yes it seems like a bug with the range selector (type='date'). It occurs when setting the scatter plot mode to 'markers' or 'lines+markers' : in fact, using markers leads to the addition of an extra range added to the xaxis, extending the default date range from the input data.
So when you click on 2w, the slider goes from that out-of-range date (in the future) back to 2w before that date, the same happens with the other ranges actually, not only 1m, but it is less noticeable for bigger ones.
You could set a fixed range to circumvent the problem using rangeslider_range and range :
fig.update_xaxes(
rangeslider_visible=True,
rangebreaks=[dict(bounds=["sat", "mon"])],
range=[df.index[0], df.index[-1]],
rangeslider_range=[df.index[0], df.index[-1]]
)
...until you click on "all". The problem is that the range selector button step='all' will always reset the layout to use the extended range.
This does not happen when you set the scatter mode to just mode='lines'. In this case the default/'all' slider range fits the data.
So I guess the quick fix is to remove markers, the long one is to use Dash callbacks to have a full control of what the buttons do.
I tried many things, but the X-axis on plotly, which contains times in the format of HH:MM:SS in a pandas dataframe doesn't want to change.
I tried the following:
Trying to change the datatype of the dataframe column to pydatetime
adding the pydates to a list and applying strftime with '%Y-%m-%d %H:%M:%S' (to convert it later) and '%H:%M:%S'
tried the basic stuff with tickformat="%H:%M" in fig.update_xaxes - Method
and tickformat='%H:%M' in fig.update_layout - Method (also with dictionary) for xaxis parameter
also tried this: fig.layout['xaxis_tickformat'] = "%H:%M"
also tried to apply the dates to strftime with "%H:%M" but the values are a type of aggregated than (or only one value for the certain minute is picked)
The results change: Sometimes all datapoints disappear (they are on the left or right corner) and if they not disappear, the x-axis shows the values for 15:23:01 for example.
Below is a code snippet of my method:
pd.options.plotting.backend = "plotly"
dates = pd.to_datetime(dataframe.Time, format='%H:%M:%S')
dates = dates.dt.to_pydatetime()
datelist = []
for date in dates:
date = datetime.datetime.strftime(date, '%Y-%m-%d %H:%M:%S')
datelist.append(date)
# dates['Time'] = pd.Series(dates, dtype=object)
# dates = dates.apply(lambda x: x.strftime('%H:%M:%S'))
print(dataframe.Time)
print(dates)
df = dataframe
# also tried here with x = dataframe.Time with same results
fig = px.line(df, x=datelist, y=["Foo1", "Foo2"], title='Foo')
# changing Y-Axis ticks to 10 minutes
fig.update_xaxes(tickangle=45,
# type="date",
tickmode='array',
tickformat="%H:%M",
tickvals=df["Time"][0::600])
fig.update_layout(
title="Foo", title_x=0.5,
xaxis_title="Time",
xaxis=dict(
tickformat='%H:%M'
),
# xaxis_tickformat = "%H:%M",
yaxis_title="<i>g</i>",
legend_title="Sensor",
font=dict(
family="Courier New, monospace",
size=50,
color="RebeccaPurple"
)
)
# fig.layout['xaxis_tickformat'] = "%H:%M"
fig.show()
I hope you can help me as I followed the instructions on the plotly website and googled, but plotly stuff seems to be rare.
Thanks in advice, I can provide more infos if needed.
I'm using the datetime axis of Bokeh. In the Bokeh data source, I have my x in numpy datetime format and others are y numbers. I'm looking for a way to show the label of the x datetimx axis right below the point. I want Bokeh to show the exact datetime that I provided via my data source, not some approximation! For instance, I provide 5:15:00 and it shows 5:00:00 somewhere before the related point.I plan to stream data to the chart every 1 hour, and I want to show 5 points each time. Therefore, I need 5 date-time labels. How can I do that? I tried p.yaxis[0].ticker.desired_num_ticks = 5 but it didn't help. Bokeh still shows as many number of ticks as it wants! Here is my code and result:
import numpy as np
from bokeh.models.sources import ColumnDataSource
from bokeh.plotting import figure
from bokeh.io import show
from bokeh.palettes import Category10
p = figure(x_axis_type="datetime", plot_width=800, plot_height=500)
data = {'x':
[np.datetime64('2019-01-26T03:15:10'),
np.datetime64('2019-01-26T04:15:10'),
np.datetime64('2019-01-26T05:15:10'),
np.datetime64('2019-01-26T06:15:10'),
np.datetime64('2019-01-26T07:15:10')],
'A': [10,25,15,55,40],
'B': [60,50,80,65,120],}
source = ColumnDataSource(data=data)
cl = Category10[3][1:]
r11 = p.line(source=source, x='x', y='A', color=cl[0], line_width=3)
r12 = p.line(source=source, x='x', y='B', color=cl[1], line_width=3)
p.xaxis.formatter=DatetimeTickFormatter(
seconds=["%H:%M:%S"],
minsec=["%H:%M:%S"],
minutes=["%H:%M:%S"],
hourmin=["%H:%M:%S"],
hours=["%H:%M:%S"],
days=["%H:%M:%S"],
months=["%H:%M:%S"],
years=["%H:%M:%S"],
)
p.y_range.start = -100
p.x_range.range_padding = 0.1
p.yaxis[0].ticker.desired_num_ticks = 5
p.xaxis.major_label_orientation = math.pi/2
show(p)
and here is the result:
As stated in the docs, num_desired_ticks is only a suggestion. If you want a ticks at specific locations that do not change, then you can use a FixedTicker, which can be set by plain list as convenience:
p.xaxis.ticker = [2, 3.5, 4]
For datetimes, you would pass the values as milliseconds since epoch.
If you want a fixed number of ticks, but the locations may change (i.e. because the range may change), then there is nothing built in to do that. You could make a custom ticker extension.
I am trying to plot a graph with dates (pandas datetime) on the x axis. However, they are plotting in numerical format instead (showing up as exponents).
Example of dates:
0 2014-05-01
1 2014-05-02
2 2014-05-03
3 2014-05-04
4 2014-05-05
Name: date, dtype: datetime64[ns]
Code for plotly:
trace1 = go.Scatter(x = df_iso_h.date,
y=del18_f_hum,
mode = 'markers')
data = [trace1]
py.iplot(data)
My x-axis:
Not sure how to fix this??
You need to add layout and specify parameter xaxis in it. Such as here.
So try this:
# Create trace
trace1 = go.Scatter(x = df_iso_h.date,
y=del18_f_hum,
mode = 'markers')
# Add trace in data
data = [trace1]
# Create layout. With layout you can customize plotly plot
layout = dict(title = 'Scatter',
# Add what you want to see at xaxis
xaxis = df_iso_h.date
)
#Do not forget added layout to fig!
fig = dict(data=data, layout=layout)
# Plot scatter
py.iplot(data, filename="scatterplot")
This should help you.
Update: Try to convert datetime column with strftime (new column should be in object format!):
df_iso_h["date"] = df_iso_h["date"].dt.strftime("%d-%m-%Y")
If not worked, add this column in xaxis. Maybe plotly do not support datetime format yyyy-mm-dd... Notice, you xaxis will be looks like 01-05-2014
Figured it out... Plotly does not take pandas datetime, so I had to convert my pandas datetime to python datetime.datetime or datetime.date.
It seems that this was a regression introduced in plotly.py Version 3.2.0 and has been fixed in Version 3.2.1
You can now simply pass the pandas datetime column to plotly and it will handle the proper conversion for you like in the past.
See https://github.com/plotly/plotly.py/issues/1160