Python绘图

http://matplotlib.org/examples/images_contours_and_fields/interpolation_methods.html

1
2
3
4
5
6
7
8
9
import matplotlib.pyplot as plt
import numpy as np

#从-1到1之间取50个点
X = np.linspace(-1,1,50)
y = 2*X+1
#展示z = 2*X+1
plt.plot(X,y)
plt.show()
PYTHON

figure以及num参数

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
import matplotlib.pyplot as plt
import numpy as np

#从-3到3之间取50个点
X = np.linspace(-3,3,50)
y1 = 2*X+1
y2 = X**2

#展示z = 2*X+1
plt.figure()
plt.plot(X,y1)

plt.figure(num = 3,figsize=(8,5))
plt.plot(X,y2)


plt.show()
APACHE

1
2
3
4
5
6
7
8
9
10
11
12
13
import matplotlib.pyplot as plt
import numpy as np

#从-3到3之间取50个点
X = np.linspace(-3,3,50)
y1 = 2*X+1
y2 = X**2

plt.figure(num = 2,figsize=(8,5))
plt.plot(X,y2,color = 'red',linewidth = 1.0,linestyle = '--')
plt.plot(X,y1,color = 'blue',linewidth = 3.0,linestyle = '-')

plt.show()
PYTHON

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
import matplotlib.pyplot as plt
import numpy as np

#从-3到3之间取50个点
X = np.linspace(-3,3,50)
y1 = 2*X+1
y2 = X**2


plt.figure(num = 3,figsize=(8,5))
plt.plot(X,y2,color = 'red',linewidth = 1.0,linestyle = '--')
plt.plot(X,y1,color = 'blue',linewidth = 3.0,linestyle = '-')

#坐标轴的取值范围
plt.xlim((-1,2))
plt.ylim((-2,3))

#设置坐标轴的标签
plt.xlabel('I am x')
plt.ylabel('I am y')

#设置刻度标记
new_ticks = np.linspace(-1,2,5)
print(new_ticks)
plt.xticks(new_ticks)
plt.yticks([-2,-1.8,-1,1.22,3],
[r'$really\ bad$',r'$bad\ \alpha$',r'$normal$',r'$good$',r'$really\ good$'])

plt.show()
PYTHON

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
import matplotlib.pyplot as plt
import numpy as np

#从-3到3之间取50个点
X = np.linspace(-3,3,50)
y1 = 2*X+1
y2 = X**2


plt.figure(num = 3,figsize=(8,5))
plt.plot(X,y2,color = 'red',linewidth = 1.0,linestyle = '--')
plt.plot(X,y1,color = 'blue',linewidth = 3.0,linestyle = '-')

#坐标轴的取值范围
plt.xlim((-1,2))
plt.ylim((-2,3))

#设置坐标轴的标签
plt.xlabel('I am x')
plt.ylabel('I am y')

#设置刻度标记
new_ticks = np.linspace(-1,2,5)
print(new_ticks)
plt.xticks(new_ticks)
plt.yticks([-2,-1.8,-1,1.22,3],
[r'$really\ bad$',r'$bad\ \alpha$',r'$normal$',r'$good$',r'$really\ good$'])

# gca = get current axis,即ax获取上面的坐标轴
ax = plt.gca()
ax.spines['right'].set_color('none')
ax.spines['top'].set_color('none')
ax.xaxis.set_ticks_position('bottom')
ax.yaxis.set_ticks_position('left')
ax.spines['bottom'].set_position(('data',0))
ax.spines['left'].set_position(('data',0))


plt.show()
PYTHON

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
import matplotlib.pyplot as plt
import numpy as np

#从-3到3之间取50个点
X = np.linspace(-3,3,50)
y1 = 2*X+1
y2 = X**2


plt.figure(num = 5,figsize=(8,5))

#坐标轴的取值范围
plt.xlim((-1,2))
plt.ylim((-2,3))

#设置坐标轴的标签
plt.xlabel('I am x')
plt.ylabel('I am y')

#设置刻度标记
new_ticks = np.linspace(-1,2,5)
print(new_ticks)
plt.xticks(new_ticks)
plt.yticks([-2,-1.8,-1,1.22,3],
[r'$really\ bad$',r'$bad\ \alpha$',r'$normal$',r'$good$',r'$really\ good$'])

l1, = plt.plot(X,y1,color = 'blue',linewidth = 3.0,linestyle = '-',label='down')
l2, = plt.plot(X,y2,color = 'red',linewidth = 1.0,linestyle = '--',label='up')

