matplotlib之简单折线图

matplotlib绘制图形
主要用于2-d图形绘制,也可以绘制3-d图形
绘制流程
1、创建画布
2、绘制图形
3、图形展示

import matplotlib.pyplot as plt
import numpy as np

# 以简单的折线图为例,绘制图形
# 1、创建画布
plt.figure()
# 2、绘制图形
# 折线图---点 --->(x,y)
# (x1,y1),(x2,y2),(x3,y3),...,
# 准备横轴
x = np.array([1, 2, 3])

# 准备纵轴
y = np.array([7, 6, 8])
# 绘制折线图
plt.plot(x, y)

# 3、图形展示
plt.show()

效果图如下:
在这里插入图片描述
利用matplotlib绘制一周天气变化折线图

import matplotlib.pyplot as plt
import numpy as np

# 1、创建画布
# figsize= (,) --表示画布的大小
# dpi ---像素值
# 返回值--->画布对象
plt.figure()
# 默认不支持中文,需要修改参数,让其支持中文
plt.rcParams['font.sans-serif'] = 'SimHei'  # 雅黑字体
# 继续修改参数,让其继续支持负号
plt.rcParams['axes.unicode_minus'] = False

# 2、绘制图形
# 准备横轴数据
# 周一、周二、...、周日
# 注意:如果横轴为中文,绘制图形的时候,建议先用序号来代替,后续再替换序号
x = np.arange(1, 8)
# 准确纵轴数据
y_gz = np.array([15, 20, 22, 23, 20, 18, 16])
y_heb = np.array([-10, -8, -12, -10, -8, -6, 1])

# 绘制图形
# color ---> 线条的颜色
# linestyle -->线的样式
# linewidth -->线的宽度
# marker --->点的样式
# markersize --->点的大小
# markerfacecolor --->点的填充颜色
# markeredgecolor --->点的边缘颜色
plt.plot(x, y_gz, color='r', linestyle=':', linewidth=1.2, marker="*", markersize=7, markerfacecolor='b',
         markeredgecolor='g')
plt.plot(x, y_heb, color='b', linestyle='-.', linewidth=1.2, marker="o", markersize=7, markerfacecolor='k',
         markeredgecolor='k')

# 注意:建议将所有的修饰都放置在绘图之后

# 增加标题
plt.title('一周广州、哈尔滨天气温度走势')

# 增加横轴名称
plt.xlabel('日期')

# 增加纵轴名称
plt.ylabel('温度(℃)')

# 修改横轴刻度
# 如果设置刻度的时候,需要将刻度替换为中文,此时需要2个参数
# 参数1:需要被替换的序号
# 参数2:中文刻度
xticks = ['周一', '周二', '周三', '周四', '周五', '周六', '周日', ]
plt.xticks(x, xticks)

# 修改纵轴刻度
# 如果修改刻度的时候,重设刻度显示范围,此时只需要一个参数
# 参数:新的刻度范围
yticks = np.arange(-15, 33, 3)
plt.yticks(yticks)

# 增加图例
# 参数1:图例
# 参数loc:图例位置
plt.legend(['广州温度', '哈尔滨温度'], loc='upper right')

# 标注点
# plt.text ---每一次只能标注一个点
for i, j in zip(x, y_gz):
    # print(i, j)
    # 参数1:标注位置的横轴
    # 参数2:标注位置的纵轴
    # 参数3:标注的内容--str
    # horizontalalignment='center' --让其标注水平居中
    # verticalalignment='bottom'
    # 这里让j+1目的为了让标注在点的上面一点显示
    plt.text(i, j + 1, '%d℃' % j, horizontalalignment='center')

for i, j in zip(x, y_heb):
    # print(i, j)
    # 参数1:标注位置的横轴
    # 参数2:标注位置的纵轴
    # 参数3:标注的内容--str
    # horizontalalignment='center' --让其标注居中
    # verticalalignment='bottom'
    plt.text(i, j + 1, '%d℃' % j, horizontalalignment='center')

# 保存图片:必须在展示之前保存
plt.savefig('./png/一周广州、哈尔滨天气温度走势.png')

# 3、展示
plt.show()

# 折线图:用于查看数据的发展变化趋势

折线图如下:
在这里插入图片描述
关于:线的样式linestyle 颜色color 点的样式marker 颜色markerfacecolor可查看源码

               **Markers**

        =============    ===============================
        character        description
        =============    ===============================
        ``'.'``          point marker
        ``','``          pixel marker
        ``'o'``          circle marker
        ``'v'``          triangle_down marker
        ``'^'``          triangle_up marker
        ``'<'``          triangle_left marker
        ``'>'``          triangle_right marker
        ``'1'``          tri_down marker
        ``'2'``          tri_up marker
        ``'3'``          tri_left marker
        ``'4'``          tri_right marker
        ``'s'``          square marker
        ``'p'``          pentagon marker
        ``'*'``          star marker
        ``'h'``          hexagon1 marker
        ``'H'``          hexagon2 marker
        ``'+'``          plus marker
        ``'x'``          x marker
        ``'D'``          diamond marker
        ``'d'``          thin_diamond marker
        ``'|'``          vline marker
        ``'_'``          hline marker
        =============    ===============================

        **Line Styles**

        =============    ===============================
        character        description
        =============    ===============================
        ``'-'``          solid line style
        ``'--'``         dashed line style
        ``'-.'``         dash-dot line style
        ``':'``          dotted line style
        =============    ===============================

        Example format strings::

            'b'    # blue markers with default shape
            'or'   # red circles
            '-g'   # green solid line
            '--'   # dashed line with default color
            '^k:'  # black triangle_up markers connected by a dotted line

        **Colors**

        The supported color abbreviations are the single letter codes

        =============    ===============================
        character        color
        =============    ===============================
        ``'b'``          blue
        ``'g'``          green
        ``'r'``          red
        ``'c'``          cyan
        ``'m'``          magenta
        ``'y'``          yellow
        ``'k'``          black
        ``'w'``          white
        =============    ===============================
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值