简化了matplotlib 不存在直方图
Seaborn是一个Python数据可视化库,它提供简单的代码,为统计探索和洞察创建优雅的可视化。Seaborn基于Matplotlib,但在几个方面改进了Matplotlib:
- Seaborn提供更具视觉吸引力的绘图风格和简洁的语法。
- Seaborn本身了解Pandas DataFrames,可以更轻松地直接从CSV绘制数据。
- Seaborn可以轻松地将包含许多行数据的Pandas DataFrames汇总到聚合图表中。
1.引入
import seaborn as sns
2.使用
首先使用pandas导入csv再使用seaborn绘图
例子:
import codecademylib3_seaborn
import pandas as pd
from matplotlib import pyplot as plt
import seaborn as sns
# Load results.csv here:
df = pd.read_csv('results.csv')
print(df)
sns.barplot(
data=df,
x='Gender',
y='Mean Satisfaction'
)
plt.show()
Gender | Mean Satisfaction | |
---|---|---|
0 | Male | 7.2 |
1 | Female | 8.1 |
2 | Non-binary | 6.8 |
3.聚合
自动将数据聚合了
例子:
import codecademylib3_seaborn
import pandas as pd
from matplotlib import pyplot as plt
import seaborn as sns
gradebook = pd.read_csv("gradebook.csv")
print(gradebook.head())
sns.barplot(data=gradebook,
x='assignment_name',
y='grade')
plt.show()
student | assignment_name | grade | |
---|---|---|---|
0 | Amy | Assignment 1 | 75 |
1 | Amy | Assignment 2 | 82 |
2 | Bob | Assignment 1 | 99 |
3 | Bob | Assignment 2 | 90 |
4 | Chris | Assignment 1 | 72 |
5 | Chris | Assignment 2 | 66 |
6 | Dan | Assignment 1 | 88 |
7 | Dan | Assignment 2 | 82 |