Setting the default zoom (start position and level) of a plot

Say I have this:

s = pandas.Series(numpy.random.randint(10, 90, 500))
s.hvplot.bar()

This generates a plot similar to this:

which is not that useful due to the number of bars displayed. A user can manually zoom in to get something like this:

(since I’m a new user, I’m not allowed to put more than one image - I’ll add the image that should have been here as a comment)

but in cases where the number of bars is expected to be high it’s not a good user experience to have them always zoom in.

Is there a way to set the starting position and zoom level (preferably in the number of bars displayed in the case of a bar chart)? So e.g. display first 50 bars or last 50 bars or something of sorts (while still allowing the user to zoom out and see some other part of the chart)?

The image that should have been in the post:

NOTE: See post below from icyrockcom, in short xlim doesn’t work on bar chart due to axis being categorical.

Hi @icyrockcom,

I’m not aware if you have some kind of control over the zoom tool as such but if you know your data size then you can simply manually set the x axis range for example something like this: (not tried but think it’ll work)

s.hvplot.bar(xlim=(124,150))

If the x is going to vary between data sets you could add in some method to get the length and subtract an amount to get the tail or plus an amount from zero or find the middle plus minus and set the xlim appropriately. Something to this effect, but I’d maybe have a graph on the side showing the full picture or ensure user is aware there is more to be seen

#To find the last fifty bars
import hvplot
import pandas as pd
import numpy as np 

s = pd.Series(np.random.randint(10, 90, 500))

x_max = len(s)
x_min = x_max - 50 # random number probably better with a percentage of max length

s.hvplot.bar(xlim=(x_min,x_max))

interestingly if you reset the graph not sure if it’ll reset to your new defined x or take the max x resulting in initial picture.

Anyway hope it helps, Carl.

Thanks for the hint Carl. Apparently, xlim is ignored by hvplot.bar :frowning: I’ve found another discussion that gives a workaround Barh Ignoring xlim and I’ve created an issue since this seems like a bug hvplot.bar ignores xlim · Issue #946 · holoviz/hvplot · GitHub.

1 Like

Out of curiosity, what kind of data is that? I’m asking as indeed HoloViews, on which hvPlot is based, expects Bar plots to take care of categorical data only.

You could probably use the workaround provided here: Workaround for date-based histogram tick labels?