1

I am wondering is it possible to include the percentage breakdown of each subgroup in the bars of a likert-scale with MatPlotLib? Any help would be greatly appreciated!

Python Code:

from numpy.core import numeric
import plot_likert
import pandas as pd

# define my selections
myscale1 = \
    ['Very unlikely',
     'Unlikely',
     'Neutral',
     'Likely',
     'Very likely']

# create a likert plot

ax1  = plot_likert.plot_likert(GenZ, myscale1, plot_percentage=True, figsize=(15,15), colors=plot_likert.colors.likert5)

ax1.set_title(('Casually watch big matches'), fontsize=30)
ax1.set_ylabel('Football/Soccer Moments',fontdict={'fontsize':28})    
ax1.tick_params(axis='y', labelsize=20)    
ax1.legend(loc="upper middle", ncol=5)

Likert-scale - include percentages to subgroups of each group in the bars

JohanC
  • 71,591
  • 8
  • 33
  • 66

2 Answers2

0

The example code below supposes you're using nmalkin/plot-likert. It creates horizontal bars. There are 6 groups of bars. First, a dummy invisible group to create the correct spacing. And then one group of each of the scales (each group has a fixed color).

You can use matplotlib's new bar_label to label the bars (bar_label is new since matplotlib 3.4, so might need to upgrade):

import plot_likert
import matplotlib.pyplot as plt
import pandas as pd
import numpy as np

rng = np.random.default_rng(seed=42)
data = pd.DataFrame(rng.choice(plot_likert.scales.agree, (200, 2)), columns=['Q1', 'Q2'])

ax = plot_likert.plot_likert(data, plot_likert.scales.agree, plot_percentage=True, figsize=(14, 4))
for bars, color in zip(ax.containers[1:], ['white'] + ['black'] * 2 + ['white'] * 2):
    ax.bar_label(bars, label_type='center', fmt='%.1f %%', color=color, fontsize=15)

plt.tight_layout()
plt.show()

plot_likert with bar_label

JohanC
  • 71,591
  • 8
  • 33
  • 66
0

@JohanC answered my question -

you need to update to the most recent MatPlotLib

import plot_likert
import pandas as pd
# define my selections
myscale1 = \
        ['Strongly disagree',
     'Somewhat disagree',
     'Neither agree nor disagree',
     'Somewhat agree',
     'Strongly agree']
# create a likert plot
ax1  = plot_likert.plot_likert(agree_statements, myscale1, plot_percentage=True, figsize=(20,20), colors=plot_likert.colors.likert5)
ax1.set_title(('Casually watch big matches (21-34 year old)'), fontsize=30)
ax1.set_ylabel('Football/Soccer Moments',fontdict={'fontsize':28})
ax1.set_xlabel('% Breakdown',fontdict={'fontsize':28})
ax1.tick_params(axis='y', labelsize= 15)
ax1.tick_params(axis='x', labelsize= 10)

for bars, color in zip(ax1.containers[1:], ['white'] + ['black'] * 2 + ['white'] * 2):
    ax1.bar_label(bars, label_type='center', fmt='%.1f %%', color=color, fontsize=15)

enter image description here