How to make graph_objects.Bar() look as close to graph_objects.Histogram() as possible

Due to needing access to the counts (y-axis) values, Iโ€™m currently plotting my histograms by first using numpy.histogram() and then calling go.Bar(). This is actually working out better than I thought in terms of speed. However, I would still like to have the plot carry the histogram style as much as possible instead of a plot of bars next to one another with no gap. Is there already a method (e.g. default layout) that I can use to update the plot at the end and make it look like a histogram?

Hello @henrybilliot52 welcome to the forums.

I donโ€™t think that there is a default layout, how do you want it to look like?

Hi @AIMPED,

Thank you for replying! If possible, I would like to be able to make the histograms look like this

where there is no fill in each bar and only the top outline is drawn. So far, I have gotten the bar chart to look like this

by setting marker_line=dict(width=0) and bargap=0.0. Do you know if itโ€™s possible to add more settings and have the bar chart look like the picture I have above?

Hm, you could create a series of points from your y- values and plot them as lineโ€ฆ

EDIT:

I had some time to play around:

import numpy as np
import plotly.express as px

# set number of bins and widht (>=2) of "colums" 
BINS = 20
WIDTH = 5

# create data
np.random.seed(42)
hist, bins = np.histogram(np.random.standard_normal(10000), bins=BINS)

# create y- coordinates of points
y = np.asarray([[i] * WIDTH for i in hist])
y = y.flatten()

# create x- coordinates of points
x_base = np.arange(WIDTH)
x = np.asarray([x_base + i*(WIDTH - 1) for i in range(BINS)])
x = x.flatten()

# create figure
fig = px.line(x=x, y=y)
fig.update_layout(height=500, width=500, xaxis_range=[0, 90])

which creates:
newplot(19)

1 Like

That looks great!! Thank you so much!

1 Like