您当前的位置:首页 > IT编程 > python
| C语言 | Java | VB | VC | python | Android | TensorFlow | C++ | oracle | 学术与代码 | cnn卷积神经网络 | gnn | 图像修复 | Keras | 数据集 | Neo4j | 自然语言处理 | 深度学习 | 医学CAD | 医学影像 | 超参数 | pointnet | pytorch | 异常检测 | Transformers | 情感分类 | 知识图谱 |

自学教程:Python

51自学网 2022-02-21 10:44:28
  python
这篇教程Python写得很实用,希望能帮到您。

1、画第一个图形

第一个图形从简单的开始。

1.1 代码

# importing the required moduleimport matplotlib.pyplot as plt# x axis valuesx = [1,2,3]# corresponding y axis valuesy = [2,4,1]# plotting the pointsplt.plot(x, y)# naming the x axisplt.xlabel('x - axis')# naming the y axisplt.ylabel('y - axis')# giving a title to my graphplt.title('My first graph!')# function to show the plotplt.show()

1.2 输出

1.3 代码的部分解释

  • 1)将 x 轴和相应的 y 轴值定义为列表。
  • 2)使用 .plot() 函数在画布上绘制它们。
  • 3)使用 .xlabel() 和 .ylabel() 函数为 x 轴和 y 轴命名。
  • 4)使用 .title() 函数为绘图命名。
  • 5)使用 .show() 函数查看绘图。
  • 2、在同一图上绘制两条或多条线

2、在同一图上绘制两条或多条线

如果想在同一张图上再绘制多条线,可反复使用.plot()函数。

2.1 代码

import matplotlib.pyplot as plt# line 1 pointsx1 = [1,2,3]y1 = [2,4,1]# plotting the line 1 pointsplt.plot(x1, y1, label = "line 1")# line 2 pointsx2 = [1,2,3]y2 = [4,1,3]# plotting the line 2 pointsplt.plot(x2, y2, label = "line 2")# naming the x axisplt.xlabel('x - axis')# naming the y axisplt.ylabel('y - axis')# giving a title to my graphplt.title('Two lines on same graph!')# show a legend on the plotplt.legend()# function to show the plotplt.show()

2.2 输出

2.3 代码的部分解释

  • 1)在同一张图上绘制两条线。 通过给它们一个名称(label)来区分它们,该名称作为 .plot() 函数的参数传递。
  • 2)提供有关线条类型及其颜色信息的小矩形框称为图例。 可以使用 .legend() 函数为绘图添加图例。

3、自定义绘图

下面将讨论适用于几乎所有场景的一些基本自定义。

3.1 代码

import matplotlib.pyplot as plt# x axis valuesx = [1,2,3,4,5,6]# corresponding y axis valuesy = [2,4,1,5,2,6]# plotting the pointsplt.plot(x, y, color='green', linestyle='dashed', linewidth = 3,marker='o', markerfacecolor='blue', markersize=12)# setting x and y axis rangeplt.ylim(1,8)plt.xlim(1,8)# naming the x axisplt.xlabel('x - axis')# naming the y axisplt.ylabel('y - axis')# giving a title to my graphplt.title('Some cool customizations!')# function to show the plotplt.show()

3.2 输出

3.3 代码的部分解释

如上面代码所示,我们进行了一些自定义的改变:

  • 1)设定线的宽度、样式以及颜色。
  • 2)设定了标记的形状、颜色和尺寸。
  • 3)覆盖 x 和 y 轴范围。如果未完成覆盖,pyplot 模块使用自动缩放功能来设置轴范围和比例。

到此这篇关于Python 图形绘制详细代码的文章就介绍到这了,更多相关Python 图形绘制详细内容请搜索51zixue.net以前的文章或继续浏览下面的相关文章希望大家以后多多支持51zixue.net!


python数据结构之搜索讲解
Python实现为图像添加下雪特效
万事OK自学网:51自学网_软件自学网_CAD自学网自学excel、自学PS、自学CAD、自学C语言、自学css3实例,是一个通过网络自主学习工作技能的自学平台,网友喜欢的软件自学网站。