Skip to content

Commit de66978

Browse files
committed
Matplotlib examples
Matplotlib的案例
1 parent d3e7961 commit de66978

29 files changed

Lines changed: 602 additions & 33 deletions

PythonDemo/.spyproject/workspace.ini

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,5 +6,5 @@ save_non_project_files = False
66

77
[main]
88
version = 0.1.0
9-
recent_files = ['E:\\GitHubWorkplace\\Python\\PythonDemo\\spider\\bs4JokeToExcel.py', 'E:\\GitHubWorkplace\\Python\\PythonDemo\\spider\\bs4WangYiYunToExcel.py', 'E:\\GitHubWorkplace\\Python\\PythonDemo\\saveToExcel\\xlwtDemo.py', 'E:\\GitHubWorkplace\\Python\\PythonDemo\\saveToExcel\\xwltDemo2.py', 'E:\\GitHubWorkplace\\Python\\PythonDemo\\spider\\bs4WangYiYunToExcel2.py', 'E:\\GitHubWorkplace\\Python\\PythonDemo\\spider\\bs4quickstart.py', 'E:\\GitHubWorkplace\\Python\\PythonDemo\\spider\\bs4Meizitu.py', 'E:\\GitHubWorkplace\\Python\\PythonDemo\\spider\\urllib.requestDemo1.py', 'E:\\GitHubWorkplace\\Python\\PythonDemo\\spider\\weixin.py', 'E:\\GitHubWorkplace\\Python\\PythonDemo\\spider\\weixinItchatSexAs.py', 'E:\\GitHubWorkplace\\Python\\PythonDemo\\spider\\weixinchatRobotGroup.py', 'E:\\GitHubWorkplace\\Python\\PythonDemo\\spider\\weixinchatRobotSingle.py', 'E:\\GitHubWorkplace\\Python\\PythonDemo\\spider\\bs4WangYiYun.py', 'E:\\GitHubWorkplace\\Python\\PythonDemo\\spider\\qqbotRobot.py', 'E:\\GitHubWorkplace\\Python\\PythonDemo\\spider\\weixinQianming.py', 'E:\\GitHubWorkplace\\Python\\PythonDemo\\numpy\\arrayboolean.py', 'D:\\Anaconda3\\lib\\site-packages\\matplotlib\\pyplot.py', 'E:\\GitHubWorkplace\\Python\\PythonDemo\\wordcloud\\simple.py', 'D:\\Anaconda3\\lib\\site-packages\\wordcloud\\__init__.py', 'E:\\GitHubWorkplace\\Python\\PythonDemo\\wordcloud\\examples\\a_new_hope.py', 'E:\\GitHubWorkplace\\Python\\PythonDemo\\wordcloud\\examples\\a_new_hope.txt', 'E:\\GitHubWorkplace\\Python\\PythonDemo\\wordcloud\\examples\\colored_by_group.py', 'E:\\GitHubWorkplace\\Python\\PythonDemo\\wordcloud\\examples\\emoji.py', 'E:\\GitHubWorkplace\\Python\\PythonDemo\\wordcloud\\examples\\frequency.py', 'E:\\GitHubWorkplace\\Python\\PythonDemo\\wordcloud\\examples\\happy-emoji.txt', 'E:\\GitHubWorkplace\\Python\\PythonDemo\\wordcloud\\examples\\masked.py', 'E:\\GitHubWorkplace\\Python\\PythonDemo\\readme.txt', 'E:\\GitHubWorkplace\\Python\\PythonDemo\\wordcloud\\examples\\colored.py', 'D:\\Anaconda3\\lib\\site-packages\\PIL\\Image.py']
9+
recent_files = ['E:\\GitHubWorkplace\\Python\\PythonDemo\\Matplotlib\\demo19.py', 'E:\\GitHubWorkplace\\Python\\PythonDemo\\Matplotlib\\demo18.py']
1010

PythonDemo/Matplotlib/demo1.py

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
# -*- coding: utf-8 -*-
2+
"""
3+
简单图形绘制
4+
5+
"""
6+
7+
import matplotlib.pyplot as plt
8+
import numpy as np
9+
10+
#从-1-----1之间等间隔采66个数.也就是说所画出来的图形是66个点连接得来的
11+
#注意:如果点数过小的话会导致画出来二次函数图像不平滑
12+
x = np.linspace(-1, 1,66)
13+
# 绘制y=2x+1函数的图像
14+
y = 2 * x + 1
15+
plt.plot(x, y)
16+
plt.show()
17+
18+
# 绘制x^2函数的图像
19+
y = x**2
20+
plt.plot(x, y)
21+
plt.show()

