python-绘图-matplotlib

1. matplotlib是什么?

  • python底层的绘图工具

2. matplotlib的基本要点:

  • 如何设置图片的大小;
    plt.figure(figsize=(, ))
  • 如何保存到本地;
    plt.savefig(’’)
  • x轴和y轴的描述信息;
    plt.xlabel("", fontproperties=)
    plt.ylabel("", fontproperties=)
  • 中文显示乱码问题;
    myfont = font_manager.FontProperties(fname="", size=)
  • 调整x轴和y轴的刻度;
    plt.xticks
  • x轴的刻度信息过长, 如何调整?
    rotation=45
  • 标记最高点;
    plt.scatter(x[0],y[0])
  • 添加网格;
    plt.grid(alpha=0.5)

基本要点:设置图片大小、中文乱码、调整坐标、绘制图形、保存图片、座标轴描述信息

案例1: 假设一天中每隔两个小时气温变化的折线图绘制;
首先安装pip install matplotlib

from matplotlib import pyplot as plt
from matplotlib import font_manager
# 4). 中文显示乱码问题;
myfont = font_manager.FontProperties(fname="/usr/share/fonts/cjkuni-uming/uming.ttc", size=18)
titlefont = font_manager.FontProperties(fname="/usr/share/fonts/cjkuni-uming/uming.ttc", size=24)
# 图表的x轴的数据, 是一个可迭代的数据类型
x_times = range(0, 24, 2)
# 图表的y轴的数据, 是一个可迭代的数据类型
y_temp = [15, 12, 13, 20, 23, 30, 15, 12, 13, 20, 23, 30]

# min(y_temp), max(y_temp)

#  1). 如何设置图片的大小;
plt.figure(figsize=(10, 10))

# 传入x和y轴的数据, 绘制图形;
plt.plot(x_times, y_temp)

# 3). x轴和y轴的描述信息;
plt.title("每天的气温变化(每隔两个小时)",fontproperties=titlefont )
plt.xlabel("时间", fontproperties=myfont)
plt.ylabel("温度", fontproperties=myfont)

# 5). 调整x轴和y轴的刻度;
# 6). x轴的刻度信息过长, 如何调整?

plt.xticks(x_times, labels=["%s时0分"%(i) for i in x_times], fontproperties=myfont, rotation=45)
# fontproperties 指定字体,rotation 倾斜程度
# x_times指定的是x轴的刻度 labels是每个刻度上的表示

y_temp_range = range(min(y_temp), max(y_temp)+1, 2)
# y_temp_range y轴的刻度
plt.yticks(y_temp_range, labels=["%s 。C"%(i) for i in y_temp_range], fontproperties=myfont)

# 标点
plt.scatter(x_times[2], y_temp[2], color='b')
plt.scatter(x_times[2], y_temp[2], color='', marker='o', edgecolors='r', s=300)

# 2). 如何保存到本地;
plt.savefig('doc/temp.png')

# 在执行程序时显示图像
# plt.show()

输出:
在这里插入图片描述

3. matplotlib的折线图, 柱状图, 直方图, 散点图;

折线图:plt.plot()
散点图:plt.scatter()
横向柱状图:plt.bar(width)
竖向柱状图:plt.barh(height)
直方图:plt.list()
在这里插入图片描述
在这里插入图片描述

1.折线图plot

需求:绘制10点到12点每分钟的气温, 如何绘制折线图观察每分钟气温的变化情况?
temps = [random.randint(20, 35) for i in range(120)]

import random

from matplotlib import pyplot as plt
from matplotlib import font_manager

myfont = font_manager.FontProperties(fname="/usr/share/fonts/cjkuni-uming/uming.ttc", size=25)
titlefont = font_manager.FontProperties(fname="/usr/share/fonts/cjkuni-uming/uming.ttc", size=50)

x_times = range(0,123)
y_temp = [random.randint(20,35) for i in range(123)]

plt.
  • 0
    点赞
  • 6
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值