萌新快速上手:matplotlib学习笔记(1):简介,安装,在PyCharm中快速上手

萌新快速上手

1.matplotlib是什么

官方:
Matplotlib is a Python 2D plotting library which produces publication quality figures in a variety of hardcopy formats and interactive environments across platforms. Matplotlib can be used in Python scripts, the Python and IPython shells, the Jupyter notebook, web application servers, and four graphical user interface toolkits.
Matplotlib是一个Python 2D绘图库,它以各种硬拷贝格式和跨平台的交互式环境生成出版物质量的图形。Matplotlib可用于Python脚本,Python和IPython Shell,Jupyter笔记本,Web应用程序服务器和四个图形用户界面工具包。

2.如何安装

官方文档非常详细,推荐使用谷歌浏览器直接页面翻成中文观看
链接: https://matplotlib.org/users/installing.html

3.开始上手

3.1预备知识

一个图的结构(来自官方文档): Alt

3.2第一次上手
import matplotlib.pyplot as plt
fig = plt.figure()  # an empty figure with no axes
fig.suptitle('No axes on this figure')  # Add a title so we know which it is
fig, ax_lst = plt.subplots(2, 2)  # a figure with a 2x2 grid of Axes
plt.show()

本来弹不出来图片,加入了plt.show()可以弹出来了
运行结果:
在这里插入图片描述
蛮有意思的 感觉掌握了之后又是生产力工具
继续读文档
奥利给

3.3Matplotlib,pyplot和pylab:它们之间有什么关系?

Matplotlib是整个软件包,matplotlib.pyplot是Matplotlib中的模块。
pylab是一个便捷模块, 在单个名称空间中批量导入 matplotlib.pyplot(用于绘图)和numpy(用于数学以及使用数组)。pylab已过时,并且由于命名空间污染而强烈不建议使用pylab。使用pyplot代替。

对于非交互式绘图,建议使用pyplot创建图形,然后使用OO接口进行绘图。

对于pyplot模块中的功能,始终有一个“当前”图形和轴(根据要求自动创建)。例如,在下面的例子中,在第一次调用plt.plot创建轴,则后续调用plt.plot在同一坐标添加额外的线,以及 plt.xlabel,plt.ylabel,plt.title和plt.legend设置轴标签和标题和添加的图例。

import matplotlib.pyplot as plt
import numpy as np
x = np.linspace(0, 2, 100) //开始值,结束值,样本数
plt.plot(x, x, label='linear')
plt.plot(x, x**2, label='quadratic')
plt.plot(x, x**3, label='cubic')
plt.xlabel('x label')
plt.ylabel('y label')
plt.title("Simple Plot")
plt.legend()
plt.show()

运行结果:
在这里插入图片描述

3.4方便绘图,编写自己的绘图函数

官方建议的书写格式:

def my_plotter(ax, data1, data2, param_dict):
    """
    A helper function to make a graph

    Parameters
    ----------
    ax : Axes
        The axes to draw to

    data1 : array
       The x data

    data2 : array
       The y data

    param_dict : dict
       Dictionary of kwargs to pass to ax.plot

    Returns
    -------
    out : list
        list of artists added
    """
    out = ax.plot(data1, data2, **param_dict)
    return out

# which you would then use as:

data1, data2, data3, data4 = np.random.randn(4, 100)
fig, ax = plt.subplots(1, 1)
my_plotter(ax, data1, data2, {'marker': 'x'})
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值