I have the below code as per .ipynb
!pip install plot_likert
import plot_likert
import pandas as pd
import matplotlib.pyplot as plt
precomputed_counts2 = pd.DataFrame(
{'Strongly disagree': {'Q1': 3.0},
'Disagree': {'Q1': 9.0},
'Neither agree nor disagree': {'Q1': 13.0},
'Agree': {'Q1': 69.0},
'Strongly agree': {'Q1': 31.0}}
)
precomputed_counts2
ax=plot_likert.plot_counts(precomputed_counts2, plot_likert.scales.agree,colors=plot_likert.colors.likert6,bar_labels_color="black",bar_labels="count");
ax.figure.set_size_inches(12, 2)
ax.xaxis.set_label_text('Difficulty Level of Questions');
And the Output is as below: The output has labels but not for Strongly Disagree
How to ensure all options have the data labels in the plot?
I tried to increase the value of Strongly Disagree from 3 to 15 and it does show up as data label in the output.
I have the below code as per https://github/nmalkin/plot-likert/blob/release/docs/guide.ipynb
!pip install plot_likert
import plot_likert
import pandas as pd
import matplotlib.pyplot as plt
precomputed_counts2 = pd.DataFrame(
{'Strongly disagree': {'Q1': 3.0},
'Disagree': {'Q1': 9.0},
'Neither agree nor disagree': {'Q1': 13.0},
'Agree': {'Q1': 69.0},
'Strongly agree': {'Q1': 31.0}}
)
precomputed_counts2
ax=plot_likert.plot_counts(precomputed_counts2, plot_likert.scales.agree,colors=plot_likert.colors.likert6,bar_labels_color="black",bar_labels="count");
ax.figure.set_size_inches(12, 2)
ax.xaxis.set_label_text('Difficulty Level of Questions');
And the Output is as below: The output has labels but not for Strongly Disagree
How to ensure all options have the data labels in the plot?
I tried to increase the value of Strongly Disagree from 3 to 15 and it does show up as data label in the output.
Share Improve this question asked Feb 16 at 18:57 RaisingPhDStarRaisingPhDStar 111 bronze badge New contributor RaisingPhDStar is a new contributor to this site. Take care in asking for clarification, commenting, and answering. Check out our Code of Conduct.1 Answer
Reset to default 0
import plot_likert
import pandas as pd
import matplotlib.pyplot as plt
precomputed_counts2 = pd.DataFrame(
{'Strongly disagree': {'Q1': 3.0},
'Disagree': {'Q1': 9.0},
'Neither agree nor disagree': {'Q1': 13.0},
'Agree': {'Q1': 69.0},
'Strongly agree': {'Q1': 31.0}}
)
ax = plot_likert.plot_counts(precomputed_counts2, plot_likert.scales.agree,
colors=plot_likert.colors.likert6,
bar_labels_color="black")
for p in ax.patches:
width = p.get_width()
x_pos = p.get_x() + width / 2
y_pos = p.get_height() / 2
if width > 0:
ax.text(x_pos, y_pos, f'{int(p.get_width())}',
ha='center', va='center', color="black")
ax.figure.set_size_inches(12, 2)
ax.xaxis.set_label_text('Difficulty Level of Questions')
plt.show()
Remove bar_labels="count"
and add a loop through the bars in the plot using ax.patches. Afterwards I used ax.text
function to manually add text at specific positions for each bar, with adjusted position to make sure the label is centered and always visible.