代码如下
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
def test4():
names = [ '电影1' , '电影2' , '电影3' ]
real_num1 = [ 7548 , 4013 , 1673 ]
real_num2 = [ 5453 , 1840 , 1080 ]
real_num3 = [ 4348 , 2345 , 1890 ]
x = np.arange( len (names))
# 绘制柱形图
width = 0.3
plt.bar(x, real_num1, alpha = 0.5 , width = width, label = names[ 0 ])
plt.bar([i + width for i in x], real_num2, alpha = 0.5 , width = width, label = names[ 1 ])
plt.bar([i + 2 * width for i in x], real_num3, alpha = 0.5 , width = width, label = names[ 2 ])
# 正常显示中文
plt.rcParams[ "font.sans-serif" ] = [ "SimHei" ]
# 设置x坐标轴的值
x_label = [ "第{}天" . format (i + 1 ) for i in x]
# 让x坐标轴显示在中间
plt.xticks([i + width for i in x], x_label)
# 添加ylabel
plt.ylabel( "票房数" )
# 添加图例
plt.legend()
# 添加标题
plt.title( "3天3部电影票房数" )
plt.show()
test4()
|
结果显示:
代码如下
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
|
from mpl_toolkits.mplot3d import Axes3Dimport matplotlib.pyplot as pltimport numpy as np
def test5():
# ax.plot_surface(X, Y, Z, rstride=1, cstride=1, cmap='rainbow') #绘面
# 绘制3D曲面图
fig = plt.figure()
ax = Axes3D(fig)
# -4 到4 [-4, 4),步长为0.25
X = np.arange( - 4 , 4 , 0.25 )
Y = np.arange( - 4 , 4 , 0.25 )
# meshgrid方法,你只需要构造一个表示x轴上的坐标的向量和一个表示y轴上的坐标的向量;然后作为参数给到meshgrid(),该函数就会返回相应维度的两个矩阵;
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 = "rainbow" )
plt.show()
|
结果如下:
代码如下
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
|
import matplotlib.pyplot as plt
import numpy as np
def test6():
# 绘制三维散点图
# ax.scatter(x[1000:4000],y[1000:4000],z[1000:4000],c='r') #绘点
data = np.random.randint( 0 , 255 , size = [ 40 , 40 , 40 ])
x, y, z = data[ 0 ], data[ 1 ], data[ 2 ]
# 创建一个三维的绘图工程
ax = plt.subplot( 111 , projection = "3d" )
# 将数据点分成三部分画,在颜色上有区分度
ax.scatter(x[: 10 ], y[: 10 ], z[: 10 ], c = 'y' ) # 绘制数据点
ax.scatter(x[ 10 : 20 ], y[ 10 : 20 ], z[ 10 : 20 ], c = 'r' )
ax.scatter(x[ 30 : 40 ], y[ 30 : 40 ], z[ 30 : 40 ], c = 'g' )
# 坐标轴
ax.set_zlabel( "Z" )
ax.set_ylabel( "Y" )
ax.set_xlabel( "X" )
plt.show()
|
效果如下:
以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持服务器之家。
原文链接:https://www.cnblogs.com/zhouzetian/p/12698465.html