python繪圖之坐標軸的超詳細講解
1. 2D坐標軸
1.1 繪制簡單的曲線
import matplotlib.pyplot as plt import numpy as np x=np.linspace(-1,1,50)#-1到1中畫50個點 y=x**2 plt.plot(x,y,color='green') plt.tick_params(axis='x',colors='blue') plt.tick_params(axis='y',colors='red') plt.show()
作圖:
1.2 坐標軸的刻度線向內(nèi)
import matplotlib.pyplot as plt import numpy as np x=np.linspace(-1,1,50)#-1到1中畫50個點 y=x**2 # 下面兩行代碼要放在plt.plot的前面 plt.rcParams['xtick.direction'] = 'in'#將x軸的刻度線方向設置向內(nèi) plt.rcParams['ytick.direction'] = 'in'#將y軸的刻度線方向設置向內(nèi) plt.plot(x,y,color='green') plt.tick_params(axis='x',colors='blue') plt.tick_params(axis='y',colors='red') plt.show()
1.3 將坐標刻度從整0開始
plt.margins(x=0) plt.margins(y=0) #設置坐標軸范圍 #plt.ylim([0,0.8]) #plt.xlim([0,0.75])
1.4 設置刻度柵格
簡單的刻度:
plt.grid()
主刻度和次刻度
import matplotlib.pyplot as plt import numpy as np plt.figure(figsize=(17,3)) ax = plt.gca() ax.xaxis.set_major_locator(plt.MultipleLocator(0.2)) #設置x軸主刻度 ax.xaxis.set_minor_locator(plt.MultipleLocator(0.04)) #設置x軸次刻度 ax.yaxis.set_major_locator(plt.MultipleLocator(0.5)) #設置x軸次刻度 ax.yaxis.set_minor_locator(plt.MultipleLocator(0.1)) #設置x軸次刻度 ax.grid(which='major',axis="both",linewidth=0.75,linestyle='-',color='r') ax.grid(which='minor',axis="both",linewidth=0.25,linestyle='-',color='r') x=np.linspace(0,3*np.pi,50)#-1到1中畫50個點 y=2*np.sin(x) plt.plot(x,y) plt.margins(x=0) plt.margins(y=0) plt.show()
下面的圖形類似于心電圖紙繪制,關于心電圖相關的繪制,具體在使用python繪制心電圖中體現(xiàn):
1.5 不顯示坐標
只是坐標刻度不可見,兩種方式的效果一樣
# plt.xticks([]) plt.yticks([]) # 或者下面的 # frame.axes.get_xaxis().set_visible(False) # x 軸不可見 frame.axes.get_yaxis().set_visible(False) # y 軸不可見
關閉兩者坐標軸,只有曲線圖形
plt.axis('off')
1.6 坐標值
為坐標設置刻度值, 并且將刻度值旋轉45度
import matplotlib.pyplot as plt import numpy as np x=np.linspace(0,2,50)#-1到1中畫50個點 y=x**2 plt.rcParams['xtick.direction'] = 'in'#將x軸的刻度線方向設置向內(nèi) plt.rcParams['ytick.direction'] = 'in'#將y軸的刻度線方向設置向內(nèi) ax = plt.gca() xlabel=[str(val)+'_1' for val in range(100,110)] ax.set_xticklabels(xlabel) plt.xticks(rotation = 45) plt.plot(x,y,color='green') plt.tick_params(axis='x',colors='blue') plt.tick_params(axis='y',colors='red') plt.margins(x=0) plt.margins(y=0) plt.show()
1.7 繪制橫線和豎線
plt.axvline(1) plt.axhline(1.5)
1.8 設置坐標點的顏色
下圖中設置y軸第3個坐標值的顏色為黃色,x軸第6個坐標值顏色為綠色。
import matplotlib.pyplot as plt import numpy as np x=np.linspace(0,2,50)#-1到1中畫50個點 y=x**2 plt.rcParams['xtick.direction'] = 'in'#將x軸的刻度線方向設置向內(nèi) plt.rcParams['ytick.direction'] = 'in'#將y軸的刻度線方向設置向內(nèi) ax = plt.gca() plt.plot(x,y,color='green') plt.tick_params(axis='x',colors='blue') plt.tick_params(axis='y',colors='red') ax.get_yticklabels()[3].set_color("y") ax.get_xticklabels()[6].set_color("g") plt.show()
1.9 雙坐標
代碼如下:
import numpy as np import matplotlib.pyplot as plt t = np.arange(0.01, 10.0, 0.01) data1 = np.exp(t) data2 = np.sin(2 * np.pi * t) fig, ax1 = plt.subplots() color = 'tab:red' ax1.set_xlabel('time (s)') ax1.set_ylabel('exp', color=color) ax1.plot(t, data1, color=color) ax1.tick_params(axis='y', labelcolor=color) ax2 = ax1.twinx() # instantiate a second axes that shares the same x-axis color = 'tab:blue' ax2.set_ylabel('sin', color=color) # we already handled the x-label with ax1 ax2.plot(t, data2, color=color) ax2.tick_params(axis='y', labelcolor=color) fig.tight_layout() # otherwise the right y-label is slightly clipped plt.show()
2. 3D坐標軸
2.1 繪制3D散點圖
關鍵代碼ax.scatter(xs, ys, zs, c=c, marker=m)
,輸入數(shù)據(jù)xs,ys,zs是相同長度的一維數(shù)據(jù)。c是顏色,marker是散點類型。
from mpl_toolkits.mplot3d import Axes3D import matplotlib.pyplot as plt import numpy as np def randrange(n, vmin, vmax): return (vmax - vmin)*np.random.rand(n) + vmin fig = plt.figure() ax = fig.add_subplot(111, projection='3d') n = 100 for c, m, zlow, zhigh in [('r', '*', -10, 20), ('b', 'o', -30, -10)]: xs = randrange(n, 23, 32) ys = randrange(n, 0, 100) zs = randrange(n, zlow, zhigh) ax.scatter(xs, ys, zs, c=c, marker=m) ax.set_xlabel('X Label') ax.set_ylabel('Y Label') ax.set_zlabel('Z Label') plt.show()
2.2 繪制3D曲面圖
import numpy as np import matplotlib.pyplot as plt from mpl_toolkits.mplot3d import Axes3D fig=plt.figure(num=1,figsize=(8,6)) ax = Axes3D(fig) X = np.arange(-4, 4, 0.25) Y = np.arange(-4, 4, 0.25) X, Y = np.meshgrid(X, Y) R = np.sqrt(X ** 2 + Y ** 2) # height value Z = np.cos(R) ax.plot_surface(X, Y, Z, rstride=1, cstride=1, cmap=plt.get_cmap('rainbow'),edgecolors='white') ax.contourf(X, Y, Z, zdir='z', offset=-2, cmap=plt.get_cmap('rainbow'))#投影等高線,改變zdir='x', offset=-4實現(xiàn)投影到不同坐標軸 ax.set_zlim(-2, 2) ax.tick_params(axis='x',colors='g') ax.tick_params(axis='y',colors='g') ax.tick_params(axis='z',colors='g') plt.show()
2.3 繪制3D柱形圖
import numpy as np import matplotlib.pyplot as plt from mpl_toolkits.mplot3d import Axes3D from matplotlib import cm X=np.arange(0, 9, step=1)#X軸的坐標 Y=np.arange(0, 10, step=1)#Y軸的坐標 arr = [[np.random.randint(1,50) for i in range(9)] for i in range(10)] Z = np.array(arr) xx, yy=np.meshgrid(X, Y)#網(wǎng)格化坐標 X, Y=xx.ravel(), yy.ravel()#矩陣扁平化 bottom=np.zeros_like(X)#設置柱狀圖的底端位值 Z=Z.ravel()#扁平化矩陣 width=height=0.8#每一個柱子的長和寬 #繪圖設置 fig=plt.figure() ax=fig.gca(projection='3d')#三維坐標軸 ax.bar3d(X, Y, bottom, width, height, Z, shade=True,color='lightgreen')# #坐標軸設置 ax.set_xlabel('X') ax.set_ylabel('Y') ax.set_zlabel('Z') plt.show()
引用
[2]python繪制三維圖
[4]Python + matplotlib更改縱橫坐標刻度顏色
[5]Python繪圖總結(Matplotlib篇)之坐標軸及刻度
總結
到此這篇關于python繪圖之坐標軸的文章就介紹到這了,更多相關python繪圖坐標軸內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關文章希望大家以后多多支持腳本之家!
相關文章
Python文本情感分類識別基于SVM算法Django框架實現(xiàn)
這篇文章主要為大家介紹了Python文本情感分類識別基于SVM算法Django框架實現(xiàn)詳解,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進步,早日升職加薪2023-07-07