Search code examples
pythonnumpyplotly

Use count in create_distplot Python


I would like to use count as the y-axis instead of density as a percentage, as shown in the plot with code below:

import plotly.figure_factory as ff
import numpy as np

# Add histogram data
x1 = np.random.randn(200) - 2
x2 = np.random.randn(200)
x3 = np.random.randn(200) + 2
x4 = np.random.randn(200) + 4

# Group data together
hist_data = [x1, x2, x3, x4]

group_labels = ['Group 1', 'Group 2', 'Group 3', 'Group 4']

# Create distplot with custom bin_size
fig = ff.create_distplot(hist_data, group_labels, bin_size=.2)
fig.show()

enter image description here

I tried adding the argument histnorm= '', which did change y-axis to count:

import plotly.figure_factory as ff
import numpy as np
fig = go.Figure()
# Add histogram data
x1 = np.random.randn(200) - 2
x2 = np.random.randn(200)
x3 = np.random.randn(200) + 2
x4 = np.random.randn(200) + 4

# Group data together
hist_data = [x1, x2, x3, x4]

group_labels = ['Group 1', 'Group 2', 'Group 3', 'Group 4']

# Create distplot with custom bin_size
fig = ff.create_distplot(hist_data, group_labels, bin_size=.2, histnorm= '')
fig.show()

However it had the line chart removed: enter image description here

Is there a way to change the y-axis to count without altering the other parts of the plot, eg., the line chart?


Solution

  • Looking at the hist='' output again, I noticed that the kde curve is not deleted, it just appears to be straightened and deleted due to the increased y-axis value. So by changing the height of the graph size, the curve can be seen.

    import plotly.figure_factory as ff
    import numpy as np
    
    # Add histogram data
    x1 = np.random.randn(200) - 2
    x2 = np.random.randn(200)
    x3 = np.random.randn(200) + 2
    x4 = np.random.randn(200) + 4
    
    # Group data together
    hist_data = [x1, x2, x3, x4]
    
    group_labels = ['Group 1', 'Group 2', 'Group 3', 'Group 4']
    
    # Create distplot with custom bin_size
    fig = ff.create_distplot(hist_data, group_labels, bin_size=.2, histnorm='')
    fig.update_layout(autosize=False, height=800)# update
    
    fig.show()
    

    enter image description here