Search code examples
pythonbar-chartdata-visualizationaxis-labels

How do I plot percentage labels for a horizontal bar graph in Python?


Can someone please help me plot x axis labels in percentages given the following code of my horizontal bar chart?

Finding it difficult to find as I want a more simplistic chart without x axis labels and ticks.

[Horizontal Bar Chart][1]

# Plot the figure size
plt.figure(figsize= (8,6))

# New variable and plot the question of the data frame in a normalized in a horizontal bar chat. 
ax1 = df[q1].value_counts(normalize=True).sort_values().plot(kind="barh", color='#fd6a02', width=0.75, zorder=2)

# Draw vague vertical axis lines and set lines to the back of the order
vals = ax1.get_xticks()
for tick in vals:
    ax1.axvline(x=tick, linestyle='dashed', alpha=0.4, color = '#d3d3d3', zorder=1)
    
# Tot3als to produce a composition ratio
total_percent = df[q1].value_counts(normalize=True) *100

# Remove borders
ax1.spines['right'].set_visible(False)
ax1.spines['top'].set_visible(False)
ax1.spines['left'].set_visible(False)
ax1.spines['bottom'].set_visible(False)

# Set the title of the graph inline with the Y axis labels. 
ax1.set_title(q1, weight='bold', size=14, loc = 'left', pad=20, x = -0.16)
             
# ax.text(x,y,text,color)
for i,val in enumerate(total):
    ax1.text(val - 1.5, i, str("{:.2%}".format(total_percent), color="w", fontsize=10, zorder=3)

# Create axis labels
plt.xlabel("Ratio of Responses", labelpad=20, weight='bold', size=12)

Each time I get a EOF error. Can someone help?


Solution

  • It's not based on your code, but I'll customize the answer from the official reference. The point is achieved with ax.text(), which is a looping process.

    import matplotlib.pyplot as plt
    import numpy as np
    
    # Fixing random state for reproducibility
    np.random.seed(19680801)
    
    plt.rcdefaults()
    fig, ax = plt.subplots()
    
    # Example data
    people = ('Tom', 'Dick', 'Harry', 'Slim', 'Jim')
    y_pos = np.arange(len(people))
    performance = 3 + 10 * np.random.rand(len(people))
    
    ax.barh(y_pos, performance,  align='center')
    ax.set_yticks(y_pos)
    ax.set_yticklabels(people)
    ax.invert_yaxis()  # labels read top-to-bottom
    ax.set_xlabel('Performance')
    ax.set_title('How fast do you want to go today?')
    
    # Totals to produce a composition ratio
    total = sum(performance)
    
    # ax.text(x,y,text,color)
    for i,val in enumerate(performance):
        ax.text(val - 1.5, i, str("{:.2%}".format(val/total)), color="w", fontsize=10)
    
    plt.show()
    

    enter image description here