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

软件发布|下载排行|最新软件

当前位置:首页IT学院IT技术

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

全心全意_运维   2020-09-22 我要评论

本文着重讲解了python使用matplotlib绘制折线图的代码实例,帮助大家更好的利用matplotlib绘制图形,欢迎大家阅读

示例代码如下:

#!/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()

效果如下

Copyright 2022 版权所有 软件发布 访问手机版

声明:所有软件和文章来自软件开发商或者作者 如有异议 请与本站联系 联系我们