首页
学习
活动
专区
工具
TVP
发布
社区首页 >问答首页 >Plotly:如何绘制累积的“步数”直方图?

Plotly:如何绘制累积的“步数”直方图?
EN

Stack Overflow用户
提问于 2019-03-18 20:03:43
回答 1查看 5.1K关注 0票数 2

我试图在python中使用Plotly绘制一个累积直方图,但使其看起来像“步骤”,即没有颜色的条形图,只显示顶线。如下所示:

基本上,我试图重现以下matplotlib代码的行为:

代码语言:javascript
复制
import matplotlib.pyplot as plt
plt.hist(x, cumulative=True, histtype='step')

到目前为止,我能做的最好的事情是:

代码语言:javascript
复制
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])

这会导致类似的结果:

那么诀窍是什么呢?

EN

回答 1

Stack Overflow用户

发布于 2020-06-18 04:52:10

公认的解决方案是有效的,但可能是有限的,因为所有的垃圾箱都是等宽的。一种方法是使用matplotlib计算统计数据,然后使用plotly绘制:

代码语言:javascript
复制
# 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)

结果应该如下所示:

票数 3
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/55220935

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档