Bar chart in Seaborn showing missing values in the axes

I am working with a dataset that contains the following columns:

Electronics   Answers
Smartphone    Right
Computer      Right
Smartphone    Wrong
Smartphone    Wrong

I am looking to create a barplot using seaborn, where the y-axis represents Electronics and the x-axis represents Answers. However, when I attempted to create the plot using the code snippet below:

ax = sns.barplot(x='answers', y='electronics', fill ='electronics', data=df)

I encountered an error saying "ValueError: Neither the x nor y variable appears to be numeric."

Is there a way to create a plot where the x-axis displays two columns (one for 'Wrong' and one for 'Right'), and the y-axis represents the electronics used in each category?

Thank you in advance for any assistance provided.

Answer №1

  • For your data, I recommend using seaborn.countplot with the parameter hue=Electronics.
import seaborn as sns
import pandas as pd
import matplotlib.pyplot as plt

data = {'Electronics': ['Smartphone', 'Computer', 'Smartphone', 'Smartphone'],
        'Answers': ['Right', 'Right', 'Wrong', 'Wrong']}

df = pd.DataFrame(data)

p = sns.countplot(x='Answers', data=df, hue='Electronics')

https://i.stack.imgur.com/ZwfNY.png

Similar questions

If you have not found the answer to your question or you are interested in this topic, then look at other similar questions below or use the search

The execute command within a function is malfunctioning in Python 3.5

Currently, I am working on a python program that aims to parse JSON files based on their tags using the Python `exec` function. However, I encountered an issue where the program fails when the `exec` statement is within a function. RUN 1: Exec in a functi ...

SQLFORM that does not include a submit button

Currently working with the web2py framework, I am faced with a challenge in creating a SQLFORM without a submit button. The issue arises from having multiple forms on the page, some of which share common fields, making it impossible to use SQLFORM.factor ...

Tips for importing an Excel file into Databricks using PySpark

I am currently facing an issue with importing my Excel file into PySpark on Azure-DataBricks machine so that I can convert it to a PySpark Dataframe. However, I am encountering errors while trying to execute this task. import pandas data = pandas.read_exc ...

Can multiple instances of a program be launched simultaneously?

As part of an educational project, I am developing a Python bot that creates Twitter accounts using disposable email addresses (catchalls) and Russian phone numbers. Successfully passing both the email and phone verification stages piqued my interest in sc ...

Error thrown due to obtaining multiple objects using the `get_or_create` method

Here is a concise snippet of code I have for retrieving or creating a conversation for the current user: c, created = Conversation.objects.get_or_create( message__sender=request.user, content_type=get_content_type(request.POST['ct']), ...

What are some efficient ways to read multiple lines from a file quickly in Python?

Currently, I am using the following Python code: file = open(filePath, "r") lines=file.readlines() file.close() If my file contains multiple lines (10,000 or more), my program tends to slow down when processing more than one file. Is there a way to optim ...

Guide on showcasing a list over several pages in Flask

I'm working with Flask and facing a challenge in displaying a dynamic list of items across multiple pages, as the list can potentially contain over 1000 items. Inside app.py, I've set up a route like this: @app.route('/displayitems') ...

Searching for a keyword within quotation marks followed by another keyword within quotation marks using regular expressions in Python

Hi there! I've recently started learning Python and am currently experimenting with regex to match a specific string. string = '"formula_pretty":"MoS2"' whatIsee =re.search(r'(?<="formula_pretty":").+(?= \")',string.group( ...

In Python, use a loop to assign different values to various index positions within a list

In my current project, I am working with a variety of data types including Lists, float values, and a NumPy array. dt = list(range(1, 12)) c = 18 limit = 2.75 Energy = np.zeros(len(dt)) My goal is to assign the value c = 18 in the NumPy array Energy. ...

How can one best tackle the solution of a differential equation at each interval of time?

Is there a suitable equation solver for a timestep scenario? I have tried using ODEint, Solve_ivp, and even sympy to solve a first order differential equation like this: dTsdt = Ts* A - B + C # Defined in a function. This is the mathematical model. wh ...

I am interested in inputting information into a specific tab within a Google Sheet

Using the following code snippet, I can successfully retrieve data from my Gsheet. #AUTHENTICATING THE CREDENTIALS FOR CONNECTING THE SCRIPT TO GSHEET gc = pygsheets.authorize(service_file='client_secret.json' ) # CONNECTING THE GSHEET sh = gc.o ...

Choosing a value from a dropdown menu with Selenium and Python: Step-by-step guide

Currently, I am using Selenium with Python and I am looking to automate the process of selecting an option from a drop-down menu. Specifically, I need to select the option labeled (00:00-06:00). <div class="prepopulated-select__SelectContainer-sc-xyh ...

Setting boundaries for the values of a list that is being passed: what is the best approach

I am encountering difficulty setting a range for the x and y coordinates in my code. Even after trying to implement a for loop, I am still facing issues. My main goal is to create a distinct range for the coordinates without having them repeated multiple ...

What are the steps to obtain the phase FFT of a signal? Is it possible to retrieve the phase information in the time domain?

I have successfully obtained the magnitude of a signal from a .wav file, but now I am wondering how to also retrieve the phase information. Here is my code snippet that allows me to browse for a .wav file and extract the signal: def browse_wav(self): ...

Temporary Placeholder

I am a beginner in the world of Python and I am faced with the challenge of assigning a value to the input() function. This value is passed as an argument (sys.argv[1]), and my goal is to make it editable. Here is an example of how I want to handle the inp ...

Compare the data in one column of the dataframe with the data in

Here is the data frame I am working with: CET MaxTemp MeanTemp MinTemp MaxHumidity MeanHumidity MinHumidity revenue events 0 2016-11-17 11 9 7 100 85 63 385.943800 rain 1 2016-11-18 ...

What could be causing sympy.diff to not behave as anticipated when differentiating sympy polynomials?

Trying to understand why the differentiation of sympy polynomials using sympy.diff isn't producing the expected result. It seems that when a function is defined with sympy.Poly, the derivative calculation doesn't work as intended even though it w ...

Python script successfully executing queries on Postgres database but failing to create tables

My objective is to create a temporary table in the postgres database and retrieve records from it to insert into another table. Here is the code I have implemented: The import psycopg2 import sys import pprint from __future__ import print_function from ...

Using Python and Selenium to interact with an image button element by clicking on it

I've been attempting to click on that button, but no matter what I try, I can't seem to make it work. button id = edita_customer Here is the HTML code snippet: <div class="card mb-3" style="color: #ace;"> <div c ...