matplotlib:

1.绘图

2.图表:属性

    线条:颜色、样式、数据点
    图表额外:轴标签(x,y)、图例、标题


3.有哪些图表:

    折线图、柱状图、散点图、饼状图。。。

4 matpoltlib 编程模型:

1.Figure  画布
2.Axes    图表

部分代码展示

花sin图像

 x = np.linspace(0, 2 * np.pi, 200)
    y = np.sin(x)

    fig, ax = plt.subplots()
    ax.plot(x, y)
    plt.show()

绘制折线、柱状、条形、饼状、散点图

import numpy as np
import pandas as pd

if __name__ == '__main__':
    # 1.数据准备
    df = pd.read_csv("D:\\data\data.csv",index_col="年份")
    print(df.head())

    #2.绘制图表 matplotlib
        #准备数据 x y
    x = df.index.values #年份
    y = df['啤酒产量(万千升)'].values

    from pylab import mpl
    mpl.rcParams['font.sans-serif'] = ['FangSong']

    #画图
    #1.折线图
    fig, ax = plt.subplots()
    ax.plot(x,y,"r--*")
    ax.set(title="啤酒产量走势",xlabel="年份",ylabel="啤酒产量(万千升") #标题 x y
    plt.show()

    # 2.柱状图
    fig, ax = plt.subplots()
    ax.bar(x, y, width=0.5, color="skyblue")
    ax.set(title="啤酒产量走势", xlabel="年份", ylabel="啤酒产量(万千升")  # 标题 x y
    plt.show()

    # 3.柱状图 - 水平柱状图
    fig, ax = plt.subplots()
    ax.barh(x, y, 0.5, color="skyblue")
    ax.set(title="啤酒产量走势", xlabel="年份", ylabel="啤酒产量(万千升")  # 标题 x y
    plt.show()

    #4.饼图
    fig, ax = plt.subplots()
    ax.pie(y, labels=x)
    plt.show()

    #5.散点图
    fig, ax = plt.subplots()
    ax.scatter(x, y,c="#ff7f0e",alpha=0.5)
    ax.set(title="啤酒产量走势", xlabel="年份", ylabel="啤酒产量(万千升")  # 标题 x y
    plt.show()

一个画布多个图表

#1.数据准备
    x = np.arange(0, 1, 0.05)
    y = np.sin(2*np.pi*x)

    #2.画布+多个图表
    fig = plt.figure()
    ax1 = fig.add_subplot(221)
    ax2 = fig.add_subplot(222)
    ax3 = fig.add_subplot(223)
    ax4 = fig.add_subplot(224)
    plt.show()

    # 3.画布+多个图表+数据
    fig = plt.figure()
    ax1 = fig.add_subplot(221)
    ax2 = fig.add_subplot(222)
    ax3 = fig.add_subplot(223)
    ax4 = fig.add_subplot(224)

    ax2.plot(x,y)
    plt.show()

    #4.修改曲线的颜色 线条样式 显示数据点
    fig = plt.figure()
    ax1 = fig.add_subplot(221)
    ax2 = fig.add_subplot(222)
    ax3 = fig.add_subplot(223)
    ax4 = fig.add_subplot(224)

    ax2.plot(x, y,"r--o")
    plt.show()

    #2.
    fig = plt.figure()
    ax1 = fig.add_subplot(221)
    ax2 = fig.add_subplot(222)
    ax3 = fig.add_subplot(223)
    ax4 = fig.add_subplot(224)

    ax2.plot(x, y, color="c",linestyle="--",marker="o")
    plt.show()
 

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值