我想用这种风格制作直方图:
但是在 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()
go.Scatter()
而不是 go.Histogram()
?使用 fig = go.Figure(data=[go.Histogram(x=x)])
的方法最接近您想要的绘图是:
这非常接近,但您特别想排除每个条"的垂直线.而且我还没有找到使用 go.Histogram
设置排除或隐藏它们的方法.
go.Histogram()
导入 plotly.graph_objects将熊猫导入为 pd将 numpy 导入为 np将 plotly.io 导入为 pio将 plotly.express 导入为 pxpio.templates.default = "plotly_white";将 numpy 导入为 npx = 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')))图.show()
I want to make a histogram with this style:
But using plotly in Python. I.e. I want to merge the bars and plot only the contour. I am using this code:
import plotly.graph_objects as go
import numpy as np
x = np.random.randn(500)
fig = go.Figure(data=[go.Histogram(x=x)])
fig.show()
I have been looking for examples on how to do this but could not find any.
Your best option is to handle the histogram with numpy like count, index = np.histogram(df['data'], bins=25)
, and then use go.Scatter()
and set the linetype to horizontal, vertical, horizontal
with line=dict(width = 1, shape='hvh')
. Take a look at the very last section why go.Histogram()
will not be your best option. With a few other specifications for the layout of go.Scatter()
, the snippet below will produce the following plot:
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()
go.Scatter()
and not go.Histogram()
?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.
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 样式绘制直方图,仅显示直方图的轮廓?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!