python可視化1

繪製散點圖

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>

在這裏插入圖片描述

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]

在這裏插入圖片描述

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>

在這裏插入圖片描述

繪製餅狀圖

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()

在這裏插入圖片描述

多圖繪製到一個圖片上

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()		# 顯示窗口

在這裏插入圖片描述

多圖繪製到不同分佈上

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.

在這裏插入圖片描述

繪製三維立體圖

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.

在這裏插入圖片描述

在這裏插入圖片描述

繪製三維曲線

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()

在這裏插入圖片描述

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章