plt.legend(handles=[l1,l2,],labels=['aaa','bbb'],loc='best')


plt.show()
PYTHON

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
import matplotlib.pyplot as plt
import numpy as np

#从-3到3之间取50个点
X = np.linspace(-3,3,50)
y1 = 2*X+1


plt.figure(num = 6,figsize=(8,5))
plt.plot(X,y1,color = 'b',linewidth = 1.0,linestyle = '-')


# gca = get current axis,即ax获取上面的坐标轴
ax = plt.gca()
ax.spines['right'].set_color('none')
ax.spines['top'].set_color('none')
ax.xaxis.set_ticks_position('bottom')
ax.yaxis.set_ticks_position('left')
ax.spines['bottom'].set_position(('data',0))
ax.spines['left'].set_position(('data',0))

#画点
X_1 = 1
y_1 = 2*X_1+1
plt.scatter(X_1,y_1,s=50,color='b')
plt.plot([X_1,X_1],[y_1,0],'k--',lw=2.5)

#标注方法一
plt.annotate(r'$2x+1=%s$'%y_1,xy=(X_1,y_1),xycoords='data',
xytext=(+30,-30),textcoords='offset points',fontsize=16,
arrowprops=dict(arrowstyle='->',connectionstyle='arc3,rad=.2'))

#标注方法二
plt.text(-3.7,3,r'$This\ is\ the\ some\ text.\ \mu\ \sigma_i\ \alpha_t$',fontdict={'size':16,'color':'r'})

plt.show()
PYTHON

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
import matplotlib.pyplot as plt
import numpy as np

#从-3到3之间取50个点
X = np.linspace(-3,3,50)
y1 = 0.1*X


plt.figure(num = 7,figsize=(8,5))
plt.plot(X,y1,linewidth = 10,zorder=1)
plt.ylim((-2,2))

# gca = get current axis,即ax获取上面的坐标轴
ax = plt.gca()
ax.spines['right'].set_color('none')
ax.spines['top'].set_color('none')
ax.xaxis.set_ticks_position('bottom')
ax.yaxis.set_ticks_position('left')
ax.spines['bottom'].set_position(('data',0))
ax.spines['left'].set_position(('data',0))

for label in ax.get_xticklabels() + ax.get_yticklabels():
label.set_fontsize(12)
label.set_zorder(2)
label.set_bbox(dict(facecolor='white',edgecolor='None',alpha=0.7))

plt.show()
PYTHON

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
import matplotlib.pyplot as plt
import numpy as np

n = 1024
X = np.random.normal(0, 1, n)
Y = np.random.normal(0, 1, n)
#for color values
T = np.arctan2(Y, X)

plt.figure(num = 8,figsize=(8,5))
plt.scatter(X, Y, s=75, c=T, alpha=0.5)

plt.xlim(-1.5, 1.5)
plt.ylim(-1.5, 1.5)

plt.xticks(())
plt.yticks(())


plt.show()
PYTHON

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
import matplotlib.pyplot as plt
import numpy as np

n = 12
X = np.arange(n)
Y1 = (1-X/float(n)) * np.random.uniform(0.5, 1.0, size=n)
Y2 = (1-X/float(n)) * np.random.uniform(0.5, 1.0, size=n)

plt.xlim(-1,n)
plt.xticks(())
plt.ylim(-1.25,1.25)
plt.yticks(())

plt.bar(X, +Y1, facecolor='#9999ff', edgecolor='white')
plt.bar(X, -Y2, facecolor='#ff9999', edgecolor='white')

for x,y in zip(X,Y1):
plt.text(x, y+0.05, '%.2f' % y, ha='center', va='bottom')

for x,y in zip(X,Y2):
plt.text(x, -y-0.05, '-%.2f' % y, ha='center', va='top')

plt.show()
PYTHON

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
import matplotlib.pyplot as plt
import numpy as np

def f(x,y):
return (1-x/2+x**5+y**3)*np.exp(-x**2-y**2)

n = 256
x = np.linspace(-3,3,n)
y = np.linspace(-3,3,n)

#生成网格数据
X,Y = np.meshgrid(x,y)

#颜色
plt.contourf(X,Y,f(X,Y),8,alpha=.75,cmap=plt.cm.hot)

#添加颜色条,上面0是两部分,上面8就是分十个部分
C = plt.contour(X,Y,f(X,Y),10,colors='black',linewidth=.5)

