Howdy folks;
I’m trying to do something similar to this graph using plotly:
But I’m having trouble figuring out how to create the custom label, like on the above graph.
The closest I’ve come is this:
But I’d like to do something more along the lines of the first graph. Does anyone know how to do this using plotly?
Many thanks!
Here is the code I’m using::
# Create an interactive graph to help visualize numerical data
@widgets.interact(
numericalFeature = numericalFeatures_widget,
marginalFeature = marginals_widget
)
def numercialInspector(marginalFeature, numericalFeature):
# Create a histogram object
fig = px.histogram(
diamonds_num_df,
x = numericalFeature,
marginal = marginalFeature
)
# Extract the mean of the dataset
mean = diamonds_num_df[numericalFeature].mean()
# Create a text stream
meanValue = f"mean = {mean:.2f}"
# Add the annotation to the histogram
fig.add_vline(
x = mean,
line_width = 2,
line_dash = 'dot',
line_color = 'red',
annotation = {
'font' : {
'size' : 12,
'family' : 'Times New Roman',
'color': 'red',
}
},
annotation_text = meanValue,
annotation_position = 'top right'
)
# Extract the median of the dataset
median = diamonds_num_df[numericalFeature].median()
# Create a text stream
medianValue = f"median = {median:.2f}"
# Add the annotation to the histogram
fig.add_vline(
x = median,
line_width = 2,
line_dash = 'dot',
line_color = 'darkgreen',
annotation = {
'font' : {
'size' : 12,
'family' : 'Times New Roman',
'color': 'darkgreen',
}
},
annotation_text = medianValue,
annotation_position = 'top left'
)
# Extract the mode of the dataset
mode = diamonds_num_df[numericalFeature].mode()[0]
# Create a text stream
modeValue = f"mode = {mode:.2f}"
# Add the annotation to the histogram
fig.add_vline(
x = mode,
line_width = 2,
line_dash = 'dot',
line_color = 'black',
annotation = {
'font' : {
'size' : 12,
'family' : 'Times New Roman',
'color': 'black',
}
},
annotation_text = modeValue,
annotation_position = 'top left'
)
# Create a graph title
graphTitle = f"Measures of central tendency for: {numericalFeature}"
# Update the graph
fig.update_layout(
title_text = graphTitle,
yaxis_title_text = 'Total Counts', # yaxis label
height = 600,
width = 1200
)
# Display the results
return fig.show()