python 设置xlabel,ylabel 坐标轴字体大小,字体类型

yipeiwu_com6年前Python基础

本文介绍了python 设置xlabel,ylabel 坐标轴字体大小,字体类型,分享给大家,具体如下:

#--coding:utf-8--
import matplotlib.pyplot as plt
 
#数据设置
x1 =[0,5000,10000, 15000, 20000, 25000, 30000, 35000, 40000, 45000, 50000, 55000];
y1=[0, 223, 488, 673, 870, 1027, 1193, 1407, 1609, 1791, 2113, 2388];
 
x2 =[0,5000,10000, 15000, 20000, 25000, 30000, 35000, 40000, 45000, 50000, 55000];
y2=[0, 214, 445, 627, 800, 956, 1090, 1281, 1489, 1625, 1896, 2151];
 
#设置输出的图片大小
figsize = 11,9
figure, ax = plt.subplots(figsize=figsize)
 
#在同一幅图片上画两条折线
A,=plt.plot(x1,y1,'-r',label='A',linewidth=5.0)
B,=plt.plot(x2,y2,'b-.',label='B',linewidth=5.0)
 
#设置图例并且设置图例的字体及大小
font1 = {'family' : 'Times New Roman',
'weight' : 'normal',
'size' : 23,
}
legend = plt.legend(handles=[A,B],prop=font1)
 
#设置坐标刻度值的大小以及刻度值的字体
plt.tick_params(labelsize=23)
labels = ax.get_xticklabels() + ax.get_yticklabels()
[label.set_fontname('Times New Roman') for label in labels]
 
#设置横纵坐标的名称以及对应字体格式
font2 = {'family' : 'Times New Roman',
'weight' : 'normal',
'size' : 30,
}
plt.xlabel('round',font2)
plt.ylabel('value',font2)
 
#将文件保存至文件中并且画出图
plt.savefig('figure.eps')
plt.show()

实例1:为二维子图设置坐标轴标题

#!/usr/bin/python3
#code-python(3.6)
import matplotlib.pyplot as plt
fig = plt.figure() #设置画布
#将画布分为2行2列,共4个子图,并定位在第1个子图
ax = fig.add_subplot(2,2,1)  #返回第1个子图
ax.set_xlabel('Month') #为子图设置横轴标题
ax.set_ylabel('Year') #为子图设置纵轴标题
plt.show()

函数说明

#函数中的参数的值均为默认的参数值
matplotlib.axes.Axes.set_xlabel(xlabel, fontdict=None, labelpad=None, **kwargs)

实例2:为三维子图设置坐标轴标题

#!/usr/bin/python3
#code-python(3.6)
import matplotlib.pyplot as plt
fig = plt.figure() #设置画布
from mpl_toolkits.mplot3d import Axes3D
#将画布分为2行1列,共2个子图,并定位在第2个子图
ax = fig.add_subplot(212, projection='3d')
ax.set_xlabel('Month') #为子图设置x轴标题
ax.set_ylabel('Year') #为子图设置y轴标题
ax.set_zlabel('Sales') #为子图设置z轴标题
plt.show()

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持【听图阁-专注于Python设计】。

相关文章

使用python将最新的测试报告以附件的形式发到指定邮箱

使用python将最新的测试报告以附件的形式发到指定邮箱

具体代码如下所示: import smtplib, email, os, time from email.mime.multipart import MIMEMultipart fr...

python实现读取并显示图片的两种方法

在 python 中除了用 opencv,也可以用 matplotlib 和 PIL 这两个库操作图片。本人偏爱 matpoltlib,因为它的语法更像 matlab。 一、matplo...

详解python tkinter模块安装过程

引言: 在Python3下运行Matplotlib之时,碰到了”No module named _tkinter“的问题,花费数小时进行研究解决,这里讲整个过程记录下来,并尝试分析过程中...

Django错误:TypeError at / 'bool' object is not callable解决

使用 Django自带的 auth 用户验证功能,编写函数,使用 is_authenticated 检查用户是否登录,结果报错: TypeError at / 'bool' object...

matplotlib在python上绘制3D散点图实例详解

matplotlib在python上绘制3D散点图实例详解

大家可以先参考官方演示文档: 效果图: ''' ============== 3D scatterplot ============== Demonstration of a ba...