matplotlib学习笔记
程序员文章站
2022-03-19 23:38:15
...
import numpy as np
import matplotlib.pyplot as plt
# 在notebook中画图
%matplotlib inline
# 画图
plt.plot([1,2,3,4,5],[1,4,9,16,25],'--',color = 'm')
# 改x轴显示
plt.xticks(x,bar_label)
plt.show()
# 绘制多个线
n = np.arange(0,10,0.5)
plt.plot(n,n,'r--',n,n**2,'bs',n,n**3,'go')
# 指定宽度
x = np.linspace(-10,10)
y = np.sin(x)
plt.plot(x,y,linewidth = 2.5)
# 详细设置
plt.plot(x,y,color = 'b',linestyle = ':',marker = 'o',markerfacecolor = 'r',markersize = 6)
# 设置图的信息
line = plt.plot(x,y)
plt.setp(line,color = 'r',linewidth = 2.0,alpha = 0.5)
# 子图,同时显示两个图
# 211 一会要画的图2行1列,最后一个1表示子图中第一个图
plt.subplot(211)
plt.plot(x,y,color = 'r')
# 211 一会要画的图2行1列,最后一个1表示子图中第二个图
plt.subplot(212)
plt.plot(x,y,color = 'r')
# 加上各种注释和网格
plt.plot(x,y,color = 'b',linestyle = ':',marker = 'o',markerfacecolor = 'r',markersize = 6)
plt.xlabel('x:---')
plt.ylabel('y:---')
plt.title('table:---')
plt.text(0,0,'zeor')
plt.grid(True)
# xy指向的点,xytext起始点
plt.annotate('zhu shi',xy =(-2.5,-1) ,xytext = (0,0),arrowprops = dict(facecolor = 'black',shrink = 1))
# 设置xy轴不显示
x = range(10)
y = range(10)
fig = plt.gca()
plt.plot(x,y)
fig.axes.get_xaxis().set_visible(False)
fig.axes.get_yaxis().set_visible(False)
# 绘制直方图
import math
x = np.random.normal(loc = 0.0,scale=1.0,size=300)
width = 0.5
# math.floor - 返回向下舍去小数点后面的数
# math.ceil - 返回向上进后的数
# bins - 一个个小块,宽度为width,从x轴上最小到最大
bins = np.arange(math.floor(x.min())-width,math.ceil(x.max())+width,width)
ax = plt.subplot(111)
# 设置图的上面右面不可见
ax.spines['top'].set_visible(False)
ax.spines['right'].set_visible(False)
# 设置刻度不可见
plt.tick_params(bottom='off',top='off',left = 'off',right='off')
plt.grid()
# 绘制直方图
plt.hist(x,alpha = 0.5,bins = bins)
# 一旦坐标系上的标注太长不能写完全,就要改
x = range(10)
y = range(10)
labels = ['hexiaoyang' for i in range(10)]
fig,ax = plt.subplots()
plt.plot(x,y)
plt.title('tangyudi')
ax.set_xticklabels(labels,rotation = 45,horizontalalignment='right')
# 显示线的指标
x = np.arange(10)
for i in range(1,4):
plt.plot(x,i*x**2,label = 'Group %d'%i)
plt.legend(loc='best')
# 定位box
ax = plt.subplot(111)
x = np.arange(10)
for i in range(1,4):
plt.plot(x,i*x**2,label = 'Group %d'%i)
# bbox_to_anchor - 定位存放指标的box的位置
ax.legend(loc='upper center',bbox_to_anchor = (0.5,1.15) ,ncol=3)
# 将线变为点型
x = np.arange(10)
for i in range(1,4):
plt.plot(x,i*x**2,label = 'Group %d'%i,marker='o')
plt.legend(loc='upper right',framealpha = 0.1)
# 修改全局变量
import matplotlib as mpl
mpl.rcParams['axes.titlesize'] = '10'
# 设置x/y轴
plt.xlabel('xlabel',fontsize = 14) # 加标签
plt.ylabel('ylabel',fontsize = 14) # 加标签
# 风格设置
ply.style.available
x = np.linspace(-10,10)
y = np.sin(x)
ply.plot(x,y)
# 一
ply.style.use(['ggplot','bmh'])
ply.plot(x,y)
# 二
ply.xkcd()
ply.plot(x,y)
# 条形图
np.random.seed(0)
x = np.arange(5)
y = np.random.randint(-5,5,5)
# axes轴
fig,axes = plt.subplots(ncols = 2)
v_bars = axes[0].bar(x,y,color = 'red')
h_bars = axes[1].barh(x,y,color = 'red')
# 在x=0处加线
axes[0].axhline(0,color = 'grey',linewidth = 2)
axes[1].axvline(0,color = 'grey',linewidth = 2)
plt.show()
# fig - 画布 ax - 图
fig,ax = plt.subplots()
v_bars = ax.bar(x,y,color = 'lightblue')
for bar,height in zip(v_bars,y):
if height < 0:
bar.set(edgecolor = 'darkred',color = 'green',linewidth = 3)
plt.show()
# 填充折线图
x = np.random.randn(100).cumsum()
y = np.linspace(0,10,100)
fig,ax = plt.subplots()
ax.fill_between(x,y,color = 'lightblue')
ax.plot(x,y,color = 'black')
plt.show()
# 填充折线图
x = np.linspace(0,10,200)
y1 = 2*x + 1
y2 = 3*x + 1.2
y_mean = 0.5*x*np.cos(2*x) + 2.5*x + 1.1
fig,ax = plt.subplots()
# x轴为底,填充y1到y2之间,用红色填充
ax.fill_between(x,y1,y2,color = 'red')
# 折线图用黑色
ax.plot(x,y_mean,color = 'black')
plt.show()
# 误差范围
v = [1,2,3]
variance = [0.2,0.4,0.5]
bar_label = ['bar1','bar2','bar3']
x = list(range(len(bar_label)))
# yerr误差范围
plt.bar(x,v,yerr = variance,alpha = 0.5)
y = max(zip(v,variance))
plt.ylim([0,(y[0]+y[1])*1.2])
plt.ylabel('variable y')
# 三个条形图
green_data = [1, 2, 3]
blue_data = [3, 2, 1]
red_data = [2, 3, 3]
labels = ['group 1', 'group 2', 'group 3']
pos = list(range(len(green_data)))
width = 0.2
fig, ax = plt.subplots(figsize=(5,5))
plt.bar(pos,green_data,width,alpha = 0.5,color = 'g',label = labels[0])
plt.bar([p+width for p in pos],blue_data,width,alpha = 0.5,color = 'b',label = labels[1])
plt.bar([p+width*2 for p in pos],red_data,width,alpha = 0.5,color = 'r',label = labels[2])
plt.legend()
plt.show()
# 加垂直分割线
data = range(200, 225, 5)
bar_labels = ['a', 'b', 'c', 'd', 'e']
fig = plt.figure(figsize=(10,8))
y_pos = np.arange(len(data))
plt.yticks(y_pos, bar_labels, fontsize=16)
bars = plt.barh(y_pos,data,alpha = 0.5,color='g')
# 垂直分割线
# vlines(x起点,y起点,y终点,线风格)
plt.vlines(min(data),-1,len(data),linestyle = 'dashed')
# bars代表条形
# plt.text(x坐标,y坐标,标注)
for b,d in zip(bars,data):
plt.text(b.get_width()+b.get_width()*0.05,b.get_y()+b.get_height()/2,'{0:.2%}'.format(d/min(data)))
plt.show()
# 盒图
tang_data = [np.random.normal(0,std,100) for std in range(1,4)]
fig = plt.figure(figsize = (8,6))
# notch两种不同形状
# sym方块还是圆圈
# vert 横竖
plt.boxplot(tang_data,notch=False,sym='s',vert=True)
plt.xticks([y+1 for y in range(len(tang_data))],['x1','x2','x3'])
plt.xlabel('x')
plt.title('box plot')
# 遍历盒子的原件索引
for components in bplot.keys():
# 选择对应的原件变颜色
for line in bplot[components]:
line.set_color('black')
# 填充颜色
tang_data = [np.random.normal(0,std,100) for std in range(1,4)]
fig = plt.figure(figsize = (8,6))
# patch_artist - 改颜色
bplot = plt.boxplot(tang_data,notch=False,sym='s',vert=True,patch_artist=True)
plt.xticks([y+1 for y in range(len(tang_data))],['x1','x2','x3'])
plt.xlabel('x')
plt.title('box plot')
colors = ['pink','lightblue','lightgreen']
for pathch,color in zip(bplot['boxes'],colors):
pathch.set_facecolor(color)
# 小提琴图
fig,axes = plt.subplots(nrows=1,ncols=2,figsize=(12,5))
tang_data = [np.random.normal(0,std,100) for std in range(6,10)]
axes[0].violinplot(tang_data,showmeans=False,showmedians=True)
axes[0].set_title('violin plot')
axes[1].boxplot(tang_data)
axes[1].set_title('box plot')
# setp该命令可以对一个列表或者单个对象进行设置
for ax in axes:
ax.yaxis.grid(True)
ax.set_xticks([y+1 for y in range(len(tang_data))])
plt.setp(axes,xticks=[y+1 for y in range(len(tang_data))],xticklabels=['x1','x2','x3','x4'])
# 直方图
data = np.random.normal(0,20,1000)
bins = np.arange(-100,100,5)
plt.hist(data,bins=bins)
plt.xlim([min(data)-5,max(data)+5])
plt.show()
# 两个直方图
import random
data1 = [random.gauss(15,10) for i in range(500)]
data2 = [random.gauss(5,5) for i in range(500)]
bins = np.arange(-50,50,2.5)
plt.hist(data1,bins=bins,label='class 1',alpha = 0.3)
plt.hist(data2,bins=bins,label='class 2',alpha = 0.3)
plt.legend(loc='best')
plt.show()
# 散点图
mu_vec1 = np.array([0,0])
cov_mat1 = np.array([[2,0],[0,2]])
# multivariate_normal - 依据指定的均值和协方差生成数据
x1_samples = np.random.multivariate_normal(mu_vec1, cov_mat1, 100)
x2_samples = np.random.multivariate_normal(mu_vec1+0.2, cov_mat1+0.2, 100)
x3_samples = np.random.multivariate_normal(mu_vec1+0.4, cov_mat1+0.4, 100)
plt.figure(figsize = (8,6))
plt.scatter(x1_samples[:,0],x1_samples[:,1],marker ='x',color='blue',alpha=0.6,label='x1')
plt.scatter(x2_samples[:,0],x2_samples[:,1],marker ='o',color='red',alpha=0.6,label='x2')
plt.scatter(x3_samples[:,0],x3_samples[:,1],marker ='^',color='green',alpha=0.6,label='x3')
plt.legend(loc='best')
plt.show()
# 散点图
x_coords = [0.13, 0.22, 0.39, 0.59, 0.68, 0.74, 0.93]
y_coords = [0.75, 0.34, 0.44, 0.52, 0.80, 0.25, 0.55]
plt.figure(figsize = (8,6))
# s为大小size
plt.scatter(x_coords,y_coords,marker='s',s=50)
for x,y in zip(x_coords,y_coords):
# 第一个参数为需要加的注释,第二个写的点,第三个是定位注释位置,textcoords - 为显示注释,ha为对齐
plt.annotate('(%s,%s)'%(x,y),xy=(x,y),xytext=(0,-15),textcoords = 'offset points',ha='center')
plt.show()
# 按大小不同规范
mu_vec1 = np.array([0,0])
cov_mat1 = np.array([[1,0],[0,1]])
X = np.random.multivariate_normal(mu_vec1, cov_mat1, 500)
fig = plt.figure(figsize=(8,6))
R=X**2
R_sum=R.sum(axis = 1)
plt.scatter(X[:,0],X[:,1],color='grey',marker='o',s=20*R_sum,alpha=0.5)
plt.show()
# 3D图工具
from mpl_toolkits.mplot3d import Axes3D
# 3D图
fig = plt.figure()
ax = Axes3D(fig)
x = np.arange(-4,4,0.25)
y = np.arange(-4,4,0.25)
X,Y = np.meshgrid(x,y)
# sqrt - 开根号
Z = np.sin(np.sqrt(X**2+Y**2))
# rstride cstride - 格子密度
# cmap为颜色
ax.plot_surface(X,Y,Z,rstride = 1,cstride = 1,cmap='rainbow')
# 投影 offset - 定位
ax.contour(X,Y,Z,zdim='z',offset = -2 ,cmap='rainbow')
ax.set_zlim(-2,2)
plt.show()
# 详细步骤
# 导包
import matplotlib.pyplot as plt
from mpl_toolkits.mplot3d import Axes3D
# fig画布/画面
fig = plt.figure()
ax = fig.add_subplot(111,projection = '3d')
plt.show()
# 线图
fig = plt.figure()
ax = fig.gca(projection='3d')
theta = np.linspace(-4 * np.pi, 4 * np.pi, 100)
z = np.linspace(-2, 2, 100)
r = z**2 + 1
x = r * np.sin(theta)
y = r * np.cos(theta)
ax.plot(x,y,z)
plt.show()
# 散点图
np.random.seed(1)
def randrange(n,vmin,vmax):
return (vmax-vmin)*np.random.rand(n)+vmin
# 得到画布
fig = plt.figure()
# 两种都可以
#ax = fig.gca(projection='3d')
ax = fig.add_subplot(111,projection = '3d')
n = 100
for c,m,zlow,zhigh in [('r','o',-50,-25),('b','x','-30','-5')]:
xs = randrange(n,23,32)
ys = randrange(n,0,100)
zs = randrange(n,int(zlow),int(zhigh))
ax.scatter(xs,ys,zs,c=c,marker=m)
# 转换视角
ax.view_init(40,0)
plt.show()
# 条形图
fig = plt.figure()
ax = fig.add_subplot(111, projection='3d')
for c, z in zip(['r', 'g', 'b', 'y'], [30, 20, 10, 0]):
xs = np.arange(20)
ys = np.random.rand(20)
# zdir - 图形是否是立起来的
ax.bar(xs,ys,zs = z,zdir='y',color = c,alpha = 0.5)
plt.show()
# pie图
m = 51212.
f = 40742.
m_perc = m/(m+f)
f_perc = f/(m+f)
colors = ['navy','lightcoral']
labels = ["Male","Female"]
plt.figure(figsize=(8,8))
# 统计的块之间的分离成都 - explode
paches,texts,autotexts = plt.pie([m_perc,f_perc],labels = labels,autopct = '%1.1f%%',explode=[0,0.05],colors = colors)
for text in texts+autotexts:
text.set_fontsize(20)
for text in autotexts:
text.set_color('white')
# 多图拼凑
ax1 = plt.subplot2grid((3,3),(0,0))
ax2 = plt.subplot2grid((3,3),(1,0))
ax3 = plt.subplot2grid((3,3),(0,2),rowspan=3)
ax4 = plt.subplot2grid((3,3),(2,0),colspan = 2)
ax5 = plt.subplot2grid((3,3),(0,1),rowspan=2)
# 多图拼凑
x = np.linspace(0,10,1000)
y2 = np.sin(x**2)
y1 = x**2
fig,ax1 = plt.subplots()
left,bottom,width,height = [0.22,0.45,0.3,0.35]
ax2 = fig.add_axes([left,bottom,width,height])
ax1.plot(x,y1)
ax2.plot(x,y2)
# 多图拼凑
import matplotlib.pyplot as plt
# 在子图1中画图需要的工具
from mpl_toolkits.axes_grid1.inset_locator import inset_axes
def autolabel(rects):
for rect in rects:
height = rect.get_height()
ax1.text(rect.get_x() + rect.get_width()/2., 1.02*height,
"{:,}".format(float(height)),
ha='center', va='bottom',fontsize=18)
top10_arrivals_countries = ['CANADA','MEXICO','UNITED\nKINGDOM',\
'JAPAN','CHINA','GERMANY','SOUTH\nKOREA',\
'FRANCE','BRAZIL','AUSTRALIA']
top10_arrivals_values = [16.625687, 15.378026, 3.934508, 2.999718,\
2.618737, 1.769498, 1.628563, 1.419409,\
1.393710, 1.136974]
arrivals_countries = ['WESTERN\nEUROPE','ASIA','SOUTH\nAMERICA',\
'OCEANIA','CARIBBEAN','MIDDLE\nEAST',\
'CENTRAL\nAMERICA','EASTERN\nEUROPE','AFRICA']
arrivals_percent = [36.9,30.4,13.8,4.4,4.0,3.6,2.9,2.6,1.5]
fig, ax1 = plt.subplots(figsize=(20,12))
tang = ax1.bar(range(10),top10_arrivals_values,color='blue')
plt.xticks(range(10),top10_arrivals_countries,fontsize=18)
ax2 = inset_axes(ax1,width = 6,height = 6,loc = 5)
explode = (0.08, 0.08, 0.05, 0.05,0.05,0.05,0.05,0.05,0.05)
patches, texts, autotexts = ax2.pie(arrivals_percent,labels=arrivals_countries,autopct='%1.1f%%',explode=explode)
for text in texts+autotexts:
text.set_fontsize(16)
for spine in ax1.spines.values():
spine.set_visible(False)
# 标注
autolabel(tang)
# pandas与sklearn结合
import pandas as pd
import numpy as np
import matplotlib.pyplot as pl
url = 'https://archive.ics.uci.edu/ml/machine-learning-databases/00383/risk_factors_cervical_cancer.csv'
df = pd.read_csv(url, na_values="?")
df.head()
# 填充缺失值
from sklearn.preprocessing import Imputer
impute = pd.DataFrame(Imputer().fit_transform(df))
impute.columns = df.columns
impute.index = df.index
impute.head()
# PCA - 主成分分析,找到主要的特征
import seaborn as sns
from sklearn.decomposition import PCA
from mpl_toolkits.mplot3d import Axes3D
features = impute.drop('Dx:Cancer', axis=1)
y = impute["Dx:Cancer"]
pca = PCA(n_components=3)
X_r = pca.fit_transform(features)
print("Explained variance:\nPC1 {:.2%}\nPC2 {:.2%}\nPC3 {:.2%}"
.format(pca.explained_variance_ratio_[0],
pca.explained_variance_ratio_[1],
pca.explained_variance_ratio_[2]))
fig = plt.figure()
ax = Axes3D(fig)
ax.scatter(X_r[:, 0], X_r[:, 1], X_r[:, 2], c=y, cmap=plt.cm.coolwarm)
# Label the axes
ax.set_xlabel('PC1')
ax.set_ylabel('PC2')
ax.set_zlabel('PC3')
plt.show()
上一篇: Eclipse开发PHP调试(debug)环境配置
下一篇: 这吐槽也忒惨了