没有合适的资源?快使用搜索试试~ 我知道了~
首页Python图形编程:2D和3D图像创建指南
Python图形编程:2D和3D图像创建指南
![](https://csdnimg.cn/release/wenkucmsfe/public/img/star.98a08eaa.png)
"Python Graphics: A Reference for Creating 2D and 3D Images" 是一本由 B.J. Korites 编著的书籍,专注于利用Python进行数据可视化和技术插图的创新图形创作。这本书不仅介绍了基本的绘图命令和库,还深入讲解了如何通过旋转、着色和编辑来增强图形的表现力。 在书中,读者将从Python的基础函数开始,学习如何设置绘图空间,生成二维和三维对象。随着内容的深入,读者可以掌握构建复杂图形的方法,包括对象的平移与旋转、隐藏线的删除、阴影效果的添加以增强真实感,以及数据集的图像投影技术。书中的最后一章提供了多个科学和工程领域的实际应用示例,如行星模型,鼓励读者根据自身需求进行个性化调整。 本书特别适合那些希望通过Python优化图像的开发者,以及对数据可视化感兴趣的科学家、工程师和学生。书中涵盖了绘制各种元素(如点、线、箭头和弧线)的命令,创建自定义网格,校正扭曲,添加文字和标签,以及处理弧线和圆圈的技巧。此外,还详细讨论了颜色的指定和使用。为了充分理解这些技术,读者需要具备一定的矢量、矩阵、几何和三角学基础知识。 通过学习本书,读者将能够创建和定制详细的科技插图,以及有效的数据可视化作品。无论是为了科研、工程还是教育目的,Python Graphics 都提供了宝贵的知识和实践经验,帮助读者提升在图形和可视化领域的专业技能。
资源详情
资源推荐
![](https://csdnimg.cn/release/download_crawler_static/10874818/bg10.jpg)
5
11 plt.grid(True)
12
13 plt.show()
Listing 1-1 produces the plotting area shown in Figure1-3. It has a horizontal width
of 20 and a vertical height of 20. I could have made these numbers 200 and 200, and
the area would appear in an output pane as the same physical size but with different
numerical values on the axes. Line 13 contains the command plt.show(). The purpose
of this is to display the program’s results in the output pane. With modern versions of
Python it isn’t required since the plots are automatically displayed when the program is
run. With older versions it may or may not be displayed. plt.show() can also be placed
within a program in order to show plots created during execution. Even though it may
not be necessary, it’s a good idea to include this command at the end of your script since
it can serve as a convenient marker for the end of your program. Lines 1, 2, 10, and 11in
Listing 1-1 will be explained in the following sections. These commands, or variations of
them, will appear in all of our programs.
Figure 1-3. Plotting area produced by Listing 1-1 with (0,0) located in the center
of the area
Chapter 1 essential python Commands andFunCtions
![](https://csdnimg.cn/release/download_crawler_static/10874818/bg11.jpg)
6
1.4 Importing Plotting Commands
While Python has many built-in commands and functions available, some math and
graphics commands must be imported. Lines 1 and 2in Listing 1-1 do this. The import
numpy as np statement in line 1 imports math functions such as sin(ϕ), e
α
, and so on.
The np in this statement is an abbreviation that may be used when referring to a numpy
function. When used in a program, these functions must be identified as coming from
numpy. For example, if you were to code v=e
α
, the program statement would be v=np.
exp(α) where α would have been previously defined. You don’t have to write out the full
length numpy.exp(α) since you defined the shorthand np for numpy in line 1. Graphics
commands are handled similarly. The statement import matplotlib.pyplot as plt
imports the library pyplot, which contains graphics commands. plt is an abbreviation
for pyplot. For example, if you want to plot a dot at x,y you would write plt.scatter(x,y).
I will talk more about plt.scatter() shortly.
Functions may also be imported directly from numpy. The statement from numpy
import sin, cos, radians imports the sin(), cos(), and radians() functions. When
imported in this manner they may be used without the np prefix. There is also a math
library that operates in a similar way. For example, from math import sin, cos, radians
is equivalent to importing from numpy. You will be using all these variations in the
coming programs.
There is also a graphics library called glib that contains graphics commands. glib
uses a different syntax than pyplot. Since pyplot is used more widely, you will use it in
your work here.
Line 8in Listing 1-1, plt.axis([x1,x2,y1,y2]), is the standard form of the command
that sets up the plotting area. This is from the pyplot library and is preceded by the
plt. prefix. There are attributes to this command and there are other ways of defining
a plotting area, notably the linspace() command, but the form in line 8 is sufficient
for most purposes and is the one you will use. x1 and x2 define the values of the left
and right sides, respectively, of the plotting area; y1 and y2 define the bottom and top,
respectively. With the numeric values in lines 8-11 you get the plotting area shown in
Figure1-3. x1,x2,y1, and y2 always have the locations shown in Figure1-3. That is, x1 and
y1 always refer to the lower left corner, y2 to other end of the y axis, and x2 to the other
end of the x axis. Their values can change, but they always refer to these locations. They
may be negative, as shown in Figure1-4.
Chapter 1 essential python Commands andFunCtions
![](https://csdnimg.cn/release/download_crawler_static/10874818/bg12.jpg)
7
Because the x and y values specified in lines 4-7 are symmetric in both the x and y
directions (i.e. −10, +10), this plotting area has the (x=0, y=0) point halfway between.
In this case, the center of the area will be the origin used as reference for plotting
coordinates. Since x1 < x2, the positive direction of the x axis will run horizontally from
left to right. Similarly, since y1 < y2, the positive direction of the y axis will go vertically
up. But earlier I said we want the positive y direction to go vertically down. You can do
that by reversing the y values to y1=10, y2=−10. In this case, you get the area shown in
Figure1-4 where the positive x axis still goes from left to right but the positive y axis now
points down. The center is still in the middle of the plotting area.
You could move the origin of the coordinate system off center by manipulating x1,
x2,y1, and y2. For example, to move the x=0 point all the way to the left side, you could
specify x1=0, x2=20. To move the (x=0, y=0) point to the lower left corner, you could
specify x1=0, x2=20, y1=0, y2=20. But that would make the positive y direction point up;
you want it to point down, which you can do by making y2=0, y1=20. This will make the
origin appear in the upper left corner. You are free to position the (0,0) point anywhere,
change the direction of positive x and y, and scale the numerical values of the coordinate
axes to suit the image you will be trying to create. The numerical values you are using
here could be anything. The physical size of the plot produced by Python will be the
same; only the values of the image coordinates will change.
Figure 1-4. Plotting area with (0,0) located in the center, positive y direction
pointing down
Chapter 1 essential python Commands andFunCtions
![](https://csdnimg.cn/release/download_crawler_static/10874818/bg13.jpg)
8
1.5 Displaying thePlotting Area
In line 10 of Listing 1-1 the statement plt.axis('on') displays the plotting area with its
frame and numerical values. If you omit this command, the frame will still be displayed
with numerical values. So why include this command? Because, when creating a plot it
is sometimes desirable to turn the frame off. To do that, replace plt.axis('on') with plt.
axis('off'). Having the command there ahead of time makes it easy to type 'off' over
'on' and vice versa to switch between the frame showing and not showing. Also, after
you have finished with a plot, you may wish to use it in a document, in which case you
may not want the frame. Note that 'on' and 'off' must appear in quotes, either single or
double.
1.6 The Plotting Grid
Line 11 of Listing 1-1, plt.grid(True), turns on the dotted grid lines, which can be an aid
when constructing a plot, especially when it comes time to position textual information.
If you do not include this command, the grid lines will not be shown. To turn off the
grid lines, change the True to False. Note the first letter in True and False is capitalized.
True and False do not appear in quotations marks. As with plt.axis(), having the plt.
grid(True) and plt.grid(False) commands there makes it easy to switch back and forth.
Again, note that both True and False must have the first letter capitalized and do not
appear in quotes.
1.7 Saving aPlot
The easiest way to save a plot that appears in the output pane is to put your cursor over it
and right-click. A window will appear allowing you to give it a name and specify where it
is to be saved. It will be saved the .png format. If you are planning to use it in a program
such as Photoshop, the .png format works. Some word processing and document
programs may require the .eps (encapsulated Postscript) format. If so, save it in the .png
format, open it in Photoshop, and resave it in the .eps format. It’s a bit cumbersome but
it works.
Chapter 1 essential python Commands andFunCtions
![](https://csdnimg.cn/release/download_crawler_static/10874818/bg14.jpg)
9
1.8 Grid Color
There are some options to the plt.grid() command. You can change the color of the grid
lines with the color='color' attribute. For example, plt.grid(True, color='b') plots a
blue grid. More color options will be defined shortly.
1.9 Tick Marks
The plt.grid(True) command will create a grid with Python’s own choice of spacing,
which may not be convenient. You can alter the spacings with the plt.xticks(xmin,
xmax, dx) and plt.yticks(ymin, ymax, dy) commands. min and max are the range of
the ticks; dx and dy are the spacing. While normally you want the tick marks to appear
over the full range of x and y, you can have them appear over a smaller range if you wish.
These commands appear in lines 23 and 24 of Listing 1-2.
Listing 1-2. Program TICK_MARKS
1 import numpy as np
2 import matplotlib.pyplot as plt
3
4 #————————————————plotting area
5 x1=-10
6 x2=140
7 y1=90
8 y2=-10
9 plt.axis([x1,x2,y1,y2])
10 plt.axis('on')
11
12 #——————————————————grid
13 plt.grid(True,color='b')
14 plt.title('Tick Mark Sample')
15
16 #————————————————tick marks
Chapter 1 essential python Commands andFunCtions
剩余364页未读,继续阅读
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://profile-avatar.csdnimg.cn/default.jpg!1)
TsiuHinghiok
- 粉丝: 6
- 资源: 64
上传资源 快速赚钱
我的内容管理 展开
我的资源 快来上传第一个资源
我的收益
登录查看自己的收益我的积分 登录查看自己的积分
我的C币 登录后查看C币余额
我的收藏
我的下载
下载帮助
![](https://csdnimg.cn/release/wenkucmsfe/public/img/voice.245cc511.png)
最新资源
- AirKiss技术详解:无线传递信息与智能家居连接
- Hibernate主键生成策略详解
- 操作系统实验:位示图法管理磁盘空闲空间
- JSON详解:数据交换的主流格式
- Win7安装Ubuntu双系统详细指南
- FPGA内部结构与工作原理探索
- 信用评分模型解析:WOE、IV与ROC
- 使用LVS+Keepalived构建高可用负载均衡集群
- 微信小程序驱动餐饮与服装业创新转型:便捷管理与低成本优势
- 机器学习入门指南:从基础到进阶
- 解决Win7 IIS配置错误500.22与0x80070032
- SQL-DFS:优化HDFS小文件存储的解决方案
- Hadoop、Hbase、Spark环境部署与主机配置详解
- Kisso:加密会话Cookie实现的单点登录SSO
- OpenCV读取与拼接多幅图像教程
- QT实战:轻松生成与解析JSON数据
资源上传下载、课程学习等过程中有任何疑问或建议,欢迎提出宝贵意见哦~我们会及时处理!
点击此处反馈
![](https://img-home.csdnimg.cn/images/20220527035711.png)
![](https://img-home.csdnimg.cn/images/20220527035711.png)
![](https://img-home.csdnimg.cn/images/20220527035111.png)
安全验证
文档复制为VIP权益,开通VIP直接复制
![](https://csdnimg.cn/release/wenkucmsfe/public/img/green-success.6a4acb44.png)