python使用matplotlib绘制折线图的示例代码
作者:全心全意_运维 时间:2021-06-25 22:51:18
示例代码如下:
#!/usr/bin/python
#-*- coding: utf-8 -*-
import matplotlib.pyplot as plt
# figsize - 图像尺寸(figsize=(10,10))
# facecolor - 背景色(facecolor="blue")
# dpi - 分辨率(dpi=72)
fig = plt.figure(figsize=(10,10),facecolor="blue") #figsize默认为4,4(图像尺寸)
ax1 = fig.add_subplot(1,1,1) # 行 列 位置
#ax2 = fig.add_subplot(2,1,2)
#ax = fig.add_subplot(1,1,1)
ax1.set_title("title") #不支持中文
# 设置坐标轴的label
ax1.set_xlabel("ax1 - X")
ax1.set_ylabel("ax1 - Y")
# 设置刻度
#ax1.set_xticks([1,2,3,4,5])
#ax1.set_yticks([10,20,30,40,50])
# 设置刻度label
#ax1.set_xticklabels(["one","two","three","four","five"]) # one对应1
# 绘制折线图
x = [1,2,3,4,5]
y = [80,3,4,5,1]
#生成正弦波曲线
import numpy as np
x = np.linspace(0,np.pi * 2,20)
y = np.sin(x)
#生成余弦波曲线
y2 = np.cos(x)
#ax1.plot(x,y,x,y2) #在一张图中放置两条曲线
# 使用图例
# linewidth设置线条粗细,linestyle设置线条样式,marker设置数据点
ax1.plot(x,y, label = "SIN",color="y",linewidth=3,linestyle="--",marker="o")
ax1.plot(x,y2,label= "COS",color="r")
ax1.legend(loc="best") # 使用图例 #best为最佳位置 (upper left 左上;center 居中;...)
# 注释,比如说明最高点
# xy指定最高点,xytext指定注释位置
arrowprops = {"arrowstyle": "->","color":"red"} #设置箭头
ax1.annotate("max",xy=(np.pi/2,1),xytext=(np.pi/2+0.5,1),arrowprops=arrowprops)
plt.show()
效果如下
来源:https://www.cnblogs.com/zhangquan-yw/p/12418951.html
标签:python,matplotlib,绘制,折线图
![](/images/zang.png)
![](/images/jiucuo.png)
猜你喜欢
python实现定时发送邮件
2022-12-20 07:49:49
python3.5 email实现发送邮件功能
2023-06-14 15:58:59
pandas 时间格式转换的实现
2021-03-23 13:09:01
Python标准库defaultdict模块使用示例
2022-03-02 04:35:05
QML使用Python的函数过程解析
2021-12-10 19:20:31
CSS制作11种风格不同的特效文字
2010-10-20 20:08:00
selenium+python 去除启动的黑色cmd窗口方法
2023-06-07 10:56:27
Python中导入模块的几种方式总结
2023-08-04 17:03:54
![](https://img.aspxhome.com/file/2023/2/61432_0s.png)
python抽象基类用法实例分析
2021-03-04 11:06:25
yolov5返回坐标的方法实例
2023-10-05 20:09:43
![](https://img.aspxhome.com/file/2023/3/76723_0s.jpg)
Python虚拟环境项目实例
2023-03-07 16:59:01
![](https://img.aspxhome.com/file/2023/1/100881_0s.png)
三个动画函数,透明度渐变、位置移动、尺寸变化javascript源码
2010-03-28 13:23:00
解析php session_set_save_handler 函数的用法(mysql)
2023-11-17 21:58:28
Mootools 1.2教程(9)——输入过滤第二部分(字符串)
2008-12-01 12:25:00
Pivot,信息组织的梦想之窗
2009-12-25 14:32:00
![](https://img.aspxhome.com/file/UploadPic/20101/11/pivot-01-87s.jpg)
如何在ACCESS中插入超级链接?
2007-10-22 12:13:00
![](https://img.aspxhome.com/file/UploadPic/200710/22/2007102212225806s.gif)
PHP基于迭代实现文件夹复制、删除、查看大小等操作的方法
2023-09-03 22:21:42
python 列表降维的实例讲解
2023-08-28 16:44:54
sql如何在线创建新表?
2010-06-22 21:21:00
Django实现单用户登录的方法示例
2021-12-21 03:26:41
![](https://img.aspxhome.com/file/2023/6/88896_0s.png)