How to change the edgecolor of an Histogram in plotly?

Question:

I’m trying to change from matplotlib to plotly and I have to relearn every basic move.

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 :

import matplotlib.pyplot as plt
import numpy as np

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

Which gives me :

matplotlib with edgecolor = white

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.

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()

plotly closest attempt

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 !)

Asked By: NzPierre

||

Answers:

There is function called fig.update_traces you can increase marker(edge) width and change color to ‘White’ like:

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/

Answered By: Zalak Bhalani

plotly.graph_objects.Histogram accepts a Marker as input argument. 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.

Answered By: MAZDAK
Categories: questions Tags: , ,
Answers are sorted by their score. The answer accepted by the question owner as the best is marked with
at the top-right corner.