Click here to Skip to main content
16,020,669 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
Hello everyone,

I'm a bit new to seaborn plotting and when using the following code, the figures look a bit weird and I guess not precisely what they must be. Can you please guide where I'm lacking?

What I have tried:

import pandas as pd
import numpy as np
import matplotlib.pyplot as plt
import seaborn as sns
import plotly as px

Titanic=sns.load_dataset("titanic")
sns.countplot(x='age', data=Titanic) # x-ticks are overlapping

sns.violinplot(data=Titanic, x="age", y="fare", hue="class") # same problem
sns.pairplot(Titanic) # give and error
sns.boxenplot(data=Titanic,x="age", y="fare", hue="class") # graph is too bad as well as same error as previous.
sns.violinplot(data=Titanic, x="age", y="fare", hue="class") # graph is too bad as well as same error as previous.
sns.countplot(x='age', data=Titanic) # x-ticks are overlapping
Posted
Updated 13-Jul-23 22:09pm
v2
Comments
[no name] 14-Jul-23 5:30am    
"the figures look a bit weird"
If you want help then you need to provide explicit details of what is wrong and where, with reference to your code. No one here can guess why you think the output is weird. Or possibly you need to work through some of the tutorials on the seaborn web site.
Majid H. Awan 14-Jul-23 5:42am    
Hello, I agree with you, however, I added details which are x-ticks are overlapping, and it's the example I got from the tutorial (were it was working fine) you know well Titanic and these sets of code lines are well known that's why I'm bit frustrated.
[no name] 14-Jul-23 8:10am    
That still does not tell us anything useful. The actual plotting of the graph will depend on the data that is processed, and the calculations made on that data. All you have provided is the method calls used by the seaborn framework and some vague comments that it does not work. You need to go back to the seaborn site and talk with the people who support their code.
Andre Oosthuizen 14-Jul-23 6:37am    
As Richard commented, we cannot guess what is weird, we are also not prepared to go through an entire app to see why it is weird. Please give us an example of what is expected i.e. 1+1=2, then show us what you got as a return i.e. 1+1 = 2.75. This will make more sense to us than what you gave us. Please do not post code or the returns in the comments, add it to your question by selecting "Improve Question".

1 solution

You need help with your Seaborn plots. Let's address them one by one:

1. Overlapping x-ticks in `sns.countplot`: This usually happens when too many unique values are in the 'age' column. To make the plot more readable, you can either increase the figure size or rotate the x-tick labels.

2. Problems with `sns.violinplot`, `sns.boxenplot`, and `sns.pairplot`: These issues might be arising due to the nature of data in the 'age' and 'fare' columns. The 'age' column might have NaN values, or the range of 'fare' might be too wide, causing skewed plots.

3. Error with `sns.pairplot`: This error could be due to non-numeric or NaN values in the dataset. `sns.pairplot` only works with numeric columns and cannot handle NaN values well.

Here are some code adjustments and tips to resolve these issues:

Adjusting the Countplot

Python
plt.figure(figsize=(10, 6)) # Adjusting figure size
sns.countplot(x='age', data=Titanic)
plt.xticks(rotation=90)  # Rotating x-ticks
plt.show()


Handling Violin and Boxen Plots

For `sns.violinplot` and `sns.boxenplot`, ensure that the 'age' and 'fare' columns don't have NaN values or outliers that could distort the plot. You can handle NaN values by removing them or filling them with a central tendency measure (mean, median).

Python
# Handling NaN values
Titanic = Titanic.dropna(subset=['age', 'fare'])

# Now try plotting
sns.violinplot(data=Titanic, x="age", y="fare", hue="class")
plt.show()

sns.boxenplot(data=Titanic, x="age", y="fare", hue="class")
plt.show()


Fixing Pairplot

To fix the `sns.pairplot`, ensure all columns are numeric and handle NaN values.

Python
# Dropping non-numeric columns for pairplot
numeric_cols = Titanic.select_dtypes(include=[np.number]).columns.tolist()
Titanic_numeric = Titanic[numeric_cols].dropna()

sns.pairplot(Titanic_numeric)
plt.show()


Could you try these adjustments and see if they resolve the issues with your plots? If you still encounter problems, the specific error messages could provide more insights into what might be going wrong.
 
Share this answer
 
v2

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900