Reputation: 13437
This is a similar problem to this question. Here I have a dataframe with date index and I want to plot bars in a range only so I can access to other bars with the bottom slider or zooming.
Here is my code
import pandas as pd
import plotly.offline as py
n = 30
df = pd.DataFrame({"A": list(range(n))},
index=pd.date_range(start="2017-12-01",
periods=n))
data = [{'x': df.index,
'y': df["A"],
'type': 'bar'}]
layout = dict(xaxis=dict(range=[df.index[-10], df.index[-1]]))
fig = dict(data=data, layout=layout)
py.plot(fig)
As you can see the first and the last bars are cut in half.
I could play with offset using
data = [{'x': df.index,
'y': df["A"],
'type': 'bar' ,
'offset': -0.15
}]
But then the last bar is not plotted and even the ticks are offset.
Upvotes: 1
Views: 1394
Reputation: 2151
I'm new to plotly, but I infer the bars are centered on the ticks (the dates), and so plot is in fact showing the range you specify. (I agree that this is somewhat surprising behavior, aesthetically.) You can show the whole bar by changing the range you display using pandas.tseries.offsets.DateOffset
: https://pandas.pydata.org/pandas-docs/stable/timeseries.html#dateoffset-objects
import pandas as pd
import plotly.offline as py
from pandas.tseries.offsets import *
n = 30
df = pd.DataFrame({"A": list(range(n))},
index=pd.date_range(start="2017-12-01",
periods=n))
data = [{'x': df.index,
'y': df["A"],
'type': 'bar'}]
layout = dict(xaxis=dict(range=[df.index[-10] + DateOffset(hours = -12), df.index[-1] + DateOffset(hours = 12)]))
fig = dict(data=data, layout=layout)
py.plot(fig)
Upvotes: 2