Python Forum
Likert survey data plot error
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Likert survey data plot error
#1
Hi,
I just have read and checked that idea here:
https://stackoverflow.com/questions/7097...ale-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:
Error:
TypeError: DataFrame.set_axis() got an unexpected keyword argument 'inplace'
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:
Error:
TypeError: DataFrame.set_axis() got an unexpected keyword argument 'inplace'
How to rectify this, please ?
Reply
#2
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)
Reply
#3
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/7422...ualization

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
Reply
#4
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)
Output:
Answer Q1 Q2 Q3 16 no answer 8.0 8.0 14.0 17 Very satisfied 5.0 5.0 0.0 18 Neither satisfied nor dissatisfied 2.0 1.0 1.0 19 Somewhat satisfied 1.0 1.0 1.0 20 Somewhat dissatisfied 0.0 1.0 0.0
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.
Reply
#5
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()
[Image: prv97I.png]
Reply
#6
(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.
Reply
#7
pandas.DataFrame.set_axis() dropped the inplace argument in V2.0, released April of this year. The stack overflow post predates that.
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  Error when animating 3D plot Tee 4 957 Jul-03-2023, 08:49 PM
Last Post: Tee
  Plot a pandas data fram via pyqtgraph with an modul import and qt designer widget Nietzsche 0 858 May-29-2023, 02:42 PM
Last Post: Nietzsche
  Create simple live plot of stock data dram 2 2,936 Jan-27-2023, 04:34 AM
Last Post: CucumberNox
Thumbs Up Python 3 Jupyter notebook ternary plot data nicholas 0 952 Jan-21-2023, 05:01 PM
Last Post: nicholas
  pandas.errors.ParserError: Error tokenizing data. C error: Expected 9 fields in line Anldra12 9 15,376 Jun-15-2021, 08:16 AM
Last Post: Anldra12
  Plot data from CSV allen04 2 2,407 Jan-03-2021, 10:30 AM
Last Post: Axel_Erfurt
  How to plot intraday data of several days in one plot mistermister 3 2,929 Dec-15-2020, 07:43 PM
Last Post: deanhystad
  Creating Complex Color Spectrum Plot From Data JoeDainton123 2 2,143 Sep-15-2020, 08:09 AM
Last Post: DPaul
  How to plot data from live datasource? Makada 14 5,934 Sep-06-2020, 07:13 PM
Last Post: Makada
  Bode plot from time series experiment data discus 4 7,367 Jun-20-2020, 07:46 AM
Last Post: discus

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020