先来复习一下之前的内容:
有效图表的重要特征:
- 不歪曲事实,表达准确
- 设计简单,容易理解
- 图表不会因为过度美化而将信息模糊
- 避免超载信息
准备工作
先要安装python以及两个库matplotlib和seaborn,还有两个操作数据的库numpy和pandas
pip install matplotlib
pip install seaborn
pip install numpy
pip install pandas
绘图前的配
# !pip install brewer2mpl
import numpy as np
import pandas as pd
import matplotlib as mpl
import matplotlib.pyplot as plt
import seaborn as sns
import warnings; warnings.filterwarnings(action='once')
large = 22; med = 16; small = 12
params = {'axes.titlesize': large,
'legend.fontsize': med,
'figure.figsize': (16, 10),
'axes.labelsize': med,
'axes.titlesize': med,
'xtick.labelsize': med,
'ytick.labelsize': med,
'figure.titlesize': large}
plt.rcParams.update(params)
plt.style.use('seaborn-whitegrid')
sns.set_style("white")
%matplotlib inline
# 版本要求
print(mpl.__version__) #> 3.0.0
print(sns.__version__) #> 0.9.0
顺序对比图的核心思想
这种图通常用于可视化一个数据的顺序信息,对人审美友好的同时可以精准的看出差异。
1.有序条形图
有序条形图有效地传达了项目的排名顺序。在图上方添加度量标准的值,看图者可以从图表本身获取精确信息。
# 准备数据
df_raw = pd.read_csv("https://github.com/selva86/datasets/raw/master/mpg_ggplot2.csv")
df = df_raw[['cty', 'manufacturer']].groupby('manufacturer').apply(lambda x: x.mean())
df.sort_values('cty', inplace=True)
df.reset_index(inplace=True)
# 绘图
import matplotlib.patches as patches
fig, ax = plt.subplots(figsize=(16,10), facecolor='white', dpi= 80)
ax.vlines(x=df.index, ymin=0, ymax=df.cty, color='firebrick', alpha=0.7, linewidth=20)
# 注释
for i, cty in enumerate(df.cty):
ax.text(i, cty+0.5, round(cty, 1), horizontalalignment='center')
# 标题,标签,标识和y轴最大小值
ax.set_title('Bar Chart for Highway Mileage', fontdict={'size':22})
ax.set(ylabel='Miles Per Gallon', ylim=(0, 30))
plt.xticks(df.index, df.manufacturer.str.upper(), rotation=60, horizontalalignment='right', fontsize=12)
# 修饰X轴
p1 = patches.Rectangle((.57, -0.005), width=.33, height=.13, alpha=.1, facecolor='green', transform=fig.transFigure)
p2 = patches.Rectangle((.124, -0.005), width=.446, height=.13, alpha=.1, facecolor='red', transform=fig.transFigure)
fig.add_artist(p1)
fig.add_artist(p2)
plt.show()
2.棒棒糖图
此图和上图的核心思想一致,只是表达方式不同。
df_raw = pd.read_csv("https://github.com/selva86/datasets/raw/master/mpg_ggplot2.csv")
df = df_raw[['cty', 'manufacturer']].groupby('manufacturer').apply(lambda x: x.mean())
df.sort_values('cty', inplace=True)
df.reset_index(inplace=True)
fig, ax = plt.subplots(figsize=(16,10), dpi= 80)
ax.vlines(x=df.index, ymin=0, ymax=df.cty, color='firebrick', alpha=0.7, linewidth=2)
ax.scatter(x=df.index, y=df.cty, s=75, color='firebrick', alpha=0.7)
ax.set_title('Lollipop Chart for Highway Mileage', fontdict={'size':22})
ax.set_ylabel('Miles Per Gallon')
ax.set_xticks(df.index)
ax.set_xticklabels(df.manufacturer.str.upper(), rotation=60, fontdict={'horizontalalignment': 'right', 'size':12})
ax.set_ylim(0, 30)
for row in df.itertuples():
ax.text(row.Index, row.cty+.5, s=round(row.cty, 2), horizontalalignment= 'center', verticalalignment='bottom', fontsize=14)
plt.show()
3.点序图
点序图传达了项目的排名顺序。 由于它沿水平轴对齐,因此可以更容易地看到点彼此之间的距离。
df_raw = pd.read_csv("1.txt")
df = df_raw[['cty', 'manufacturer']].groupby('manufacturer').apply(lambda x: x.mean())
df.sort_values('cty', inplace=True)
df.reset_index(inplace=True)
fig, ax = plt.subplots(figsize=(16,10), dpi= 80)
ax.hlines(y=df.index, xmin=11, xmax=26, color='gray', alpha=0.7, linewidth=1, linestyles='dashdot')
ax.scatter(y=df.index, x=df.cty, s=75, color='firebrick', alpha=0.7)
ax.set_title('Dot Plot for Highway Mileage', fontdict={'size':22})
ax.set_xlabel('Miles Per Gallon')
ax.set_yticks(df.index)
ax.set_yticklabels(df.manufacturer.str.title(), fontdict={'horizontalalignment': 'right'})
ax.set_xlim(10, 27)
plt.show()
值得一提的是,它的数据是这样:
"manufacturer","model","displ","year","cyl","trans","drv","cty","hwy","fl","class"
"audi","a4",1.8,1999,4,"auto(l5)","f",18,29,"p","compact"
"audi","a4",1.8,1999,4,"manual(m5)","f",21,29,"p","compact"
"audi","a4",2,2008,4,"manual(m6)","f",20,31,"p","compact"
"audi","a4",2,2008,4,"auto(av)","f",21,30,"p","compact"
"audi","a4",2.8,1999,6,"auto(l5)","f",16,26,"p","compact"
"audi","a4",2.8,1999,6,"manual(m5)","f",18,26,"p","compact"
"audi","a4",3.1,2008,6,"auto(av)","f",18,27,"p","compact"
"audi","a4 quattro",1.8,1999,4,"manual(m5)","4",18,26,"p","compact"
"audi","a4 quattro",1.8,1999,4,"auto(l5)","4",16,25,"p","compact"
"audi","a4 quattro",2,2008,4,"manual(m6)","4",20,28,"p","compact"
"audi","a4 quattro",2,2008,4,"auto(s6)","4",19,27,"p","compact"
"audi","a4 quattro",2.8,1999,6,"auto(l5)","4",15,25,"p","compact"
"audi","a4 quattro",2.8,1999,6,"manual(m5)","4",17,25,"p","compact"
"audi","a4 quattro",3.1,2008,6,"auto(s6)","4",17,25,"p","compact"
"audi","a4 quattro",3.1,2008,6,"manual(m6)","4",15,25,"p","compact"
"audi","a6 quattro",2.8,1999,6,"auto(l5)","4",15,24,"p","midsize"
"audi","a6 quattro",3.1,2008,6,"auto(s6)","4",17,25,"p","midsize"
"audi","a6 quattro",4.2,2008,8,"auto(s6)","4",16,23,"p","midsize"
"chevrolet","c1500 suburban 2wd",5.3,2008,8,"auto(l4)","r",14,20,"r","suv"
"chevrolet","c1500 suburban 2wd",5.3,2008,8,"auto(l4)","r",11,15,"e","suv"
"chevrolet","c1500 suburban 2wd",5.3,2008,8,"auto(l4)","r",14,20,"r","suv"
"chevrolet","c1500 suburban 2wd",5.7,1999,8,"auto(l4)","r",13,17,"r","suv"
"chevrolet","c1500 suburban 2wd",6,2008,8,"auto(l4)","r",12,17,"r","suv"
"chevrolet","corvette",5.7,1999,8,"manual(m6)","r",16,26,"p","2seater"
"chevrolet","corvette",5.7,1999,8,"auto(l4)","r",15,23,"p","2seater"
"chevrolet","corvette",6.2,2008,8,"manual(m6)","r",16,26,"p","2seater"
"chevrolet","corvette",6.2,2008,8,"auto(s6)","r",15,25,"p","2seater"
"chevrolet","corvette",7,2008,8,"manual(m6)","r",15,24,"p","2seater"
"chevrolet","k1500 tahoe 4wd",5.3,2008,8,"auto(l4)","4",14,19,"r","suv"
"chevrolet","k1500 tahoe 4wd",5.3,2008,8,"auto(l4)","4",11,14,"e","suv"
"chevrolet","k1500 tahoe 4wd",5.7,1999,8,"auto(l4)","4",11,15,"r","suv"
"chevrolet","k1500 tahoe 4wd",6.5,1999,8,"auto(l4)","4",14,17,"d","suv"
"chevrolet","malibu",2.4,1999,4,"auto(l4)","f",19,27,"r","midsize"
"chevrolet","malibu",2.4,2008,4,"auto(l4)","f",22,30,"r","midsize"
"chevrolet","malibu",3.1,1999,6,"auto(l4)","f",18,26,"r","midsize"
"chevrolet","malibu",3.5,2008,6,"auto(l4)","f",18,29,"r","midsize"
"chevrolet","malibu",3.6,2008,6,"auto(s6)","f",17,26,"r","midsize"
"dodge","caravan 2wd",2.4,1999,4,"auto(l3)","f",18,24,"r","minivan"
"dodge","caravan 2wd",3,1999,6,"auto(l4)","f",17,24,"r","minivan"
"dodge","caravan 2wd",3.3,1999,6,"auto(l4)","f",16,22,"r","minivan"
"dodge","caravan 2wd",3.3,1999,6,"auto(l4)","f",16,22,"r","minivan"
"dodge","caravan 2wd",3.3,2008,6,"auto(l4)","f",17,24,"r","minivan"
"dodge","caravan 2wd",3.3,2008,6,"auto(l4)","f",17,24,"r","minivan"
"dodge","caravan 2wd",3.3,2008,6,"auto(l4)","f",11,17,"e","minivan"
"dodge","caravan 2wd",3.8,1999,6,"auto(l4)","f",15,22,"r","minivan"
"dodge","caravan 2wd",3.8,1999,6,"auto(l4)","f",15,21,"r","minivan"
"dodge","caravan 2wd",3.8,2008,6,"auto(l6)","f",16,23,"r","minivan"
"dodge","caravan 2wd",4,2008,6,"auto(l6)","f",16,23,"r","minivan"
"dodge","dakota pickup 4wd",3.7,2008,6,"manual(m6)","4",15,19,"r","pickup"
"dodge","dakota pickup 4wd",3.7,2008,6,"auto(l4)","4",14,18,"r","pickup"
"dodge","dakota pickup 4wd",3.9,1999,6,"auto(l4)","4",13,17,"r","pickup"
"dodge","dakota pickup 4wd",3.9,1999,6,"manual(m5)","4",14,17,"r","pickup"
"dodge","dakota pickup 4wd",4.7,2008,8,"auto(l5)","4",14,19,"r","pickup"
"dodge","dakota pickup 4wd",4.7,2008,8,"auto(l5)","4",14,19,"r","pickup"
"dodge","dakota pickup 4wd",4.7,2008,8,"auto(l5)","4",9,12,"e","pickup"
"dodge","dakota pickup 4wd",5.2,1999,8,"manual(m5)","4",11,17,"r","pickup"
"dodge","dakota pickup 4wd",5.2,1999,8,"auto(l4)","4",11,15,"r","pickup"
"dodge","durango 4wd",3.9,1999,6,"auto(l4)","4",13,17,"r","suv"
"dodge","durango 4wd",4.7,2008,8,"auto(l5)","4",13,17,"r","suv"
"dodge","durango 4wd",4.7,2008,8,"auto(l5)","4",9,12,"e","suv"
"dodge","durango 4wd",4.7,2008,8,"auto(l5)","4",13,17,"r","suv"
"dodge","durango 4wd",5.2,1999,8,"auto(l4)","4",11,16,"r","suv"
"dodge","durango 4wd",5.7,2008,8,"auto(l5)","4",13,18,"r","suv"
"dodge","durango 4wd",5.9,1999,8,"auto(l4)","4",11,15,"r","suv"
"dodge","ram 1500 pickup 4wd",4.7,2008,8,"manual(m6)","4",12,16,"r","pickup"
"dodge","ram 1500 pickup 4wd",4.7,2008,8,"auto(l5)","4",9,12,"e","pickup"
"dodge","ram 1500 pickup 4wd",4.7,2008,8,"auto(l5)","4",13,17,"r","pickup"
"dodge","ram 1500 pickup 4wd",4.7,2008,8,"auto(l5)","4",13,17,"r","pickup"
"dodge","ram 1500 pickup 4wd",4.7,2008,8,"manual(m6)","4",12,16,"r","pickup"
"dodge","ram 1500 pickup 4wd",4.7,2008,8,"manual(m6)","4",9,12,"e","pickup"
"dodge","ram 1500 pickup 4wd",5.2,1999,8,"auto(l4)","4",11,15,"r","pickup"
"dodge","ram 1500 pickup 4wd",5.2,1999,8,"manual(m5)","4",11,16,"r","pickup"
"dodge","ram 1500 pickup 4wd",5.7,2008,8,"auto(l5)","4",13,17,"r","pickup"
"dodge","ram 1500 pickup 4wd",5.9,1999,8,"auto(l4)","4",11,15,"r","pickup"
"ford","expedition 2wd",4.6,1999,8,"auto(l4)","r",11,17,"r","suv"
"ford","expedition 2wd",5.4,1999,8,"auto(l4)","r",11,17,"r","suv"
"ford","expedition 2wd",5.4,2008,8,"auto(l6)","r",12,18,"r","suv"
"ford","explorer 4wd",4,1999,6,"auto(l5)","4",14,17,"r","suv"
"ford","explorer 4wd",4,1999,6,"manual(m5)","4",15,19,"r","suv"
"ford","explorer 4wd",4,1999,6,"auto(l5)","4",14,17,"r","suv"
"ford","explorer 4wd",4,2008,6,"auto(l5)","4",13,19,"r","suv"
"ford","explorer 4wd",4.6,2008,8,"auto(l6)","4",13,19,"r","suv"
"ford","explorer 4wd",5,1999,8,"auto(l4)","4",13,17,"r","suv"
"ford","f150 pickup 4wd",4.2,1999,6,"auto(l4)","4",14,17,"r","pickup"
"ford","f150 pickup 4wd",4.2,1999,6,"manual(m5)","4",14,17,"r","pickup"
"ford","f150 pickup 4wd",4.6,1999,8,"manual(m5)","4",13,16,"r","pickup"
"ford","f150 pickup 4wd",4.6,1999,8,"auto(l4)","4",13,16,"r","pickup"
"ford","f150 pickup 4wd",4.6,2008,8,"auto(l4)","4",13,17,"r","pickup"
"ford","f150 pickup 4wd",5.4,1999,8,"auto(l4)","4",11,15,"r","pickup"
"ford","f150 pickup 4wd",5.4,2008,8,"auto(l4)","4",13,17,"r","pickup"
"ford","mustang",3.8,1999,6,"manual(m5)","r",18,26,"r","subcompact"
"ford","mustang",3.8,1999,6,"auto(l4)","r",18,25,"r","subcompact"
"ford","mustang",4,2008,6,"manual(m5)","r",17,26,"r","subcompact"
"ford","mustang",4,2008,6,"auto(l5)","r",16,24,"r","subcompact"
"ford","mustang",4.6,1999,8,"auto(l4)","r",15,21,"r","subcompact"
"ford","mustang",4.6,1999,8,"manual(m5)","r",15,22,"r","subcompact"
"ford","mustang",4.6,2008,8,"manual(m5)","r",15,23,"r","subcompact"
"ford","mustang",4.6,2008,8,"auto(l5)","r",15,22,"r","subcompact"
"ford","mustang",5.4,2008,8,"manual(m6)","r",14,20,"p","subcompact"
"honda","civic",1.6,1999,4,"manual(m5)","f",28,33,"r","subcompact"
"honda","civic",1.6,1999,4,"auto(l4)","f",24,32,"r","subcompact"
"honda","civic",1.6,1999,4,"manual(m5)","f",25,32,"r","subcompact"
"honda","civic",1.6,1999,4,"manual(m5)","f",23,29,"p","subcompact"
"honda","civic",1.6,1999,4,"auto(l4)","f",24,32,"r","subcompact"
"honda","civic",1.8,2008,4,"manual(m5)","f",26,34,"r","subcompact"
"honda","civic",1.8,2008,4,"auto(l5)","f",25,36,"r","subcompact"
"honda","civic",1.8,2008,4,"auto(l5)","f",24,36,"c","subcompact"
"honda","civic",2,2008,4,"manual(m6)","f",21,29,"p","subcompact"
"hyundai","sonata",2.4,1999,4,"auto(l4)","f",18,26,"r","midsize"
"hyundai","sonata",2.4,1999,4,"manual(m5)","f",18,27,"r","midsize"
"hyundai","sonata",2.4,2008,4,"auto(l4)","f",21,30,"r","midsize"
"hyundai","sonata",2.4,2008,4,"manual(m5)","f",21,31,"r","midsize"
"hyundai","sonata",2.5,1999,6,"auto(l4)","f",18,26,"r","midsize"
"hyundai","sonata",2.5,1999,6,"manual(m5)","f",18,26,"r","midsize"
"hyundai","sonata",3.3,2008,6,"auto(l5)","f",19,28,"r","midsize"
"hyundai","tiburon",2,1999,4,"auto(l4)","f",19,26,"r","subcompact"
"hyundai","tiburon",2,1999,4,"manual(m5)","f",19,29,"r","subcompact"
"hyundai","tiburon",2,2008,4,"manual(m5)","f",20,28,"r","subcompact"
"hyundai","tiburon",2,2008,4,"auto(l4)","f",20,27,"r","subcompact"
"hyundai","tiburon",2.7,2008,6,"auto(l4)","f",17,24,"r","subcompact"
"hyundai","tiburon",2.7,2008,6,"manual(m6)","f",16,24,"r","subcompact"
"hyundai","tiburon",2.7,2008,6,"manual(m5)","f",17,24,"r","subcompact"
"jeep","grand cherokee 4wd",3,2008,6,"auto(l5)","4",17,22,"d","suv"
"jeep","grand cherokee 4wd",3.7,2008,6,"auto(l5)","4",15,19,"r","suv"
"jeep","grand cherokee 4wd",4,1999,6,"auto(l4)","4",15,20,"r","suv"
"jeep","grand cherokee 4wd",4.7,1999,8,"auto(l4)","4",14,17,"r","suv"
"jeep","grand cherokee 4wd",4.7,2008,8,"auto(l5)","4",9,12,"e","suv"
"jeep","grand cherokee 4wd",4.7,2008,8,"auto(l5)","4",14,19,"r","suv"
"jeep","grand cherokee 4wd",5.7,2008,8,"auto(l5)","4",13,18,"r","suv"
"jeep","grand cherokee 4wd",6.1,2008,8,"auto(l5)","4",11,14,"p","suv"
"land rover","range rover",4,1999,8,"auto(l4)","4",11,15,"p","suv"
"land rover","range rover",4.2,2008,8,"auto(s6)","4",12,18,"r","suv"
"land rover","range rover",4.4,2008,8,"auto(s6)","4",12,18,"r","suv"
"land rover","range rover",4.6,1999,8,"auto(l4)","4",11,15,"p","suv"
"lincoln","navigator 2wd",5.4,1999,8,"auto(l4)","r",11,17,"r","suv"
"lincoln","navigator 2wd",5.4,1999,8,"auto(l4)","r",11,16,"p","suv"
"lincoln","navigator 2wd",5.4,2008,8,"auto(l6)","r",12,18,"r","suv"
"mercury","mountaineer 4wd",4,1999,6,"auto(l5)","4",14,17,"r","suv"
"mercury","mountaineer 4wd",4,2008,6,"auto(l5)","4",13,19,"r","suv"
"mercury","mountaineer 4wd",4.6,2008,8,"auto(l6)","4",13,19,"r","suv"
"mercury","mountaineer 4wd",5,1999,8,"auto(l4)","4",13,17,"r","suv"
"nissan","altima",2.4,1999,4,"manual(m5)","f",21,29,"r","compact"
"nissan","altima",2.4,1999,4,"auto(l4)","f",19,27,"r","compact"
"nissan","altima",2.5,2008,4,"auto(av)","f",23,31,"r","midsize"
"nissan","altima",2.5,2008,4,"manual(m6)","f",23,32,"r","midsize"
"nissan","altima",3.5,2008,6,"manual(m6)","f",19,27,"p","midsize"
"nissan","altima",3.5,2008,6,"auto(av)","f",19,26,"p","midsize"
"nissan","maxima",3,1999,6,"auto(l4)","f",18,26,"r","midsize"
"nissan","maxima",3,1999,6,"manual(m5)","f",19,25,"r","midsize"
"nissan","maxima",3.5,2008,6,"auto(av)","f",19,25,"p","midsize"
"nissan","pathfinder 4wd",3.3,1999,6,"auto(l4)","4",14,17,"r","suv"
"nissan","pathfinder 4wd",3.3,1999,6,"manual(m5)","4",15,17,"r","suv"
"nissan","pathfinder 4wd",4,2008,6,"auto(l5)","4",14,20,"p","suv"
"nissan","pathfinder 4wd",5.6,2008,8,"auto(s5)","4",12,18,"p","suv"
"pontiac","grand prix",3.1,1999,6,"auto(l4)","f",18,26,"r","midsize"
"pontiac","grand prix",3.8,1999,6,"auto(l4)","f",16,26,"p","midsize"
"pontiac","grand prix",3.8,1999,6,"auto(l4)","f",17,27,"r","midsize"
"pontiac","grand prix",3.8,2008,6,"auto(l4)","f",18,28,"r","midsize"
"pontiac","grand prix",5.3,2008,8,"auto(s4)","f",16,25,"p","midsize"
"subaru","forester awd",2.5,1999,4,"manual(m5)","4",18,25,"r","suv"
"subaru","forester awd",2.5,1999,4,"auto(l4)","4",18,24,"r","suv"
"subaru","forester awd",2.5,2008,4,"manual(m5)","4",20,27,"r","suv"
"subaru","forester awd",2.5,2008,4,"manual(m5)","4",19,25,"p","suv"
"subaru","forester awd",2.5,2008,4,"auto(l4)","4",20,26,"r","suv"
"subaru","forester awd",2.5,2008,4,"auto(l4)","4",18,23,"p","suv"
"subaru","impreza awd",2.2,1999,4,"auto(l4)","4",21,26,"r","subcompact"
"subaru","impreza awd",2.2,1999,4,"manual(m5)","4",19,26,"r","subcompact"
"subaru","impreza awd",2.5,1999,4,"manual(m5)","4",19,26,"r","subcompact"
"subaru","impreza awd",2.5,1999,4,"auto(l4)","4",19,26,"r","subcompact"
"subaru","impreza awd",2.5,2008,4,"auto(s4)","4",20,25,"p","compact"
"subaru","impreza awd",2.5,2008,4,"auto(s4)","4",20,27,"r","compact"
"subaru","impreza awd",2.5,2008,4,"manual(m5)","4",19,25,"p","compact"
"subaru","impreza awd",2.5,2008,4,"manual(m5)","4",20,27,"r","compact"
"toyota","4runner 4wd",2.7,1999,4,"manual(m5)","4",15,20,"r","suv"
"toyota","4runner 4wd",2.7,1999,4,"auto(l4)","4",16,20,"r","suv"
"toyota","4runner 4wd",3.4,1999,6,"auto(l4)","4",15,19,"r","suv"
"toyota","4runner 4wd",3.4,1999,6,"manual(m5)","4",15,17,"r","suv"
"toyota","4runner 4wd",4,2008,6,"auto(l5)","4",16,20,"r","suv"
"toyota","4runner 4wd",4.7,2008,8,"auto(l5)","4",14,17,"r","suv"
"toyota","camry",2.2,1999,4,"manual(m5)","f",21,29,"r","midsize"
"toyota","camry",2.2,1999,4,"auto(l4)","f",21,27,"r","midsize"
"toyota","camry",2.4,2008,4,"manual(m5)","f",21,31,"r","midsize"
"toyota","camry",2.4,2008,4,"auto(l5)","f",21,31,"r","midsize"
"toyota","camry",3,1999,6,"auto(l4)","f",18,26,"r","midsize"
"toyota","camry",3,1999,6,"manual(m5)","f",18,26,"r","midsize"
"toyota","camry",3.5,2008,6,"auto(s6)","f",19,28,"r","midsize"
"toyota","camry solara",2.2,1999,4,"auto(l4)","f",21,27,"r","compact"
"toyota","camry solara",2.2,1999,4,"manual(m5)","f",21,29,"r","compact"
"toyota","camry solara",2.4,2008,4,"manual(m5)","f",21,31,"r","compact"
"toyota","camry solara",2.4,2008,4,"auto(s5)","f",22,31,"r","compact"
"toyota","camry solara",3,1999,6,"auto(l4)","f",18,26,"r","compact"
"toyota","camry solara",3,1999,6,"manual(m5)","f",18,26,"r","compact"
"toyota","camry solara",3.3,2008,6,"auto(s5)","f",18,27,"r","compact"
"toyota","corolla",1.8,1999,4,"auto(l3)","f",24,30,"r","compact"
"toyota","corolla",1.8,1999,4,"auto(l4)","f",24,33,"r","compact"
"toyota","corolla",1.8,1999,4,"manual(m5)","f",26,35,"r","compact"
"toyota","corolla",1.8,2008,4,"manual(m5)","f",28,37,"r","compact"
"toyota","corolla",1.8,2008,4,"auto(l4)","f",26,35,"r","compact"
"toyota","land cruiser wagon 4wd",4.7,1999,8,"auto(l4)","4",11,15,"r","suv"
"toyota","land cruiser wagon 4wd",5.7,2008,8,"auto(s6)","4",13,18,"r","suv"
"toyota","toyota tacoma 4wd",2.7,1999,4,"manual(m5)","4",15,20,"r","pickup"
"toyota","toyota tacoma 4wd",2.7,1999,4,"auto(l4)","4",16,20,"r","pickup"
"toyota","toyota tacoma 4wd",2.7,2008,4,"manual(m5)","4",17,22,"r","pickup"
"toyota","toyota tacoma 4wd",3.4,1999,6,"manual(m5)","4",15,17,"r","pickup"
"toyota","toyota tacoma 4wd",3.4,1999,6,"auto(l4)","4",15,19,"r","pickup"
"toyota","toyota tacoma 4wd",4,2008,6,"manual(m6)","4",15,18,"r","pickup"
"toyota","toyota tacoma 4wd",4,2008,6,"auto(l5)","4",16,20,"r","pickup"
"volkswagen","gti",2,1999,4,"manual(m5)","f",21,29,"r","compact"
"volkswagen","gti",2,1999,4,"auto(l4)","f",19,26,"r","compact"
"volkswagen","gti",2,2008,4,"manual(m6)","f",21,29,"p","compact"
"volkswagen","gti",2,2008,4,"auto(s6)","f",22,29,"p","compact"
"volkswagen","gti",2.8,1999,6,"manual(m5)","f",17,24,"r","compact"
"volkswagen","jetta",1.9,1999,4,"manual(m5)","f",33,44,"d","compact"
"volkswagen","jetta",2,1999,4,"manual(m5)","f",21,29,"r","compact"
"volkswagen","jetta",2,1999,4,"auto(l4)","f",19,26,"r","compact"
"volkswagen","jetta",2,2008,4,"auto(s6)","f",22,29,"p","compact"
"volkswagen","jetta",2,2008,4,"manual(m6)","f",21,29,"p","compact"
"volkswagen","jetta",2.5,2008,5,"auto(s6)","f",21,29,"r","compact"
"volkswagen","jetta",2.5,2008,5,"manual(m5)","f",21,29,"r","compact"
"volkswagen","jetta",2.8,1999,6,"auto(l4)","f",16,23,"r","compact"
"volkswagen","jetta",2.8,1999,6,"manual(m5)","f",17,24,"r","compact"
"volkswagen","new beetle",1.9,1999,4,"manual(m5)","f",35,44,"d","subcompact"
"volkswagen","new beetle",1.9,1999,4,"auto(l4)","f",29,41,"d","subcompact"
"volkswagen","new beetle",2,1999,4,"manual(m5)","f",21,29,"r","subcompact"
"volkswagen","new beetle",2,1999,4,"auto(l4)","f",19,26,"r","subcompact"
"volkswagen","new beetle",2.5,2008,5,"manual(m5)","f",20,28,"r","subcompact"
"volkswagen","new beetle",2.5,2008,5,"auto(s6)","f",20,29,"r","subcompact"
"volkswagen","passat",1.8,1999,4,"manual(m5)","f",21,29,"p","midsize"
"volkswagen","passat",1.8,1999,4,"auto(l5)","f",18,29,"p","midsize"
"volkswagen","passat",2,2008,4,"auto(s6)","f",19,28,"p","midsize"
"volkswagen","passat",2,2008,4,"manual(m6)","f",21,29,"p","midsize"
"volkswagen","passat",2.8,1999,6,"auto(l5)","f",16,26,"p","midsize"
"volkswagen","passat",2.8,1999,6,"manual(m5)","f",18,26,"p","midsize"
"volkswagen","passat",3.6,2008,6,"auto(s6)","f",17,26,"p","midsize"
4.斜线图
斜线图最适合比较项目的“之前”和“之后”的变化。
import matplotlib.lines as mlines
# Import Data
df = pd.read_csv("https://raw.githubusercontent.com/selva86/datasets/master/gdppercap.csv")
left_label = [str(c) + ', '+ str(round(y)) for c, y in zip(df.continent, df['1952'])]
right_label = [str(c) + ', '+ str(round(y)) for c, y in zip(df.continent, df['1957'])]
klass = ['red' if (y1-y2) < 0 else 'green' for y1, y2 in zip(df['1952'], df['1957'])]
# draw line
# https://stackoverflow.com/questions/36470343/how-to-draw-a-line-with-matplotlib/36479941
def newline(p1, p2, color='black'):
ax = plt.gca()
l = mlines.Line2D([p1[0],p2[0]], [p1[1],p2[1]], color='red' if p1[1]-p2[1] > 0 else 'green', marker='o', markersize=6)
ax.add_line(l)
return l
fig, ax = plt.subplots(1,1,figsize=(14,14), dpi= 80)
ax.vlines(x=1, ymin=500, ymax=13000, color='black', alpha=0.7, linewidth=1, linestyles='dotted')
ax.vlines(x=3, ymin=500, ymax=13000, color='black', alpha=0.7, linewidth=1, linestyles='dotted')
ax.scatter(y=df['1952'], x=np.repeat(1, df.shape[0]), s=10, color='black', alpha=0.7)
ax.scatter(y=df['1957'], x=np.repeat(3, df.shape[0]), s=10, color='black', alpha=0.7)
for p1, p2, c in zip(df['1952'], df['1957'], df['continent']):
newline([1,p1], [3,p2])
ax.text(1-0.05, p1, c + ', ' + str(round(p1)), horizontalalignment='right', verticalalignment='center', fontdict={'size':14})
ax.text(3+0.05, p2, c + ', ' + str(round(p2)), horizontalalignment='left', verticalalignment='center', fontdict={'size':14})
ax.text(1-0.05, 13000, 'BEFORE', horizontalalignment='right', verticalalignment='center', fontdict={'size':18, 'weight':700})
ax.text(3+0.05, 13000, 'AFTER', horizontalalignment='left', verticalalignment='center', fontdict={'size':18, 'weight':700})
ax.set_title("Slopechart: Comparing GDP Per Capita between 1952 vs 1957", fontdict={'size':22})
ax.set(xlim=(0,4), ylim=(0,14000), ylabel='Mean GDP Per Capita')
ax.set_xticks([1,3])
ax.set_xticklabels(["1952", "1957"])
plt.yticks(np.arange(500, 13000, 2000), fontsize=12)
plt.gca().spines["top"].set_alpha(.0)
plt.gca().spines["bottom"].set_alpha(.0)
plt.gca().spines["right"].set_alpha(.0)
plt.gca().spines["left"].set_alpha(.0)
plt.show()