PythonDemo/Matplotlib/demo10.py

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
# -*- coding: utf-8 -*-
2+
"""
3+
绘制登高线图
4+
"""
5+
import matplotlib.pyplot as plt
6+
import numpy as np
7+
8+
# 定义等高线高度函数
9+
def f(x, y):
10+
return (1 - x / 2 + x ** 5 + y ** 3) * np.exp(- x ** 2 - y ** 2)
11+
12+
# 数据数目
13+
n = 256
14+
# 定义x, y
15+
x = np.linspace(-3, 3, n)
16+
y = np.linspace(-3, 3, n)
17+
18+
# 生成网格数据
19+
X, Y = np.meshgrid(x, y)
20+
21+
22+
# 填充等高线的颜色, 8是等高线分为几部分
23+
plt.contourf(X, Y, f(X, Y), 8, alpha = 0.75, cmap = plt.cm.hot)
24+
# 绘制等高线
25+
C = plt.contour(X, Y, f(X, Y), 8, colors = 'black', linewidth = 0.5)
26+
# 绘制等高线数据
27+
plt.clabel(C, inline = True, fontsize = 10)
28+
29+
# 去除坐标轴
30+
plt.xticks(())
31+
plt.yticks(())
32+
plt.show()

PythonDemo/Matplotlib/demo11.py

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
# -*- coding: utf-8 -*-
2+
"""
3+
绘制Image
4+
"""
5+
import matplotlib.pyplot as plt
6+
import numpy as np
7+
8+
# 定义图像数据
9+
a = np.linspace(0, 1, 9).reshape(3, 3)
10+
# 显示图像数据
11+
plt.imshow(a, interpolation = 'nearest', cmap = 'bone', origin = 'lower')
12+
# 添加颜色条
13+
plt.colorbar()
14+
# 去掉坐标轴
15+
plt.xticks(())
16+
plt.yticks(())
17+
plt.show()

PythonDemo/Matplotlib/demo12.py

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
# -*- coding: utf-8 -*-
2+
"""
3+
绘制3d图形
4+
"""
5+
6+
import matplotlib.pyplot as plt
7+
import numpy as np
8+
from mpl_toolkits.mplot3d import Axes3D
9+
# 定义figure
10+
fig = plt.figure()
11+
# 将figure变为3d
12+
ax = Axes3D(fig)
13+
14+
# 数据数目
15+
n = 256
16+
# 定义x, y
17+
x = np.arange(-4, 4, 0.25)
18+
y = np.arange(-4, 4, 0.25)
19+
20+
# 生成网格数据
21+
X, Y = np.meshgrid(x, y)
22+
23+
# 计算每个点对的长度
24+
R = np.sqrt(X ** 2 + Y ** 2)
25+
# 计算Z轴的高度
26+
Z = np.sin(R)
27+
28+
# 绘制3D曲面
29+
ax.plot_surface(X, Y, Z, rstride = 1, cstride = 1, cmap = plt.get_cmap('rainbow'))
30+
# 绘制从3D曲面到底部的投影
31+
ax.contour(X, Y, Z, zdim = 'z', offset = -2, cmap = 'rainbow')
32+
33+
# 设置z轴的维度
34+
ax.set_zlim(-2, 2)
35+
36+
plt.show()

PythonDemo/Matplotlib/demo13.py

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
# -*- coding: utf-8 -*-
2+
"""
3+
subplot绘制多图
4+
"""
5+
6+
import matplotlib.pyplot as plt
7+
8+
plt.figure()
9+
10+
# 绘制第一个图
11+
plt.subplot(2, 2, 1)
12+
plt.plot([0, 1], [0, 1])
13+
# 绘制第二个图
14+
plt.subplot(2, 2, 2)
15+
plt.plot([0, 1], [0, 1])
16+
# 绘制第三个图
17+
plt.subplot(2, 2, 3)
18+
plt.plot([0, 1], [0, 1])
19+
# 绘制第四个图
20+
plt.subplot(2, 2, 4)
21+
plt.plot([0, 1], [0, 1])
22+
plt.show()

PythonDemo/Matplotlib/demo14.py

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
# -*- coding: utf-8 -*-
2+
"""
3+
subplot绘制多图
4+
"""
5+
6+
import matplotlib.pyplot as plt
7+
8+
plt.figure()
9+
10+
# 绘制第一个图
11+
plt.subplot(2, 1, 1)
12+
plt.plot([0, 1], [0, 1])
13+
# 绘制第二个图
14+
plt.subplot(2, 3, 4)
15+
plt.plot([0, 1], [0, 1])
16+
# 绘制第三个图
17+
plt.subplot(2, 3, 5)
18+
plt.plot([0, 1], [0, 1])
19+
# 绘制第四个图
20+
plt.subplot(2, 3, 6)
21+
plt.plot([0, 1], [0, 1])
22+
plt.show()

