Python绘图(一):matplotlib 基本使用

本文介绍了如何使用Python的Matplotlib库进行基本绘图,包括环境搭建、常用函数说明及示例,如绘制正弦函数、添加图表标签和微信阅读量统计。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

本篇文章介绍 matplotlib 包的基本使用。这会是一个系列文章,之后我会逐步介绍Python更高级的绘图方法。

环境搭建

对于新手来说,最简单的方式就是安装 Anacoda 了,这是它的官方网站https://www.anaconda.com/

百度百科:

Anaconda指的是一个开源的Python发行版本,其包含了conda、Python等180多个科学包及其依赖项。

使用的Python库
  • numpy:NumPy系统是Python的一种开源的数值计算扩展。这种工具可用来存储和处理大型矩阵,比Python自身的嵌套列表(nested list structure)结构要高效的多(该结构也可以用来表示矩阵(matrix))。
  • pandans:Pandas 是基于NumPy 的一种工具,该工具是为了解决数据分析任务而创建的。Pandas 纳入了大量库和一些标准的数据模型,提供了高效地操作大型数据集所需的工具。
  • matplotlib:Python 的绘图库
示例 1:绘制简单正弦函数
import matplotlib.pyplot as plt
import numpy as np

x = np.linspace(0, 10, 100)
y = np.sin(x)  # sin 函数
plt.plot(x,y)
plt.show()

linspace 函数解释:

np.linspace(start, stop, num=50)

Parameters
----------
start : 
    The starting value of the sequence.
stop : scalar
    The end value of the sequence, unless `endpoint` is set to False.
    In that case, the sequence consists of all but the last of ``num + 1``
    evenly spaced samples, so that `stop` is excluded.  Note that the step
    size changes when `endpoint` is False.
num : int, optional
    Number of samples to generate. Default is 50. Must be non-negative.

Returns
-------
samples : ndarray
示例 2:在图片中加入标签
import matplotlib.pyplot as plt
import numpy as np

x = np.arange(0, 1.1, 0.01)
y =  x**2

plt.figure(figsize=(9,9))   # 设置画布大小
plt.title('lines')   # 设置title
plt.xlabel('x1')   # 设置x轴标签
plt.ylabel('y')
plt.xlim((0, 1))   # 设定x轴的范围
plt.ylim((0, 1))
plt.xticks([0, 0.2, 0.4, 0.6, 0.8, 1])   # 设置轴刻度
plt.yticks([0, 0.2, 0.4, 0.6, 0.8, 1])
plt.plot(x,y, label='y = x^2')
plt.legend(loc='best')
plt.show()

arange函数介绍:

np.arange([start,] stop[, step,])

Parameters
----------
start : number, optional
    Start of interval.  The interval includes this value.  The default
    start value is 0.
stop : number
    End of interval.  The interval does not include this value, except
    in some cases where `step` is not an integer and floating point
    round-off affects the length of `out`.
step : number, optional
    Spacing between values.  For any output `out`, this is the distance
    between two adjacent values, ``out[i+1] - out[i]``.  The default
    step size is 1.  If `step` is specified as a position argument,
    `start` must also be given.

Returns
-------
samples : ndarray
示例 3: 微信阅读量统计

数据格式:

DateCountsTimes
2017/10/1399763
2017/10/2126745
import matplotlib.pyplot as plt
import pandas as pd

wechat = pd.read_excel("wechat.xlsx")
wechat.Date = pd.to_datetime(wechat.Date, format='%Y-%m-%d')

plt.plot(wechat.Date,
        wechat.Counts,
        linestyle='-',
        linewidth=2,
        color='steelblue',
        marker='o',    # 折线图中添加圆点
        markersize=6,
        markeredgecolor='black',
        markerfacecolor='red'
)

plt.ylabel('Counts')
plt.xticks(rotation=45)   # x 刻度旋转角度
plt.title("Counts Statistic")
plt.show()
python大作业 、Turtle创意大PK 自拟题目,完成个利用Python程序的创意绘图,采用turtle库绘图为主,少于50行代码,可选采用其他库。 (滑稽绘制) 二、程序练习 2.1 问题描述(10分) 人们常常提到"万小时定律",就是管你做什么事情,只要坚持万小时,应该都可以成为该领域的专家。那么,10000小时是多少年多少天呢? 2.2 问题描述(10分)0380031003800341590145037657 编写计算从n到m和的函数‬,函数名为sum(n,m)‬,函数返回值为n到m所有数据的和‬,使用该函数计算输入数据x,y之间所有数据的和。 2.3 问题描述(15分) 编写函数judgeTri(a,b,c),判断以参数a,b,c的值为边长能否构成三角形并判断三角形的形状;若是锐角三角形,返回R;若是直角三角形,返回Z;若是钝角三角形,返回D;若三边长能构成三角形,返回ERROR。 2.4 问题描述(15分) 用户输入个字符串,分别统计其中小写字母、大写字母、数字、空格和其他字符的个数,并在行内输出小写字母、大写字母、数字、空格和其他字符的个数。 2.5 问题描述(20分) 程序的功能: (1) 使用随机库功能,生成个包含10个重复且小于200的正整数列表ls1,输出ls1。‬ (2) 使用列表排序方法,对ls1按奇数在前偶数在后,并且奇数之间的相对顺序变,偶数之间的相对顺序也变进行排序,再输出ls1。‬ (3) 使用列表排序方法,对ls1按元素字符长度降序进行排序,输出ls1。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值