我试图在python中使用Plotly绘制一个累积直方图,但使其看起来像“步骤”,即没有颜色的条形图,只显示顶线。如下所示:
基本上,我试图重现以下matplotlib代码的行为:
import matplotlib.pyplot as plt
plt.hist(x, cumulative=True, histtype='step')
到目前为止,我能做的最好的事情是:
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])
这会导致类似的结果:
那么诀窍是什么呢?
发布于 2020-06-18 04:52:10
公认的解决方案是有效的,但可能是有限的,因为所有的垃圾箱都是等宽的。一种方法是使用matplotlib计算统计数据,然后使用plotly绘制:
# sample data
# I am not using a normal distribution on purpose so that the effect of varying bin widths is apparent.
x = np.random.rand(100)
# use matplotlib to get "n" and "bins"
# n_bins will affect the resolution of the cumilative histogram but not dictate the bin widths.
n_bins = 100
n, bins, patches = plt.hist(x, n_bins, density=True, histtype='step', cumulative=-1)
# use plotly (v3) to plot
data = []
trace = go.Scatter(
x=bins,
y=n,
mode='lines',
name= "test",
line=dict(
shape='hvh'
)
)
data.append(trace)
fig = go.Figure(data=data)
iplot(fig)
结果应该如下所示:
https://stackoverflow.com/questions/55220935
复制相似问题