Plotly:如何以 Root 样式绘制直方图,仅显示直方图的轮廓?

Plotly: How to plot histogram in Root style showing only the contours of the histogram?(Plotly:如何以 Root 样式绘制直方图,仅显示直方图的轮廓?)
本文介绍了Plotly:如何以 Root 样式绘制直方图,仅显示直方图的轮廓?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想用这种风格制作直方图:

但是在 Python 中使用

完整代码

导入 plotly.graph_objects将熊猫导入为 pd将 numpy 导入为 np将 plotly.io 导入为 pio将 plotly.express 导入为 pxpio.templates.default = "plotly_white";# 随机数到 dfnp.random.seed(12)df = pd.DataFrame({'data': np.random.randn(500)})# 使用 numpy 生成直方图数据计数,索引 = np.histogram(df['data'], bins=25)# plotly, go.Scatter,线形设置为 'hvh'fig = go.Figure()fig.add_traces(go.Scatter(x=index, y = count,线=字典(宽度= 1,形状='hvh')))# y轴化妆品fig.update_yaxes(显示网格=假,蜱=内部",tickson=边界",滴答声=10,显示线=真,线宽=1,线条颜色='黑色',镜子=真,零线=假)# x轴化妆品fig.update_xaxes(显示网格=假,蜱=内部",tickson=边界",滴答声=10,显示线=真,线宽=1,线条颜色='黑色',镜子=真,零线=假)图.show()

?

使用 fig = go.Figure(data=[go.Histogram(x=x)]) 的方法最接近您想要的绘图是:

这非常接近,但您特别想排除每个条"的垂直线.而且我还没有找到使用 go.Histogram 设置排除或隐藏它们的方法.

Completecode

import plotly.graph_objects as go
import pandas as pd
import numpy as np
import plotly.io as pio
import plotly.express as px

pio.templates.default = "plotly_white"

# random numbers to a df
np.random.seed(12)
df = pd.DataFrame({'data': np.random.randn(500)})

# produce histogram data wiht numpy
count, index = np.histogram(df['data'], bins=25)

# plotly, go.Scatter with line shape set to 'hvh'
fig = go.Figure()
fig.add_traces(go.Scatter(x=index, y = count,
                          line=dict(width = 1, shape='hvh')))

# y-axis cosmetics
fig.update_yaxes(
    showgrid=False,
    ticks="inside",
    tickson="boundaries",
    ticklen=10,
    showline=True,
    linewidth=1,
    linecolor='black',
    mirror=True,
    zeroline=False)

# x-axis cosmetics
fig.update_xaxes(
    showgrid=False,
    ticks="inside",
    tickson="boundaries",
    ticklen=10,
    showline=True,
    linewidth=1,
    linecolor='black',
    mirror=True,
    zeroline=False)

fig.show()

?

The closest you'll get to your desired plot using your approach with fig = go.Figure(data=[go.Histogram(x=x)]) is this:

And that's pretty close, but you specifically wanted to exclude the vertical lines for each "bar". And I have yet not found a way to exclude or hide them with the go.Histogram setup.

Code for go.Histogram()

import plotly.graph_objects as go
import pandas as pd
import numpy as np
import plotly.io as pio
import plotly.express as px

pio.templates.default = "plotly_white"

import numpy as np

x = np.random.randn(500)
fig = go.Figure(data=[go.Histogram(x=x)])
fig.update_traces(marker=dict(color='rgba(0,0,0,0)', line=dict(width=1, color='blue')))
fig.show()

这篇关于Plotly:如何以 Root 样式绘制直方图,仅显示直方图的轮廓?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!

本站部分内容来源互联网,如果有图片或者内容侵犯您的权益请联系我们删除!

相关文档推荐

Leetcode 234: Palindrome LinkedList(Leetcode 234:回文链接列表)
How do I read an Excel file directly from Dropbox#39;s API using pandas.read_excel()?(如何使用PANDAS.READ_EXCEL()直接从Dropbox的API读取Excel文件?)
subprocess.Popen tries to write to nonexistent pipe(子进程。打开尝试写入不存在的管道)
I want to realize Popen-code from Windows to Linux:(我想实现从Windows到Linux的POpen-code:)
Reading stdout from a subprocess in real time(实时读取子进程中的标准输出)
How to call type safely on a random file in Python?(如何在Python中安全地调用随机文件上的类型?)