对图箱形图的四分位数中的数字进行舍入
我已经研究了一段时间,试图弄清楚如何对悬停功能中显示的四分位数中显示的数字进行四舍五入。必须有一个简单的方法来执行此操作,就像使用 x 和 y 坐标一样。在这种情况下,四舍五入到小数点后两位就足够了。
df = pd.read_csv("https://raw.githubusercontent.com/plotly/datasets/master/tips.csv")
fig = go.Figure(data=go.Box(y=df['total_bill'],
name='total_bill',
boxmean=True,
)
)
fig.update_layout(width=800, height=800,
hoverlabel=dict(bgcolor="white",
font_size=16,
font_family="Arial",
)
)
fig.show()
I have been digging around a while trying to figure out how to round the numbers displayed in quartile figures displayed in the hover feature. There must be a straightforward to do this as it is with the x and y coordinates. In this case rounding to two decimals would be sufficient.
df = pd.read_csv("https://raw.githubusercontent.com/plotly/datasets/master/tips.csv")
fig = go.Figure(data=go.Box(y=df['total_bill'],
name='total_bill',
boxmean=True,
)
)
fig.update_layout(width=800, height=800,
hoverlabel=dict(bgcolor="white",
font_size=16,
font_family="Arial",
)
)
fig.show()
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
不幸的是,这看起来 Plotly 无法轻易做到。如果您修改
hovertemplate
,它将仅适用于您将鼠标悬停在其上的标记(异常值),并且每个箱线图统计数据后面的小数在悬停时将保持不变。 plotly-python 的另一个问题是您无法提取箱线图统计信息,因为这需要您与底层的 javascript 进行交互。但是,您可以使用与绘图相同的方法自行计算箱线图统计数据,并将所有统计数据四舍五入到小数点后两位。然后,您可以传递箱线图统计数据:
lowerfence, q1,median,mean, q3, upperfence
强制plotly手动构建箱线图,并将所有异常值绘制为另一条散点图。这是一个相当难看的黑客行为,因为您实际上是在重做 Plotly 已经执行的所有计算,然后手动构建箱线图,但它确实强制箱线图统计数据显示到小数点后两位。
Unfortunately this is something that it looks like Plotly cannot easily do. If you modify the
hovertemplate
, it will only apply to markers that you hover over (the outliers), and the decimals after each of the boxplot statistics will remain unchanged upon hovering. Another issue with plotly-python is that you cannot extract the boxplot statistics because this would require you to interact with the javascript under the hood.However, you can calculate the boxplot statistics on your own using the same method as plotly and round all of the statistics down to two decimal places. Then you can pass boxplot statistics:
lowerfence, q1, median, mean, q3, upperfence
to force plotly to construct the boxplot manually, and plot all the outliers as another trace of scatters.This is a pretty ugly hack because you are essentially redoing all of calculations Plotly already does, and then constructing the boxplot manually, but it does force the boxplot statistics to display to two decimal places.
对我来说,设置
yaxis_tickformat=",.2f"
有效:...您还可以通过设置 y 刻度的文本来覆盖 yaxis:
如果您希望 y 轴刻度保持不变
(在图 5.8.2 上测试)
for me, setting
yaxis_tickformat=",.2f"
worked:... you can also override yaxis back by setting text of y ticks:
if you want the y axis ticks unchanged
(tested on plotly 5.8.2)