包含标签 seaborn 的文章
Detailed Examples of Seaborn Plotting Kernel Density Curves
In a frequency distribution histogram, when the sample size is sufficiently enlarged to its limit, and the bin width is infinitely shortened, the step-like broken line in the frequency histogram will evolve into a smooth curve. This curve is called the density distribution curve of the population.
In this article, Chunjing Muke will detail how to use the Python plotting library Seaborn and the Iris flower dataset from Pandas to plot various cool density curves.
1. Basic Density Curve
import seaborn as sns
sns.set(color_codes=True)
sns.set_style("white")
df = pd.read_csv('iris.csv')
sns.kdeplot(df['sepal_width'])
To plot a kernel density curve using Seaborn, you only need to use kdeplot
. Note that a density curve only requires one variable; here we choose the sepal_width
column.
2. Density Curve with Shading
import seaborn as sns
sns.set(color_codes=True)
sns.set_style("white")
df = pd.read_csv('iris.csv')
sns.kdeplot(df['sepal_width'],shade=True)
TypeError: ufunc 'isnan' not supported for the input types - Solution
Today, while using Python’s Seaborn to plot a heatmap (clustermap), I kept encountering this error. My data seemed perfectly fine, and a Google search didn’t yield any good solutions. After some exploration, I’m sharing the final solution here.
1. Generating the DataFrame
import pandas as pd
import numpy as np
import matplotlib.pyplot as plt
from seaborn import clustermap
import seaborn as sns; sns.set(color_codes=True)
df = pd.DataFrame([["a","b","c","d","e","f"],[1,2,3,4,5,6],[2,3,4,5,6,7],[3,4,5,6,7,8]], columns=list('ABCDEF')).T
df
g = sns.clustermap(df.iloc[:,1:],cmap="PiYG")
After generating and transposing the DataFrame, a TypeError
occurs: TypeError: ufunc 'isnan' not supported for the input types, and the inputs could not be safely coerced to any supported types according to the casting rule "safe"
.
2. Cause of the Error
This type of error arises because the DataFrame has been transposed, and the original DataFrame contained string columns. Just like in the example above, the first column contains strings (values ‘abcdef’). When transposed, all numerical values in the DataFrame are also converted to object
types instead of float
or int
numerical types. Therefore, trying to plot a heatmap with character types naturally leads to an error.
TypeError: ufunc 'isnan' not supported for the input types - Solution
After generating and transposing the DataFrame, a TypeError occurred: TypeError: ufunc ‘isnan’ not supported for the input types, and the inputs could not be safely coerced to any supported types according to the casting rule “safe”.
2. Cause of the Error
This type of error occurs because the DataFrame has been transposed, and the original DataFrame contains a column with strings. Just like in the example above, the first column contains string values “abcdef”. After transposition, all numbers in the DataFrame also become “object” type instead of “float” or “int” numeric types. Therefore, when we try to plot a heatmap with character types, an error naturally occurs.
If the DataFrame originally contained only numeric types, there would be no issue here.
3. Solution
Knowing the cause, the solution is simple: convert the corresponding numeric columns in the transposed DataFrame to numeric types. Here’s the code:
……Drawing Violin Plots with Seaborn
Introduction
A violin plot is used to display the distribution and probability density of multiple data groups. Similar to a box plot, it offers a better representation of data density. Violin plots are particularly useful when dealing with very large datasets that are difficult to display individually. Python’s Seaborn package makes it very convenient to create violin plots.
Parameters
The parameters corresponding to each position in a violin plot are shown above. The middle line represents the box plot data, specifically the 25th, 50th (median), and 75th percentiles. The thin lines indicate the 95% confidence interval.
Drawing Violin Plots with Seaborn
Single Variable Data
While a box plot would suffice for a single variable, a violin plot can certainly be used as well:
import seaborn as sns
sns.set(color_codes=True)
sns.set_style("white")
df = sns.load_dataset('iris')
sns.violinplot( y=df["sepal_length"] )