NzPierre
NzPierre

Reputation: 139

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.

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

Upvotes: 8

Views: 6540

Answers (2)

MAZDAK
MAZDAK

Reputation: 583

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.

Upvotes: 1

Zalak Bhalani
Zalak Bhalani

Reputation: 1144

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/

Upvotes: 10

Related Questions