棒棒图就是条形图与散点图的结合
plt.hlines
重要的参数
y:绘图分y轴
xmin/xmax 条形图的起始点与终止点
color:条形的颜色
plt.scatter
重要的参数
x,y是横纵轴
s:散点的大小
c:散点的颜色
market:散点的形状
import numpy as np
import importlib as mpl
import pandas as pd
import seaborn as sns
import matplotlib.pyplot as plt
%matplotlib inline
import matplotlib.patches as patches #导入补丁库
#读取数据
df=pd.read_csv('mtcars.csv')
#数据的处理
x=df.loc[:,['mpg']]#提取想要的数据
df['mpg_z']=(x-x.mean())/x.std() #标准化的处理
#df['color']=['red' if x<0 else 'green' for x in df['mpg_z']] #生成颜色的标签列
df['color']='black'
df.sort_values('mpg_z',inplace=True)
df.reset_index(inplace=True,drop=True)
df.head()
#绘制简单的棒棒图
plt.figure(figsize=(12,14),dpi=60)
#绘制棒棒图
plt.hlines(y=df.index,xmin=0,xmax=df.mpg_z,color=df.color,alpha=0.4,linewidth=1)
plt.scatter(df.mpg_z,df.index,color=df.color,s=300,alpha=0.6)
#添加装饰
#plt.gca().set(ylabel='$Model$', xlabel='$Mileage$')
plt.xlabel('mileage',fontsize=15)
plt.xticks(fontsize=15)
plt.yticks(df.index, df.cars, fontsize=15)
plt.title('Diverging Bars of Car Mileage', fontdict={
'size':20})
plt.grid(linestyle='--', alpha=.5)
#plt.xlim(-2.5,2.5)
plt.show()
**