PythonDemo/Matplotlib/demo15.py

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
# -*- coding: utf-8 -*-
2+
"""
3+
figure绘制多图
4+
"""
5+
import matplotlib.pyplot as plt
6+
7+
# 定义figure
8+
plt.figure()
9+
# figure分成3行3列, 取得第一个子图的句柄, 第一个子图跨度为1行3列, 起点是表格(0, 0)
10+
ax1 = plt.subplot2grid((3, 3), (0, 0), colspan = 3, rowspan = 1)
11+
ax1.plot([0, 1], [0, 1])
12+
ax1.set_title('Test')
13+
14+
# figure分成3行3列, 取得第二个子图的句柄, 第二个子图跨度为1行3列, 起点是表格(1, 0)
15+
ax2 = plt.subplot2grid((3, 3), (1, 0), colspan = 2, rowspan = 1)
16+
ax2.plot([0, 1], [0, 1])
17+
18+
# figure分成3行3列, 取得第三个子图的句柄, 第三个子图跨度为1行1列, 起点是表格(1, 2)
19+
ax3 = plt.subplot2grid((3, 3), (1, 2), colspan = 1, rowspan = 1)
20+
ax3.plot([0, 1], [0, 1])
21+
22+
# figure分成3行3列, 取得第四个子图的句柄, 第四个子图跨度为1行3列, 起点是表格(2, 0)
23+
ax4 = plt.subplot2grid((3, 3), (2, 0), colspan = 3, rowspan = 1)
24+
ax4.plot([0, 1], [0, 1])
25+
26+
plt.show()

PythonDemo/Matplotlib/demo16.py

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
# -*- coding: utf-8 -*-
2+
"""
3+
figure绘制多图
4+
"""
5+
6+
import matplotlib.pyplot as plt
7+
import matplotlib.gridspec as gridspec
8+
9+
# 定义figure
10+
plt.figure()
11+
# 分隔figure
12+
gs = gridspec.GridSpec(3, 3)
13+
ax1 = plt.subplot(gs[0, :])
14+
ax2 = plt.subplot(gs[1, 0:2])
15+
ax3 = plt.subplot(gs[1, 2])
16+
ax4 = plt.subplot(gs[2, :])
17+
18+
# 绘制图像
19+
ax1.plot([0, 1], [0, 1])
20+
ax1.set_title('Test')
21+
22+
ax2.plot([0, 1], [0, 1])
23+
24+
ax3.plot([0, 1], [0, 1])
25+
26+
ax4.plot([0, 1], [0, 1])
27+
28+
plt.show()

PythonDemo/Matplotlib/demo17.py

Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
# -*- coding: utf-8 -*-
2+
"""
3+
figure图的嵌套
4+
"""
5+
6+
import matplotlib.pyplot as plt
7+
8+
# 定义figure
9+
fig = plt.figure()
10+
11+
# 定义数据
12+
x = [1, 2, 3, 4, 5, 6, 7]
13+
y = [1, 3, 4, 2, 5, 8, 6]
14+
15+
# figure的百分比, 从figure 10%的位置开始绘制, 宽高是figure的80%
16+
left, bottom, width, height = 0.1, 0.1, 0.8, 0.8
17+
# 获得绘制的句柄
18+
ax1 = fig.add_axes([left, bottom, width, height])
19+
# 绘制点(x,y)
20+
ax1.plot(x, y, 'r')
21+
ax1.set_xlabel('x')
22+
ax1.set_ylabel('y')
23+
ax1.set_title('test')
24+
25+
26+
# 嵌套方法一
27+
# figure的百分比, 从figure 10%的位置开始绘制, 宽高是figure的80%
28+
left, bottom, width, height = 0.2, 0.6, 0.25, 0.25
29+
# 获得绘制的句柄
30+
ax2 = fig.add_axes([left, bottom, width, height])
31+
# 绘制点(x,y)
32+
ax2.plot(x, y, 'r')
33+
ax2.set_xlabel('x')
34+
ax2.set_ylabel('y')
35+
ax2.set_title('part1')
36+
37+
38+
# 嵌套方法二
39+
plt.axes([bottom, left, width, height])
40+
plt.plot(x, y, 'r')
41+
plt.xlabel('x')
42+
plt.ylabel('y')
43+
plt.title('part2')
44+
45+
plt.show()

0 commit comments

Comments
 (0)