Python matplotlib绘图设置图例案例
作者:返回主页悬崖上的金鱼 时间:2021-08-17 07:05:04
目录
一、语法简介
二、完整代码
一、语法简介
plt.legend(loc=2,edgecolor='red',facecolor='green',shadow='True',fontsize=10)
edgecolor
图例边框线颜色facecolor
图例背景色shadow
是否添加阴影title
图例标题fontsize
设置字体大小
'''
设置图例位置loc参数简介
best 0 根据图标区域自动选择最合适的位置
upper right 1 右上角
upper left 2 左上角
lower left 3 左下角
lower right 4 右下角
right 5 右侧
center left 6 左侧中心
center right 7 右侧中心
lower center 8 底部中心
upper center 9 顶部中心
center 10 正中心位置
'''
二、完整代码
import matplotlib.pyplot as plt
import numpy as np
plt.rcParams['font.sans-serif'] = ['STZhongsong'] # 指定默认字体:解决plot不能显示中文问题
plt.rcParams['axes.unicode_minus'] = False #用来正常显示负号
x=np.arange(8)
y=np.arange(100,900,100)
print(y)
#建立画布 figsize,它用width和height来控制画布的宽和高
plt.figure(figsize=(8,6),dpi=90) #facecolor='red'设置画布颜色
plt.subplot(1,1,1)#建立坐标系
plt.bar(x,y,label='销售数量') #绘制柱状图
plt.xlabel("销售月份",fontsize=10,color='red',fontweight='bold',loc='center',backgroundcolor='black',labelpad=6)
#显示横坐标标题 fontsize设置字体大小,color设置字的颜色,fontweight设置标签是否加粗
#loc设置标签位置(具体值有center left right) backgroundcolor设置标签的背景颜色 labelpad与轴的距离
plt.ylabel("销售数量")
plt.xticks(x,['2021年1月','2021年2月','2021年3月','2021年4月','2021年5月','2021年6月','2021年7月','2021年8月',],rotation=15)
plt.yticks(y,['100k','200k','300k','400k','500k','600k','700k','800k',],
rotation=30,fontsize=10,color='red',fontweight='bold',backgroundcolor='black')#rotation设置刻度值倾斜角度
plt.xlim(-1,9) #设置x轴刻度值的范围
plt.ylim(0,900)#设置y轴刻度值的范围
plt.axis("on")
#plt.axis("off") #关闭坐标轴
plt.legend(loc=2,edgecolor='red',facecolor='green',shadow='True',fontsize=10)
#edgecolor 图例边框线颜色 facecolor 图例背景色 shadow 是否添加阴影 title 图例标题 fontsize 设置字体大小
'''
设置图例位置loc参数简介
best 0 根据图标区域自动选择最合适的位置
upper right 1 右上角
upper left 2 左上角
lower left 3 左下角
lower right 4 右下角
right 5 右侧
center left 6 左侧中心
center right 7 右侧中心
lower center 8 底部中心
upper center 9 顶部中心
center 10 正中心位置
'''
plt.show()
三、效果图展示
来源:https://www.cnblogs.com/lcl-cn/p/15614941.html
标签:Python,matplotlib,绘图,设置图例
![](/images/zang.png)
![](/images/jiucuo.png)
猜你喜欢
IE中雅黑字体给布局带来的变化
2008-06-13 11:22:00
![](https://img.aspxhome.com/file/UploadPic/20086/13/2008613122420492s.gif)
Python wxPython库Core组件BoxSizer用法示例
2023-01-17 23:08:32
![](https://img.aspxhome.com/file/2023/9/102859_0s.png)
数据库应用经验:如何简单安装MySQL数据库
2009-01-04 12:58:00
mysql 忘记root密码
2010-12-14 14:50:00
不用script仅用css编写无限级弹出菜单
2008-04-24 14:03:00
![](https://img.aspxhome.com/file/UploadPic/20084/24/css-menu_89s.gif)
使用Python的Twisted框架编写非阻塞程序的代码示例
2021-01-22 16:20:51
ASP导出Excel数据的四种办法
2007-08-26 18:32:00
Python实现程序的单一实例用法分析
2023-01-08 11:38:14
使用Microsoft SQL Server 2000全文搜索功能构建Web搜索应用程序
2008-09-29 12:32:00
python抓取京东商城手机列表url实例代码
2022-11-11 18:23:04
python3.6+django2.0开发一套学员管理系统
2022-11-10 14:15:21
Python多进程multiprocessing、进程池用法实例分析
2022-05-20 00:01:29
![](https://img.aspxhome.com/file/2023/0/105230_0s.png)
python基础之错误和异常处理
2021-07-28 05:46:15
![](https://img.aspxhome.com/file/2023/3/65933_0s.png)
Python中super关键字用法实例分析
2023-12-08 06:11:46
ASP从数据库中获取下载文件
2007-10-06 21:17:00
Python有序查找算法之二分法实例分析
2023-04-09 00:02:37
![](https://img.aspxhome.com/file/2023/8/85378_0s.jpg)
python中执行shell的两种方法总结
2023-03-12 15:20:39
python语言time库和datetime库基本使用详解
2022-11-15 08:59:08
提升设计品质的8个布局方案[译]
2010-03-18 16:06:00
![](https://img.aspxhome.com/file/UploadPic/20103/18/01-33s.jpg)
PHP开发中常用的三个表单验证函数使用小结
2023-11-21 19:15:50