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()

效果如下

1217d777d9ad2d8a885ef76f8172785f.png

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

本文标题: python使用matplotlib绘制折线图的示例代码

本文地址: http://www.cppcns.com/jiaoben/python/348289.html

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值