I am trying to plot a cumulative histogram using Plotly in python, but make it look like "steps", i.e. bars with no color and only the top line is displayed. Something like this:
Basically, I'm trying to reproduce the behavior of the following matplotlib code:
import matplotlib.pyplot as plt
plt.hist(x, cumulative=True, histtype='step')
So far, the best I've been able to do is:
import plotly.graph_objs as go
from plotly.offline import iplot
h = go.Histogram(x=x,
cumulative=dict(enabled=True),
marker=dict(color="rgba(0,0,0,0)",
line=dict(color="red", width=1)))
iplot([h])
Which results in something like:
So what's the trick?
The accepted solution works but may be limiting as the bins are all of equal width. One approach is to use matplotlib to calculate stats, then plot with plotly:
The result should look something like this:
If you're willing to handle the binning and accumulation before you plot the data, you can use a
go.Scatter
object with the shape property of the line set to'hvh'
.Plot:
Code: Setup for a Jupyter Notebook
I hope this is something you can use!
Don't hesitate to let me know if not.
Some details:
The data sample is made using
np.random.normal()
.x
is a sampled normal distribution with mean = 50, sigma = 5 and 500 observations.x
is then put in 50 bins usingnp.histogram()
which returns two arrays. These are used as data source for the plot.Possible alternative approaches:
I also tried using your snippet with some random sample data and include
shape='hvh'
in yourline=dict(color="red", width=1)
. That did not seem to work though. I also considered modifying the layout of yourgo.Histogram()
so that only the top line of the bars were plotted, but I don't think it's possible.