python使用matplotlib绘制折线图_python使用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()

效果如下

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

时间: 2020-09-20

你可能感兴趣的:(python使用matplotlib绘制折线图_python使用matplotlib绘制折线图的示例代码)