plt.clabel(C,inline=True,fontsize=10)

plt.xticks(())
plt.yticks(())
plt.show()
APACHE

1
2
3
4
5
6
7
8
9
10
11
12
13
import matplotlib.pyplot as plt
import numpy as np

a = np.array([[0.31366614,0.69465866,0.2340829],
[0.23093019,0.45411806,0.2127864],
[0.43514938,0.51051050,0.7419751]]).reshape(3,3)

plt.imshow(a,interpolation='nearest',cmap='bone',origin='upper')
plt.colorbar(shrink=0.9)

plt.xticks(())
plt.yticks(())
plt.show()
STYLUS

)

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
import numpy as np
import matplotlib.pyplot as plt
from mpl_toolkits.mplot3d import Axes3D

fig = plt.figure(num = 12,figsize=(8,5))
ax = Axes3D(fig,auto_add_to_figure=False)
fig.add_axes(ax)

X = np.arange(-4,4,0.25)
Y = np.arange(-4,4,0.25)

#将矩阵X,Y变成底面网格
X,Y = np.meshgrid(X,Y)
R = np.sqrt(X**2+Y**2)
Z = np.sin(R)

ax.plot_surface(X,Y,Z,rstride=1,cstride=1,cmap=plt.get_cmap('rainbow'))
#映射等高图
ax.contourf(X,Y,Z,zdir='z',offset=-2,cmap=plt.get_cmap('rainbow'))

ax.set_zlim(-2,2)

plt.show()
TP

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
import matplotlib.pyplot as plt


plt.figure(num = 13,figsize=(8,5))

plt.subplot(2,1,1)
plt.plot([0,1],[0,1])

plt.subplot(2,3,4)
plt.plot([0,1],[0,2])

plt.subplot(2,3,5)
plt.plot([0,1],[0,3])

plt.subplot(236)
plt.plot([0,1],[0,4])

plt.show()
PYTHON
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
import matplotlib.pyplot as plt
import matplotlib.gridspec as grid

############ method1 ############
# fig = plt.figure(num = 15,figsize=(8,5))
# # 从00 开始,占3行,3列,采取了一个行三个列的图
# ax1 = plt.subplot2grid((3,3),(0,0),colspan=3,rowspan=1)
# ax1.plot([1,2],[1,2])
# ax1.set_title('ax1_title')

# ax2 = plt.subplot2grid((3,3),(1,0),colspan=2)
# ax2.plot([1,2],[1,2])

# ax3 = plt.subplot2grid((3,3),(1,2),rowspan=2)
# ax4 = plt.subplot2grid((3,3),(2,0))
# ax4 = plt.subplot2grid((3,3),(2,1))

############# method2 ###########
# fig = plt.figure(num = 15,figsize=(8,5))
# gs = grid.GridSpec(3,3)
# ax1 = plt.subplot(gs[0,:])
# ax2 = plt.subplot(gs[1,:2])
# ax3 = plt.subplot(gs[1:,2])
# ax4 = plt.subplot(gs[-1,0])
# ax5 = plt.subplot(gs[-1,-2])

############ method3 ###########
f,((ax11,ax12),(ax21,ax22)) = plt.subplots(2,2,sharex=True)
ax11.scatter([1,2],[1,2])


plt.tight_layout()
plt.show()

CLEAN

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
import matplotlib.pyplot as plt
import matplotlib.gridspec as gridspec

fig = plt.figure(num = 14,figsize=(8,5))
x = [1,2,3,4,5,6,7]
y = [1,3,4,2,5,8,6]

left, bottom,width,height = 0.1, 0.1, 0.8, 0.8
ax1 = fig.add_axes([left, bottom, width, height])
ax1.plot(x,y,'r')
ax1.set_xlabel('x')
ax1.set_ylabel('y')
ax1.set_title('title')

left, bottom,width,height = 0.2, 0.6, 0.25, 0.25
ax2 = fig.add_axes([left, bottom, width, height])
ax2.plot(x,y,'b')
ax2.set_xlabel('x')
ax2.set_ylabel('y')
ax2.set_title('title inside 1')

plt.axes([0.6,0.2,0.25,0.25])
plt.plot(x,y,'g')
plt.xlabel('x')
plt.ylabel('y')
plt.title('title inside 2')

plt.show()

STYLUS


Python绘图
http://example.com/2024/12/15/20241215_Python绘图/
作者
XuanYa
发布于
2024年12月15日
许可协议