Plotly:如何以仅显示直方图轮廓的 Root 样式绘制直方图?
Plotly: How to plot histogram in Root style showing only the contours of the histogram?
我想制作这种风格的直方图:
但在 Python 中使用 plotly。 IE。我想合并条形图并仅绘制轮廓。我正在使用此代码:
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()
我一直在寻找有关如何执行此操作的示例,但找不到任何示例。
你最好的选择是用像 count, index = np.histogram(df['data'], bins=25)
这样的 numpy 处理直方图,然后使用 go.Scatter()
并将线型设置为 horizontal, vertical, horizontal
和 line=dict(width = 1, shape='hvh')
。查看最后一节为什么 go.Histogram()
不是您的最佳选择。对于 go.Scatter()
布局的一些其他规范,下面的代码片段将生成以下图:
完整代码
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()
而不是 go.Histogram()
?
使用 fig = go.Figure(data=[go.Histogram(x=x)])
的方法最接近你想要的情节是这样的:
这非常接近,但您特别想排除每个“条”的垂直线。而且我还没有找到使用 go.Histogram
设置排除或隐藏它们的方法。
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.go.Histogram(): Show only horizontal lines of distribution。仅绘制线条
- 使用 pandas 而不是 numpy 来构建直方图数据,然后绘制成线散点图
import plotly.graph_objects as go
import numpy as np
import pandas as pd
x = np.random.randn(100)
# build data frame that is histogram
df = pd.cut(x, bins=10).value_counts().to_frame().assign(
l=lambda d: pd.IntervalIndex(d.index).left,
r=lambda d: pd.IntervalIndex(d.index).right,
).sort_values(["l","r"]).rename(columns={0:"y"}).astype(float)
# lines in plotly are delimited by none
def line_array(df, cols):
return np.pad(
df.loc[:, cols].values, [(0, 0), (0, 1)], constant_values=None
).reshape(1, (len(df) * 3))[0]
# plot just lines
go.Figure(go.Scatter(x=line_array(df, ["l","r"]), y=line_array(df, ["y","y"]), marker={"color":"black"}))
我想制作这种风格的直方图:
但在 Python 中使用 plotly。 IE。我想合并条形图并仅绘制轮廓。我正在使用此代码:
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()
我一直在寻找有关如何执行此操作的示例,但找不到任何示例。
你最好的选择是用像 count, index = np.histogram(df['data'], bins=25)
这样的 numpy 处理直方图,然后使用 go.Scatter()
并将线型设置为 horizontal, vertical, horizontal
和 line=dict(width = 1, shape='hvh')
。查看最后一节为什么 go.Histogram()
不是您的最佳选择。对于 go.Scatter()
布局的一些其他规范,下面的代码片段将生成以下图:
完整代码
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()
而不是 go.Histogram()
?
使用 fig = go.Figure(data=[go.Histogram(x=x)])
的方法最接近你想要的情节是这样的:
这非常接近,但您特别想排除每个“条”的垂直线。而且我还没有找到使用 go.Histogram
设置排除或隐藏它们的方法。
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.go.Histogram(): Show only horizontal lines of distribution。仅绘制线条
- 使用 pandas 而不是 numpy 来构建直方图数据,然后绘制成线散点图
import plotly.graph_objects as go
import numpy as np
import pandas as pd
x = np.random.randn(100)
# build data frame that is histogram
df = pd.cut(x, bins=10).value_counts().to_frame().assign(
l=lambda d: pd.IntervalIndex(d.index).left,
r=lambda d: pd.IntervalIndex(d.index).right,
).sort_values(["l","r"]).rename(columns={0:"y"}).astype(float)
# lines in plotly are delimited by none
def line_array(df, cols):
return np.pad(
df.loc[:, cols].values, [(0, 0), (0, 1)], constant_values=None
).reshape(1, (len(df) * 3))[0]
# plot just lines
go.Figure(go.Scatter(x=line_array(df, ["l","r"]), y=line_array(df, ["y","y"]), marker={"color":"black"}))