CS_Matplotlib_1 Tutorials

MatplotlIb

Jupiter lab 当中matplotlib图像显示选择和变量输出

#在里面显示
%matplotlib inline

#在外面显示
%matplotlib auto

# 输出一个cel中的全部变量
from IPython.core.interactiveshell import InteractiveShell
InteractiveShell.ast_node_interactivity = 'all'

散点图

#  画散点图
def plant_fig():
    plt.style.use('ggplot') # 好看的风格主题
    # [-1,1] 消极到积极 [0,1] 客观到主观
    plt.figure(figsize=(10,10))
    # 先是x轴,然后是y轴
    plt.plot(pre_train_data["polarity"][pre_train_data["star"] == 5],
         pre_train_data["subjectivity"][pre_train_data["star"] == 5], 'bo',label=5)  # bo是蓝色的O型
    
    plt.xlabel("polarity")
    plt.ylabel("subjectivity")
    plt.title('Sentence emotion')
    plt.legend()  # 图例
    plt.show()

plant_fig()
# print(pre_train_data["polarity"][pre_train_data["star"] == 5])

绘制子图

  创建子图首先获取一个fig对象,相当于一个画板,然后再在这个画板上用fig.add_subplot添加子图,fig.add_subplot(221)当中的221是2行2列当中的第1个子图

import matplotlib.pyplot as plt 
fig=plt.figure()

# 画第1个图:折线图
x=np.arange(1,100)
ax1=fig.add_subplot(221)
ax1.plot(x,x*x)

# 画第2个图:散点图
ax2=fig.add_subplot(222)
ax2.scatter(np.arange(0,10), np.random.rand(10))

# 画第3个图:饼图
ax3=fig.add_subplot(223)
ax3.pie(x=[15,30,45,10],labels=list('ABCD'),autopct='%.0f',explode=[0,0.05,0,0])

# 画第4个图:条形图
ax4=fig.add_subplot(224)
ax4.bar([20,10,30,25,15],[25,15,35,30,20],color='b')
plt.show()

在这里插入图片描述

fig,subs=plt.subplots(2,2)

# 画第1个图:折线图
x=np.arange(1,100)
subs[0][0].plot(x,x*x)

# 画第2个图:散点图
subs[0][1].scatter(np.arange(0,10), np.random.rand(10))

# 画第3个图:饼图
subs[1][0].pie(x=[15,30,45,10],labels=list('ABCD'),autopct='%.0f',explode=[0,0.05,0,0])

# 画第4个图:条形图
subs[1][1].bar([20,10,30,25,15],[25,15,35,30,20],color='b')
plt.show()

  再介绍一种比上面的方法更简洁的创建子图的方法
  plt.subplots()来更简洁的创建子图,plt.subplots()是一个函数,返回一个包含figureaxes对象的元组。因此,使用fig,ax = plt.subplots()将返回的元组分解为figax两个变量。fig就是画板对象,ax就是子图对象
  ax.flatten()把子图展开赋值给axes,axes[0]便是第一行第一列子图,axes[1]是第一行第二列…
cmap : colormaps 色彩图,用来改变绘制风格,查看官方文档,

n_samples = 100
 
datasets = [
    make_moons(n_samples=n_samples, noise=0.2, random_state=0),
    make_circles(n_samples=n_samples, noise=0.2, factor=0.5, random_state=1),
    make_blobs(n_samples=n_samples, centers=2, random_state=5),#分簇的数据集
    make_classification(n_samples=n_samples,n_features = 2,n_informative=2,n_redundant=0, random_state=5)
                #n_features:特征数,n_informative:带信息的特征数,n_redundant:不带信息的特征数
    ]
 
Kernel = ["linear","poly","rbf","sigmoid"]
 
fig1,ax1 = plt.subplots(nrows=2, ncols=2,figsize=(10,8))
axes1 = ax1.flatten()
#四个数据集分别是什么样子呢?
for id,(X,Y) in enumerate(datasets):
    axes1[id].scatter(X[:,0],X[:,1],c = Y,s=50, cmap = "rainbow")

plt.show()
fig1.savefig("./name1.png")

参考博客 plt.subplots()的使用

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值