脚本之家,脚本语言编程技术及教程分享平台!
分类导航

Python|VBS|Ruby|Lua|perl|VBA|Golang|PowerShell|Erlang|autoit|Dos|bat|

服务器之家 - 脚本之家 - Python - python使用matplotlib绘制折线图的示例代码

python使用matplotlib绘制折线图的示例代码

2020-09-23 09:31全心全意_运维 Python

这篇文章主要介绍了python使用matplotlib绘制折线图的示例代码,帮助大家更好的利用matplotlib绘制图形,感兴趣的朋友可以了解下

示例代码如下:

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
#!/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()

效果如下

python使用matplotlib绘制折线图的示例代码

以上就是python使用matplotlib绘制折线图的示例代码的详细内容,更多关于python matplotlib绘制折线图的资料请关注服务器之家其它相关文章!

原文链接:https://www.cnblogs.com/zhangquan-yw/p/12418951.html

延伸 · 阅读

精彩推荐