简体   繁体   English

如何更改 plotly 中直方图的边缘颜色?

[英]How to change the edgecolor of an Histogram in plotly?

I'm trying to change from matplotlib to plotly and I have to relearn every basic move.我正在尝试从 matplotlib 更改为 plotly,我必须重新学习每一个基本动作。

One of my habit was to change the edge color of every histogram I made to white so it is easier to see the bars.我的一个习惯是将我制作的每个直方图的边缘颜色更改为白色,以便更容易看到条形图。

On matplotlib, I would do it like that:在 matplotlib 上,我会这样做:

import matplotlib.pyplot as plt
import numpy as np

vals = np.random.rand(50)
plt.hist(vals, edgecolor='white');

Which gives me:这给了我:

带有 edgecolor = white 的 matplotlib

I suppose there is a way to do it with plotly but I searched in the doc and other stackoverflow questions and haven't found it yet.我想有一种方法可以用 plotly 做到这一点,但我在文档和其他 stackoverflow 问题中进行了搜索,但还没有找到它。

Closest way (that make me believe it is somehow possible): setting the template of the figure to a template using white edgecolor最接近的方式(让我相信这是可能的):将图形模板设置为使用白色边缘颜色的模板

import plotly.express as px
from plotly.subplots import make_subplots
import plotly.graph_objects as go
import numpy as np

vals = np.random.rand(50)

fig = px.histogram(x=vals, template="simple_white")
fig.show()

情节最接近的尝试

I also tried to examinate the template simple_white and to look for the param that was making the edges to be white but this didn't lead to anything (if you know what this param is, the answer interests me even more !)我还尝试检查模板simple_white并寻找使边缘变白的参数,但这并没有导致任何结果(如果你知道这个参数是什么,答案会让我更感兴趣!)

There is function called fig.update_traces you can increase marker(edge) width and change color to 'White' like:有名为fig.update_traces的 function 可以增加标记(边缘)宽度并将颜色更改为“白色”,例如:

fig = px.histogram(x=vals)
fig.update_traces(marker_line_width=1,marker_line_color="white")
fig.show()

Reference: https://plotly.com/python/reference/histogram/参考: https://plotly.com/python/reference/histogram/

plotly.graph_objects.Histogram accepts a Marker as input argument. plotly.graph_objects.Histogram接受一个Marker作为输入参数。 That means you can enable the borders and specify their color using a marker:这意味着您可以启用边框并使用标记指定它们的颜色:

import plotly.graph_objects as go

fig.show()
go.Histogram(x=vals,
              marker=dict(line=dict(width=0.8,
                                    color="white")))

To see all other parameters of a marker, see the docs here .要查看标记的所有其他参数,请参阅此处的文档。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM