![]() |
Likert survey data plot error - Printable Version +- Python Forum (https://python-forum.io) +-- Forum: Python Coding (https://python-forum.io/forum-7.html) +--- Forum: General Coding Help (https://python-forum.io/forum-8.html) +--- Thread: Likert survey data plot error (/thread-40354.html) |
Likert survey data plot error - Andrzej_Andrzej - Jul-15-2023 Hi, I just have read and checked that idea here: https://stackoverflow.com/questions/70975856/adding-percentages-to-subgroups-of-each-group-likert-scale-python then copying/pasting first example: 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()and get this: So went to the second example from this link, adapting a bit a data:import plot_likert import pandas as pd rng = np.random.default_rng(seed=42) data = pd.DataFrame(rng.choice(plot_likert.scales.agree, (200, 2)), columns=['Q1', 'Q2']) # define my selections myscale1 = \ ['Strongly disagree', 'Disagree', 'Neither agree nor disagree', 'Agree', 'Strongly agree'] # create a likert plot ax1 = plot_likert.plot_likert(data, 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)and received yet still the same error: How to rectify this, please ?
RE: Likert survey data plot error - snippsat - Jul-16-2023 plot_likert is not updated to deal with changes in newer version of Pandas. Pandas newer versions has got rid of parameter inplace . So could downgrade in a virtual environment eg pip install pandas==1.4.3 If i do quick test so is this better,it work bye remove inplace=False in plot_likert.py line 251.# This line df = df.set_axis(new_labels, axis=1, inplace=False) # Change to df = df.set_axis(new_labels, axis=1) RE: Likert survey data plot error - Andrzej_Andrzej - Jul-16-2023 Thank you very much, as I have been struggling with it for a few hours. This is not yet my level to change the code inside a functions. This is another one, not so old topic as from 2022: https://stackoverflow.com/questions/74223046/how-to-transform-pandas-dataframe-for-likert-scale-visualization The data should be transformed from one form to another. I thought that there on the SO these answers were somehow mostly correct. In this case I also copied/pasted the code from solution and the result is as in the picture below. This is not the result the OP wanted and me neither. How can this be corrected, please ? https://imgur.com/a/4wTX0DV RE: Likert survey data plot error - snippsat - Jul-16-2023 You should post your code and not just link top SO as have to put this together. Something like this. import pandas as pd pd.set_option('display.expand_frame_repr', False) data = { "ID": [1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16], "Q1": [ "no answer", "Very satisfied", "no answer", "no answer", "Very satisfied", "no answer", "no answer", "Very satisfied", "Very satisfied", "no answer", "no answer", "Neither satisfied nor dissatisfied", "no answer", "Very satisfied", "Neither satisfied nor dissatisfied", "Somewhat satisfied", ], "Q2": [ "no answer", "Very satisfied", "no answer", "no answer", "Very satisfied", "no answer", "no answer", "Very satisfied", "Very satisfied", "no answer", "no answer", "Somewhat satisfied", "no answer", "Very satisfied", "Somewhat dissatisfied", "Neither satisfied nor dissatisfied", ], "Q3": [ "no answer", "no answer", "no answer", "no answer", "no answer", "no answer", "no answer", "no answer", "no answer", "no answer", "no answer", "no answer", "no answer", "Neither satisfied nor dissatisfied", "no answer", "Somewhat satisfied", ] } df = pd.DataFrame(data) df = pd.concat([df[c].value_counts() for c in df.columns],axis=1).fillna(0) df1 = df.reset_index() df1.columns = ['Answer', 'Del', 'Q1', 'Q2', 'Q3'] df1.drop('Del', inplace=True, axis=1) result = df1.iloc[16:] print(result)
Quote:Thank you very much, as I have been struggling with it for a few hours. This is not yet my level to change the code inside a functions.It not a function change,you change source code of plot_likert. Eg for me C:\Python311\Lib\site-packages\plot_likert\plot_likert.py open file plot_likert.py go to line 251 and change as i describe.You could submit this as a issue,then it maybe will be fixed if Repo still active. RE: Likert survey data plot error - snippsat - Jul-16-2023 import pandas as pd import seaborn as sns import matplotlib.pyplot as plt data = { "ID": [1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16], "Q1": [ "no answer", "Very satisfied", "no answer", "no answer", "Very satisfied", "no answer", "no answer", "Very satisfied", "Very satisfied", "no answer", "no answer", "Neither satisfied nor dissatisfied", "no answer", "Very satisfied", "Neither satisfied nor dissatisfied", "Somewhat satisfied", ], "Q2": [ "no answer", "Very satisfied", "no answer", "no answer", "Very satisfied", "no answer", "no answer", "Very satisfied", "Very satisfied", "no answer", "no answer", "Somewhat satisfied", "no answer", "Very satisfied", "Somewhat dissatisfied", "Neither satisfied nor dissatisfied", ], "Q3": [ "no answer", "no answer", "no answer", "no answer", "no answer", "no answer", "no answer", "no answer", "no answer", "no answer", "no answer", "no answer", "no answer", "Neither satisfied nor dissatisfied", "no answer", "Somewhat satisfied", ] } df = pd.DataFrame(data) # Reshape the DataFrame using melt() df_melted = df.melt(value_vars=["Q1", "Q2", "Q3"], value_name="Answer", var_name="Question") # Plotting using Seaborn countplot sns.set(style="darkgrid") plt.figure(figsize=(14, 6)) sns.countplot(data=df_melted, x="Answer", hue="Question", palette="Set3") plt.xlabel("Response Category") plt.ylabel("Count") plt.title("Count of Occurrences for Each Response Category") plt.legend(title="Question") plt.show() ![]() RE: Likert survey data plot error - Andrzej_Andrzej - Jul-16-2023 (Jul-16-2023, 11:23 AM)snippsat Wrote: You should post your code and not just link top SO as have to put this together.Thank you. I will definitely remember that for the future. So why was this SO code regarded as OK solution over there at SO website ? Your code works and SO's doesn't. What is the opposite way if I want go from result back to df ? I am asking as I come in from R language and we do something like pivot_longer and pivot_wider, melt, dcast, etc. to transform data frames. RE: Likert survey data plot error - deanhystad - Jul-16-2023 pandas.DataFrame.set_axis() dropped the inplace argument in V2.0, released April of this year. The stack overflow post predates that. |