admin 发表于 2024-1-5 00:55:47

python学习笔记

1.修改python升级源

要设置Python的升级源,可以按照以下步骤进行操作:
首先打开命令提示符或者终端。
输入以下命令来查看当前系统中已安装的Python版本及其路径:py -0p(Windows)或 which python3(Mac/Linux)。
如果没有显示任何结果,则表示未安装Python。
根据上述命令得到的信息确定需要更新的Python版本所在的目录。
使用文本编辑器打开该目录下的"pip.ini"文件(若不存在则创建一个新文件)。
将以下内容复制并粘贴到"pip.ini"文件中保存:

index-url = https://mirrors.aliyun.com/pypi/simple/
trusted-host = mirrors.aliyun.com关闭文本编辑器后,重新运行pip install --upgrade pip命令来升级pip工具。
现在就可以通过pip install <package>命令来安装、升级Python包了。

2.安装matplotlib
请确保你已经在本地的 Python 环境中安装了 matplotlib 库。你可以使用以下命令来安装 matplotlib:
pip install matplotlib
C:\Users\Administrator>pip install matplotlib
Looking in indexes: https://mirrors.aliyun.com/pypi/simple/
Collecting matplotlib
Downloading https://mirrors.aliyun.com/pypi/packages/26/5a/27fd341e4510257789f19a4b4be8bb90d1113b8f176c3dab562b4f21466e/matplotlib-3.8.2-cp311-cp311-win_amd64.whl (7.6 MB)
   ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━ 7.6/7.6 MB 1.2 MB/s eta 0:00:00
Collecting contourpy>=1.0.1 (from matplotlib)
Downloading https://mirrors.aliyun.com/pypi/packages/ca/2a/d197a412ec474391ee878b1218cf2fe9c6e963903755887fc5654c06636a/contourpy-1.2.0-cp311-cp311-win_amd64.whl (187 kB)
   ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━ 187.6/187.6 kB 1.1 MB/s eta 0:00:00
Collecting cycler>=0.10 (from matplotlib)
Downloading https://mirrors.aliyun.com/pypi/packages/e7/05/c19819d5e3d95294a6f5947fb9b9629efb316b96de511b418c53d245aae6/cycler-0.12.1-py3-none-any.whl (8.3 kB)
Collecting fonttools>=4.22.0 (from matplotlib)
Downloading https://mirrors.aliyun.com/pypi/packages/d2/54/26919780384c6dd9c3108c5ca64bbe8af45ce567e2f2493cd0cb8c596b9a/fonttools-4.47.0-cp311-cp311-win_amd64.whl (2.2 MB)
   ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━ 2.2/2.2 MB 1.2 MB/s eta 0:00:00
Collecting kiwisolver>=1.3.1 (from matplotlib)
Downloading https://mirrors.aliyun.com/pypi/packages/1e/37/d3c2d4ba2719059a0f12730947bbe1ad5ee8bff89e8c35319dcb2c9ddb4c/kiwisolver-1.4.5-cp311-cp311-win_amd64.whl (56 kB)
   ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━ 56.1/56.1 kB 1.5 MB/s eta 0:00:00
Requirement already satisfied: numpy<2,>=1.21 in c:\users\administrator\appdata\local\programs\python\python311\lib\site-packages (from matplotlib) (1.25.2)
Collecting packaging>=20.0 (from matplotlib)
Downloading https://mirrors.aliyun.com/pypi/packages/ec/1a/610693ac4ee14fcdf2d9bf3c493370e4f2ef7ae2e19217d7a237ff42367d/packaging-23.2-py3-none-any.whl (53 kB)
   ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━ 53.0/53.0 kB 910.0 kB/s eta 0:00:00
Collecting pillow>=8 (from matplotlib)
Downloading https://mirrors.aliyun.com/pypi/packages/43/56/f92715a873187b5eff72a4a0d2ac6258e18e9bfb0e136aafde65c49a841a/pillow-10.2.0-cp311-cp311-win_amd64.whl (2.6 MB)
   ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━ 2.6/2.6 MB 994.7 kB/s eta 0:00:00
Collecting pyparsing>=2.3.1 (from matplotlib)
Downloading https://mirrors.aliyun.com/pypi/packages/39/92/8486ede85fcc088f1b3dba4ce92dd29d126fd96b0008ea213167940a2475/pyparsing-3.1.1-py3-none-any.whl (103 kB)
   ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━ 103.1/103.1 kB 1.2 MB/s eta 0:00:00
Requirement already satisfied: python-dateutil>=2.7 in c:\users\administrator\appdata\local\programs\python\python311\lib\site-packages (from matplotlib) (2.8.2)
Requirement already satisfied: six>=1.5 in c:\users\administrator\appdata\local\programs\python\python311\lib\site-packages (from python-dateutil>=2.7->matplotlib) (1.16.0)
Installing collected packages: pyparsing, pillow, packaging, kiwisolver, fonttools, cycler, contourpy, matplotlib
Successfully installed contourpy-1.2.0 cycler-0.12.1 fonttools-4.47.0 kiwisolver-1.4.5 matplotlib-3.8.2 packaging-23.2 pillow-10.2.0 pyparsing-3.1.13.折线图
以下是一个简单的示例代码,展示如何使用 matplotlib 绘制一个简单的折线图:
import matplotlib.pyplot as plt

# 模拟数据
x =
y =

# 绘制折线图
plt.plot(x, y)

# 添加标签和标题
plt.xlabel("X轴")
plt.ylabel("Y轴")
plt.title("折线图")

# 显示图形
plt.show()




页: [1]
查看完整版本: python学习笔记