本文介绍的是如何使用seaborn来绘制各种柱状图
个人很喜欢的一个Seaborn绘制的图形:
Seaborn是matplotlib的高级封装,所以matplotlib还是要同时导入:
In [1]:
import pandas as pd
import numpy as np
import matplotlib.pyplot as plt
import seaborn as sns
%matplotlib inline
sns.set_theme(style="whitegrid")
sns.set_style('darkgrid')
使用的是seaborn中内置的一份消费tips数据集:
In [2]:
tips = sns.load_dataset("tips")
tips.head()
In [3]:
x = ["A","B","C"]
y = [1, 2, 3]
sns.barplot(x, y)
plt.show()
绘制水平柱状图:
# 水平柱状图
x = ["A","B","C"]
y = [1, 2, 3]
sns.barplot(y, x)
plt.show()
In [14]:
x = ["A","B","C"]
y = [1, 2, 3]
fig = sns.barplot(x, y)
fig.set_title('title of seaborn')
plt.show()
In [5]:
# 通过DataFrame来指定
ax = sns.barplot(x="day", y="tip", data=tips)
plt.show()
实现的分组显示数据
In [6]:
ax = sns.barplot(x="day",
y="total_bill",
hue="sex",
data=tips)
In [7]:
ax = sns.barplot(x="total_bill",
y="day",
data=tips)
In [8]:
ax = sns.barplot(x="total_bill",
y="day",
# 添加order参数,指定顺序
order=["Sat","Fri","Sun","Thur"], # 自定义
data=tips)
In [9]:
ax = sns.barplot(x="size",
y="total_bill",
data=tips,
color="salmon",
saturation=.5)
In [10]:
ax = sns.barplot(x="size",
y="tip",
data=tips,
palette="Blues")
In [11]:
g = sns.catplot(x="sex",
y="total_bill",
hue="smoker",
col="time",
data=tips,
kind="bar",
height=4,
aspect=.7)
In [12]:
tips["weekend"] = tips["day"].isin(["Sat", "Sun"])
tips
Out[12]:
In [13]:
ax = sns.barplot(x="day",
y="tip",
hue="weekend",
data=tips,
dodge=False)
- END -
版权说明 : 本文为转载文章, 版权归原作者所有 版权申明
原文链接 : https://blog.csdn.net/junhongzhang/article/details/125611277
内容来源于网络,如有侵权,请联系作者删除!