Search code examples
pythonhistogramridgeline-plot

Calculate sample size of each ridge in a ridgeline plot


I want my sample size text on each of the ridge for the ridge plot below (on top right possible). Has anyone tried it with joyplot.

import joypy

range_P = [0,500,1000,1500,2000,2500,3000]
labels = [('RZ for PRP. \n('+str(range_P[i])+'-'+str(range_P[i]+500)+' mm)') for i in range(7)]

fig, axes = joypy.joyplot([RZS_P['RZ'][(RZS_P['PRP'] > range_P[i]) & (RZS_P['PRP'] <= range_P[i]+500)] for i in range(7)],
                          ylim='own', 
                          overlap = 0, 
                          bins = 20, 
                          figsize=(6,10), 
                          alpha = 0.6, 
                          labels = labels, 
                          color  ='#1f78b4'
                         )
plt.xlim(0,1000)

enter image description here


Solution

  • When joyplot plot multiple graphs, axis of each graph is independent. So you can set one position and use for loop.

    I could not reproduce your code but used joyplot author's code. Text t1, t2, t3, t4 are set right side of each graph.

    1.Download iris.csv from somewhere.

    2.Set sample = [].

    3.Set x and y of axes[].text(x,y,..) as you like by adjustig y_position = axes[i].get_ylim()[1] / 3.5.

    import joypy
    import pandas as pd
    from matplotlib import pyplot as plt
    from matplotlib import cm
    
    iris = pd.read_csv("iris.csv")
    
    sample = ['t1', 't2', 't3', 't4']
    
    %matplotlib inline
    
    fig, axes = joypy.joyplot(iris, ylim='own')
    
    for i in range(len(sample)):
        y_position = axes[i].get_ylim()[1] / 3.5  # adjust with ylim for each plot
        axes[i].text(9, y_position, sample[i])
    

    enter image description here