机器学习:Matplotlib之其他函数

本文介绍了Python数据可视化库Matplotlib中的关键函数,包括figure用于创建画布,legend用于生成图例,xlabel和title分别用于设置横坐标标签和图形标题。示例展示了如何使用这些函数创建和定制图形,包括多子图的处理以及图形元素的详细标注。
摘要由CSDN通过智能技术生成

figure函数

figure函数:创建画布

plt.figure(num=None, figsize=None, dpi=None, facecolor=None, edgecolor=None, frameon=True,clear=False)

参数解释:
num:画布序号,一般默认是从1递增
figsize=(长,宽):画布尺寸,常用,单位为英寸
dpi:指定绘图对象的分辨率,即每英寸多少个像素,缺省值为80
facecolor:背景色,默认白色
edgecolor:边缘色,默认白色
frameon:是否显示边框
clear:是否清除原来的画布,默认False自动创建新画布

import matplotlib.pyplot as plt
plt.figure()
plt.show()
import matplotlib.pyplot as plt
fig=plt.figure(figsize=(5,4),facecolor='blue')
plt.show()

legend函数

legend函数:创建图例
在plt.plot()里添加label属性后,可调用plt.legend(),产生legend

xlabel函数

xlabel函数:在横坐标添加标签,字符串中不可以包含中文

plt.xlabel('X axis') ##横轴名称  
plt.ylabel('Y axis') ##纵轴名称  

如果是子图

 ax.set_xlabel('X axis') ##横轴名称 
 ax.set_ylabel('Y axis') ##纵轴名称  

太长的可以换行

ax.set_xlabel('X axis\ncolor') ##横轴名称  

title函数

title函数:增加主题
字符串中不可以包含中文

plt.title('First figure') 
plt.title('First figure', fontsize=10) 
ax[0].set_title('landmark')  #设置各个图的子标题
ax[1].set_title('benchmark')  #设置各个图的子标题
plt.suptitle('subplots函数子图展示', color='m', fontsize=14, fontweight='heavy',
             bbox=dict(boxstyle='round', facecolor='y', alpha=0.3))##子图设置主标题

示例1

import matplotlib.pyplot as plt
x = [1, 2, 3, 4, 7 ,9]
y = [1, 2, 4 ,10, 30 ,40]
plt.figure()
plt.plot(x, y,label='share price')
plt.legend()          #创建图例
plt.xlabel("X axis")  #x轴标签
plt.ylabel("Y axis")  #y轴标签
plt.title("Stock Price Trend Chart")  #主题
plt.show()

在这里插入图片描述
示例2:

import matplotlib.pyplot as plt
fig, axes = plt.subplots(nrows=2, ncols=2)
plt.subplots_adjust(hspace=0.5, wspace=0.5)
axes[0,0].set_title('A Stock Price Trend Chart')  #设置各个图的子标题
axes[0,1].set_title('B Stock Price Trend Chart')  #设置各个图的子标题
axes[1,0].set_title('C Stock Price Trend Chart')  #设置各个图的子标题
axes[1,1].set_title('D Stock Price Trend Chart')  #设置各个图的子标题
axes[0,0].set_xlabel('X axis') ##横轴名称 
axes[0,0].set_ylabel('Y axis') ##纵轴名称  
axes[0,1].set_xlabel('X axis') ##横轴名称 
axes[0,1].set_ylabel('Y axis') ##纵轴名称  
axes[1,0].set_xlabel('X axis') ##横轴名称 
axes[1,0].set_ylabel('Y axis') ##纵轴名称  
axes[1,1].set_xlabel('X axis') ##横轴名称 
axes[1,1].set_ylabel('Y axis') ##纵轴名称  
x = [1, 2, 3, 4, 7 ,9]
y1 = [1, 2, 4 ,8, 8 ,12]
y2 = [1, 2, 4 ,10, 30 ,40]
y3 = [1, 2, 5 ,12, 30 ,40]
y4 = [1, 2, 4 ,10, 8 ,5]
axes[0,0].plot(x, y1,'r',label='A share price')
axes[0,0].legend() 
axes[0,1].plot(x, y2,label='B share price')
axes[0,1].legend() 
axes[1,0].plot(x, y3,'g',label='C share price')
axes[1,0].legend() 
axes[1,1].plot(x, y4,'y',label='D share price')
axes[1,1].legend()          #创建图例
plt.suptitle('Stock Price Trend Chart', color='m', fontsize=14, fontweight='heavy')##子图设置主标题
plt.show()

在这里插入图片描述

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值