1、加载数据
import pandas as pd
import seaborn as sns
import matplotlib.pyplot as plt
# 导入数据
df = pd.read_csv('E:/workspace/dataset/seaborn-data-master/tips.csv')
df.head()
2、基于seaborn的条形图
# 利用barplot函数快速绘制
sns.barplot(
x="total_bill",
y="day",
data=df,
estimator=sum,
errorbar=None,
color='#69b3a2')
plt.show()
3、基于matplotlib的条形图
group_tips = df.groupby('day')['total_bill'].sum().reset_index()
group_tips
# 利用bar函数快速绘制
plt.bar(group_tips.day, group_tips.total_bill)
plt.show()
4、绘制子图对比
import seaborn as sns
import matplotlib.pyplot as plt
import numpy as np
sns.set(font='SimHei', font_scale=0.8, style="darkgrid") # 解决Seaborn中文显示问题
# 构造子图
fig, ax = plt.subplots(2,2,constrained_layout=True, figsize=(8, 8))
# 修改方向-垂直
ax_sub = sns.barplot(
y="total_bill",
x="day",
data=df,
estimator=sum,
errorbar=None,
color='#69b3a2',
ax=ax[0][0]
)
ax_sub.set_title('垂直条形图')
# 自定义排序
ax_sub = sns.barplot(
y="total_bill",
x="day",
data=df,
estimator=sum,
errorbar=None,
color='#69b3a2',
order=["Fri","Thur","Sat","Sun"],
ax=ax[0][1]
)
ax_sub.set_title('自定义排序')
# 数值排序
df2 = df.groupby('day')['total_bill'].sum().sort_values(ascending=False).reset_index()
ax_sub = sns.barplot(
y="day",
x="total_bill",
data=df,
errorbar=None,
color='#69b3a2',
order=df2['day'],
ax=ax[1][0]
)
ax_sub.set_title('数值排序')
# 添加误差线
ax_sub = sns.barplot(
x="day",
y="total_bill",
data=df,
estimator=np.mean,
errorbar=('ci', 85),
capsize=.2,
color='lightblue',
ax=ax[1][1]
)
ax_sub.set_title('添加误差线')
plt.show()
5、分组条形图
import seaborn as sns
import matplotlib.pyplot as plt
import numpy as np
sns.set(style="darkgrid")
fig, ax = plt.subplots(figsize=(4, 4))
# 分组条形图
colors = ["#69b3a2", "#4374B3"]
sns.barplot(x="day", y="total_bill", hue="smoker", data=df, errorbar=None, palette=colors)
plt.show()
# 分组/子分组条形图
sns.catplot(x="sex", y="total_bill", hue="smoker", col="day", data=df, kind="bar", height=4, aspect=.7)
plt.show()
6、数量堆积图
import seaborn as sns
import numpy as np
import matplotlib.pyplot as plt
import matplotlib.patches as mpatches
sns.set(style="darkgrid")
df2 = df.groupby(['day', 'smoker'])['total_bill'].sum().reset_index()
df_smoker = df2[df2['smoker']=='Yes']
df_non_smoker = df2[df2['smoker']=='No']
# 布局
plt.figure(figsize=(6, 4))
# 非吸烟者的条形图
bar1 = sns.barplot(x='day', y='total_bill', data=df_non_smoker, color='lightblue')
# 吸烟者的条形图,底部开始位置设置为非吸烟者的total_bill值(即吸烟者条形图在上面)
bar2 = sns.barplot(x='day', y='total_bill', bottom=df_non_smoker['total_bill'], data=df_smoker, color='darkblue')
# 图例
top_bar = mpatches.Patch(color='darkblue', label='smoker = Yes')
bottom_bar = mpatches.Patch(color='lightblue', label='smoker = No')
plt.legend(handles=[top_bar, bottom_bar])
plt.show()
7、基于matplotlib子图对比
import matplotlib as mpl
import matplotlib.pyplot as plt
import numpy as np
import pandas as pd
mpl.rcParams.update(mpl.rcParamsDefault) # 恢复默认的matplotlib样式
plt.rcParams['font.sans-serif'] = ['SimHei'] # 用来正常显示中文标签
# 自定义数据
height = [3, 12, 5, 18, 45]
bars = ('A', 'B', 'C', 'D', 'E')
y_pos = np.arange(len(bars))
x_pos = np.arange(len(bars))
# 初始化布局
fig = plt.figure(figsize=(8,8))
# 水平方向-水平条形图
plt.subplot(3, 3, 1)
plt.barh(y_pos, height)
plt.yticks(y_pos, bars)
plt.title('水平条形图')
# 指定顺序
height_order, bars_order = zip(*sorted(zip(height, bars), reverse=False)) # 自定义顺序
plt.subplot(3, 3, 2)
plt.barh(y_pos, height_order)
plt.yticks(y_pos, bars_order)
plt.title('指定顺序')
# 自定义颜色
plt.subplot(3, 3, 3)
plt.bar(x_pos, height, color=['black', 'red', 'green', 'blue', 'cyan'])
plt.xticks(x_pos, bars)
plt.title('自定义颜色')
# 自定义颜色-边框颜色
plt.subplot(3, 3, 4)
plt.bar(x_pos, height, color=(0.1, 0.1, 0.1, 0.1), edgecolor='blue')
plt.xticks(x_pos, bars)
plt.title('自定义边框颜色')
# 控制距离
width = [0.1, 0.2, 3, 1.5, 0.3]
x_pos_width = [0, 0.3, 2, 4.5, 5.5]
plt.subplot(3, 3, 5)
plt.bar(x_pos_width, height, width=width)
plt.xticks(x_pos_width, bars)
plt.title('控制距离')
# 控制宽度
x_pos_space = [0, 1, 5, 8, 9]
plt.subplot(3, 3, 6)
plt.bar(x_pos_space, height)
plt.xticks(x_pos_space, bars)
plt.title('控制宽度')
# 自定义布局
plt.subplot(3, 3, 7)
plt.bar(x_pos, height)
plt.xticks(x_pos, bars, color='orange', rotation=90) # 自定义x刻度名称颜色,自定义旋转
plt.xlabel('category', fontweight='bold', color = 'orange', fontsize='18') # 自定义x标签
plt.yticks(color='orange') # 自定义y刻度名称颜色
plt.title('自定义布局')
# 添加误差线
err = [val * 0.1 for val in height] # 计算误差(这里假设误差为height的10%)
plt.subplot(3, 3, 8)
plt.bar(x_pos, height, yerr=err, alpha=0.5, ecolor='black', capsize=10)
plt.xticks(x_pos, bars)
plt.title('添加误差线')
# 增加数值文本信息
plt.subplot(3, 3, 9)
ax = plt.bar(x_pos, height)
for bar in ax:
yval = bar.get_height()
plt.text(bar.get_x()+bar.get_width()/2.0, yval, int(yval), va='bottom') # va参数代表垂直对齐方式: 'top', 'bottom', 'center', 'baseline', 'center_baseline'
plt.xticks(x_pos, bars)
plt.title('增加数值文本信息')
fig.tight_layout() # 自动调整间距
plt.show()