Python matplotlib绘图设置坐标轴的标题

2021/11/28 12:39:49

本文主要是介绍Python matplotlib绘图设置坐标轴的标题,对大家解决编程问题具有一定的参考价值,需要的程序猿们随着小编来一起学习吧!

一、语法简介

plt.xlabel("销售月份",fontsize=16,color='red',fontweight='bold',loc='center',backgroundcolor='black',
labelpad=10,rotation=1,alpha=1)
'''
fontsize 设置字体大小 默认值为12
fontweight 设置字体粗细
color 设置字体颜色
loc 设置标题对齐方式 具体值有center left right top bottom
backgroundcolor 设置背景颜色
labelpad 设置距离轴的距离
rotation 设置倾斜角度 取值为数值
alpha 设置透明度 取值为0-1之间
'''
plt.ylabel("销售数量",loc='bottom',fontweight='bold')

二、对应完整代码如下

import matplotlib.pyplot as plt
import numpy as np
from pylab import mpl
mpl.rcParams['font.sans-serif'] = ['STZhongsong']    # 指定默认字体:解决plot不能显示中文问题
mpl.rcParams['axes.unicode_minus'] = False 

x=np.arange(8)
y=np.arange(8)

#建立画布 figsize,它用width和height来控制画布的宽和高
plt.figure(figsize=(8,6),dpi=90) #facecolor='red'设置画布颜色

plt.subplot(1,1,1)#建立坐标系
plt.bar(x,y)

plt.xlabel("销售月份",fontsize=16,color='red',fontweight='bold',loc='center',backgroundcolor='black',
           labelpad=10,rotation=1,alpha=1) 
'''
fontsize 设置字体大小 默认值为12
fontweight 设置字体粗细 
color 设置字体颜色
loc 设置标题对齐方式 具体值有center left right top bottom
backgroundcolor 设置背景颜色
labelpad 设置距离轴的距离
rotation 设置倾斜角度 取值为数值
alpha 设置透明度 取值为0-1之间
'''
plt.ylabel("销售数量",loc='bottom',fontweight='bold')

plt.show()

三、对应效果图如下所示

 



这篇关于Python matplotlib绘图设置坐标轴的标题的文章就介绍到这儿,希望我们推荐的文章对大家有所帮助,也希望大家多多支持为之网!


扫一扫关注最新编程教程