欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页

python可视化1

程序员文章站 2022-07-14 10:02:07
...

绘制散点图

import numpy as np
import matplotlib.pylab as pl
%matplotlib inline

a = np.arange(0, 2.0 * np.pi, 0.1)
print(a)
b = np.cos(a)
pl.scatter(a, b)
# pl.show()
[0.  0.1 0.2 0.3 0.4 0.5 0.6 0.7 0.8 0.9 1.  1.1 1.2 1.3 1.4 1.5 1.6 1.7
 1.8 1.9 2.  2.1 2.2 2.3 2.4 2.5 2.6 2.7 2.8 2.9 3.  3.1 3.2 3.3 3.4 3.5
 3.6 3.7 3.8 3.9 4.  4.1 4.2 4.3 4.4 4.5 4.6 4.7 4.8 4.9 5.  5.1 5.2 5.3
 5.4 5.5 5.6 5.7 5.8 5.9 6.  6.1 6.2]

<matplotlib.collections.PathCollection at 0x15465e2ff98>

python可视化1

import numpy as np
import matplotlib.pylab as pl
%matplotlib inline
x = np.random.random(100)  # 0-1
print(x[0:10])
y = np.random.random(100)
pl.scatter(x, y, s=x * 500, c=u'r', marker=u'*')
pl.show()
# s 指定大小, c指定颜色, marker指定符号形状, 
[0.15815532 0.65428075 0.80638782 0.17977135 0.86633134 0.60840757
 0.95604924 0.92410809 0.48354069 0.47104306]

python可视化1

import numpy as np
import matplotlib.pyplot as plt
%matplotlib  inline
# x = np.linspace(0,10,100)
x = np.arange(0, 6, 0.1) # 以0.1为单位,生成0到6的数据
y1 = np.sin(x)
y2 = np.cos(x)
plt.plot(x, y1, label="sin")
plt.plot(x, y2, linestyle = "--", label="cos") # 用虚线绘制
plt.xlabel("x") # x轴标签
plt.ylabel("y") # y轴标签
plt.title('sin & cos') # 标题
plt.legend()
# plt.show()
<matplotlib.legend.Legend at 0x1545addeb38>

python可视化1

绘制饼状图

import matplotlib.pyplot as plt
import numpy as np

%matplotlib inline

labels = ("Frogs", "Hogs", "Dogs", "Logs")
colors = ["yellowgreen", "gold", "#FF0000", "lightcoral"]
explode = (0, 0.1, 0, 0.1)  # 使饼状图中第二片和第四片分开
fig = plt.figure()
ax = fig.gca()

ax.pie(np.random.random(4),  		# 四个值
		explode    =	explode,
		labels     =	labels,
		colors     =	colors,
		autopct    =	"%1.1f%%",  # 百分比的格式 小数点保留一位, %% = "%"
		shadow     =	True, 		# 是否显示阴影
		startangle =	90,  		# 起始角度, 第一块从90度开始绘制, 从北向西转
		radius     =	0.25,		# 饼的半径
		center     =	(0, 0), 	# 中心位置
		frame      =	True)		# 

ax.set_xticks([0,1]) #设置坐标轴刻度
ax.set_yticks([0,1]) #设置坐标轴刻度
						# 0, 1被替换
ax.set_xticklabels(["Sunny", "Cloudy"]) #设置坐标轴显示的标签
ax.set_yticklabels(["Dry", "Rainy"]) #设置坐标轴显示的标签

ax.set_xlim((-0.5,1.5)) # 设置坐标轴跨度
ax.set_ylim((-0.5,1.5)) # 设置坐标轴跨度

ax.set_aspect('equal')  # 设置横纵比相等

plt.show()

python可视化1

多图绘制到一个图片上

import matplotlib.pyplot as plt
import numpy as np

%matplotlib inline
x = np.linspace(0, 2*np.pi, 500)
y = np.sin(x)
z = np.cos(x*x)
plt.figure(figsize=(8,4))

plt.plot(x,y,
	label="$sin(x)$", 
	color="red",  # 红色, 两个像素宽度
	linewidth=2) 

plt.plot(x,z,"b--", # 蓝色虚线
	label="$cos(x^2)$")

plt.xlabel("time(s)")
plt.ylabel("volt")
plt.title("sin and cos figure using pyplot")
plt.ylim(-1.2, 1.2)
plt.legend()  # 显示图例 左下角线的标识
plt.show()		# 显示窗口

python可视化1

多图绘制到不同分布上

import matplotlib.pyplot as plt
import numpy as np

%matplotlib inline
x = np.linspace(0, 2*np.pi, 500)
y1 = np.sin(x)
y2 = np.cos(x)
y3 = np.cos(x*x)

plt.figure(1)# 创建第一个图形

ax1 = plt.subplot(2,2,1) #第一行第一列第一个图形
plt.sca(ax1) # 选择ax1
plt.plot(x,y1,color="red")
plt.ylim = (-1.2, 1.2) # 限制y轴范围

ax2 = plt.subplot(2,2,2) #第一行第2列第2个图形
plt.sca(ax2) # 选择ax2
plt.plot(x,y2,"b--")
plt.ylim = (-1.2, 1.2) # 限制y轴范围

ax3 = plt.subplot(212, facecolor='r') # 背景色red
plt.sca(ax3) # 选择ax3
plt.plot(x,y3,"g--")
plt.ylim = (-1.2, 1.2) # 限制y轴范围

plt.legend()  # 显示图例 左下角线的标识
plt.show()		# 显示窗口

No handles with labels found to put in legend.

python可视化1

绘制三维立体图

import matplotlib.pyplot as plt
import numpy as np
import mpl_toolkits.mplot3d

x,y = np.mgrid[-2:2:20j, -2:2:20j] #步长使用j, 从-2到2分为了20个点
# 得到了20*20  400个点, 后面的j标识包含终点2
# print(x)
z = 50*np.sin(x+y)

ax = plt.subplot(111, projection='3d')
ax.plot_surface(x, y, z,  # 画一个面
	rstride=2, 
	cstride=1, 
	cmap=plt.cm.Blues_r)

ax.set_xlabel("X")
ax.set_ylabel("Y")
ax.set_zlabel("Z")

plt.legend()  # 显示图例 左下角线的标识
plt.show()		# 显示窗口


rho,theta = np.mgrid[0:1:40j, 0:2*np.pi:40j]
z = rho**2
x = rho*np.cos(theta)
y = rho*np.sin(theta)
ax = plt.subplot(111, projection="3d")
ax.plot_surface(x,y,z)
plt.show()

No handles with labels found to put in legend.

python可视化1

python可视化1

绘制三维曲线

import matplotlib.pyplot as plt
import matplotlib as mpl
from mpl_toolkits.mplot3d import Axes3D
import numpy as np
# import mpl_toolkits.mplot3d


mpl.rcParams["legend.fontsize"] = 10  # 图例字号
fig = plt.figure()
ax = fig.gca(projection="3d")
theta = np.linspace(-4 * np.pi, 4 * np.pi, 100)
z = np.linspace(-4, 4, 100) * 0.3
r = z**3 + 1
x = r * np.sin(theta)
y = r * np.cos(theta)
ax.plot(x, y, z, label="parametric curve")
ax.legend()
plt.show()

python可视化1

相关标签: Python