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("darkgrid") #s设立风格
#sns.set_style("white")
%matplotlib inline
#########基本的图形设计#############
###认识绘制饼图的函数### ax.pie/plt.pie #x:pie函数中为唯一必填参数,每个类别的计数 #autopct:设定写在扇贝上的百分比,默认为none,可以输入字符串,format函数,或者none #自动百分比
#代码解读
#数据准备
data = df['counts'] #需要输入的数据集
explode = [0,0,0,0,0,0.1,0]
#绘制图像
fig, ax = plt.subplots(figsize=(7, 7), dpi= 80)
wedges, texts, autotexts = ax.pie(x=data,
autopct=lambda x: "{:.2f}% ({:d})".format(x,int(x/100.*np.sum(data))), #匿名函数的欧米伽法
colors=plt.cm.Dark2.colors, #图形的颜色
startangle=150, #第一瓣扇叶从什么角度开始
explode=explode #扇叶与扇叶之间的距离
)
#装饰图像
categories = df['class'] #选取图例
ax.legend(categories #输入数据
, title="Vehicle Class" #图例的标题
, loc="center left"
, bbox_to_anchor=(1, 0, 0.5, 1) #还记得bbox_to_anchor的用法么?知识点
)
ax.set_title("Class of Vehicles: Pie Chart")
plt.setp(autotexts, size=12, weight=700, color="w" #####知识点,设置子体的
) #设置某个对象(Artist)的属性(Property)
plt.show()
%%cmd
conda install squarify
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
import squarify
#导入数据,准备数据
df_raw = pd.read_csv("mpg_ggplot2.csv")
df = df_raw.groupby('class').size().reset_index(name='counts')
#图例
labels = df.apply(lambda x: str(x[0]) + "\n (" + str(x[1]) + ")"
, axis=1 #对每一行进行apply操作
)
#输入图像的值(占比)
sizes = df['counts'].values.tolist()
#颜色
colors = [plt.cm.Spectral(i/float(len(labels))) for i in range(len(labels))]
#绘制图像
plt.figure(figsize=(12,8), dpi= 80)
squarify.plot(sizes=sizes, label=labels, color=colors, alpha=.8)
#装饰图像
plt.title('Treemap of Vechile Class')
plt.axis('off') #不显示坐标轴,还记得我们在绘制坡度图的时候如何设置不显示坐标轴么?当时我们全程是fig + ax的操作。
plt.show()
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("darkgrid")
#sns.set_style("white")
%matplotlib inline
#导入数据,准备数据
df_raw = pd.read_csv("mpg_ggplot2.csv")
df = df_raw.groupby('manufacturer').size().reset_index(name='counts')
#我们一直以来都使用光谱来进行取色,或者输入颜色的名字,还有其他方法么?
import random
all_colors = list(plt.cm.colors.cnames.keys())
random.seed(100) #设置随机数种子
n = df['manufacturer'].unique().__len__() #需要取出多少种颜色?
c = random.choices(all_colors, k=n) #random.choice,从一个列表中取出k个值
import random
#绘制图像
plt.figure(figsize=(16,10), dpi= 80)
plt.bar(df['manufacturer'] #横坐标
, df['counts'] #纵坐标
, color=c
, width=.5)
import random
#绘制图像
plt.figure(figsize=(16,10), dpi= 80)
plt.bar(df['manufacturer'] #横坐标
, df['counts'] #纵坐标
, color=c
, width=.5)
#柱状图上写上文字
for i, val in enumerate(df['counts'].values):
plt.text(i, val, float(val), horizontalalignment='center', verticalalignment='bottom', fontdict={'fontweight':500, 'size':12})
#装饰图像
plt.gca().set_xticklabels(df['manufacturer'], rotation=60, horizontalalignment= 'right')
plt.title("Number of Vehicles by Manaufacturers", fontsize=22)
plt.ylabel('# Vehicles')
plt.ylim(0, 45)
plt.show()