密谋:如何绘制累积的“步骤"?直方图? [英] Plotly: How to plot a cumulative "steps" histogram?

查看:113
本文介绍了密谋:如何绘制累积的“步骤"?直方图?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试在python中使用Plotly绘制累积直方图,但使其看起来像台阶",即没有颜色的条形图仅显示顶行.像这样:

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:

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

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:

那有什么窍门?

推荐答案

如果您愿意在绘制数据之前 进行装箱和累加,则可以将go.Scatter对象与线的shape属性设置为'hvh'.

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'.

情节:

代码:Jupyter Notebook的设置

Code: Setup for a Jupyter Notebook

#imports
import plotly.plotly as py
import plotly.graph_objs as go
from plotly.offline import download_plotlyjs, init_notebook_mode, plot, iplot

import numpy as np
import pandas as pd

# qtconsole for debugging
#%qtconsole -- style vim

# Notebook settings
init_notebook_mode(connected=True)

# Some sample data
x = np.random.normal(50, 5, 500)
binned = np.histogram(x, bins=25, density=True)
plot_y = np.cumsum(binned[0])

# Line
trace1 = go.Scatter(
    x=binned[1],
    y=plot_y,
    mode='lines',
    name="X",
    hoverinfo='all',
    line=dict(color = 'rgb(1255, 0, 0)', shape='hvh'
    )
)

data = [trace1]

# Layout
layout = dict(title = 'Binned data from normal distribution',
    legend=dict(
        y=0.5,
        traceorder='reversed',
        font=dict(
            size=16
        )
    )
)

# Make figure
fig = dict(data=data, layout=layout)

# Plot
iplot(fig, filename='line-shapes')

我希望这是您可以使用的东西!

I hope this is something you can use!

不要犹豫,让我知道.

一些详细信息:

数据样本是使用np.random.normal()进行的. x是样本正态分布,均值= 50,sigma = 5和500个观测值.然后使用np.histogram()x放入50个纸槽中,这将返回两个数组.这些用作绘图的数据源.

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 using np.histogram() which returns two arrays. These are used as data source for the plot.

可能的替代方法:

我还尝试将您的代码段与一些随机样本数据一起使用,并将shape='hvh'包含在您的line=dict(color="red", width=1)中.但这似乎没有用.我还考虑过修改go.Histogram()的布局,以便仅绘制条形图的顶线,但我认为这是不可能的.

I also tried using your snippet with some random sample data and include shape='hvh' in your line=dict(color="red", width=1). That did not seem to work though. I also considered modifying the layout of your go.Histogram() so that only the top line of the bars were plotted, but I don't think it's possible.

这篇关于密谋:如何绘制累积的“步骤"?直方图?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