python繪圖plt.figure\subplot\add_subplots\Axes3D\contourf

一、plt.figure參數解釋

  matplotlib.pyplot.figure() 創建一個新的畫布(figure)。

matplotlib.pyplot.figure(num=None, figsize=None, dpi=None, facecolor=None, edgecolor=None, frameon=True, FigureClass=<class 'matplotlib.figure.Figure'>, clear=False, **kwargs)

輸入參數:
num:整型或者字符串,可選參數,默認:None。圖像編號或名稱,數字爲編號 ,字符串爲名稱

  • 如果不提供該參數,一個新的畫布(figure)將被創建而且畫布數量將會增加。
  • 如果提供該參數,帶有id的畫布是已經存在的,激活該畫布並返回該畫布的引用。
  • 如果這個畫布不存在,創建並返回畫布實例。
  • 如果num是字符串,窗口標題將被設置爲該圖的數字。
    figsize:整型元組,可選參數 ,默認:None。每英寸的寬度和高度。如果不提供,默認值是figure.figsize。
    dpi:整型,可選參數,默認:None。每英寸像素點。如果不提供,默認是figure.dpi。
    facecolor:背景色。如果不提供,默認值:figure.facecolor。 [c=labels.astype(np.float)]
    edgecolor:邊界顏色。如果不提供,默認值:figure.edgecolor。
    framemon:布爾類型,可選參數,默認值:True。如果是False,禁止繪製畫圖框。
    FigureClass:源於matplotlib.figure.Figure的類。(可選)使用自定義圖實例。
    clear:布爾類型,可選參數,默認值:False。如果爲True和figure已經存在時,這是清理掉改圖。

返回值:
figure:Figure。返回的Figure實例也將被傳遞給後端的new_figure_manager,這允許將自定義的圖類掛接到pylab接口中。附加的kwarg將被傳遞給圖形init函數。

import matplotlib.pyplot as plt
#創建自定義圖像
fig=plt.figure(figsize=(4,3),facecolor='blue')
plt.show()

二、subplot創建單個子圖

  subplot可以規劃figure劃分爲n個子圖,但每條subplot命令只會創建一個子圖

import numpy as np  
import matplotlib.pyplot as plt  
x = np.arange(0, 100)  
#作圖1
plt.subplot(221)  
plt.plot(x, x)  
#作圖2
plt.subplot(222)  
plt.plot(x, -x)  
 #作圖3
plt.subplot(223)  
plt.plot(x, x ** 2)  
plt.grid(color='r', linestyle='--', linewidth=1,alpha=0.3)
#作圖4
plt.subplot(224)  
plt.plot(x, np.log(x))  
plt.show()  

三、subplots創建多個子圖

plt.subplots(
    nrows=1,
    ncols=1,
    sharex=False,
    sharey=False,
    squeeze=True,
    subplot_kw=None,
    gridspec_kw=None,
    **fig_kw,
)

  subplots參數與subplot相似

import numpy as np
import matplotlib.pyplot as plt
import pandas as pd
x = np.arange(0, 100)
#劃分子圖
fig,axes=plt.subplots(2,2)
# ax1=axes[0,0]
# ax2=axes[0,1]
# ax3=axes[1,0]
# ax4=axes[1,1]

ax1=axes[0,0]
ax2=axes[0,1]
ax3=axes[1,0]
ax4=axes[1,1]

#作圖1
ax1.plot(x, x, c= 'r')
#作圖2
ax2.plot(x, -x, c = 'g')
#作圖3
ax3.plot(x, x ** 2,c = 'b')
ax3.grid(color='r', linestyle='--', linewidth=1,alpha=0.3)
#作圖4
ax4.plot(x, np.log(x))
plt.show()
import numpy as np
import matplotlib.pyplot as plt
import pandas as pd

f, axes = plt.subplots(2, 2)

data = pd.Series(np.random.rand(16), index=list('abcdefghijklmnop'))
data.plot.bar(ax=axes[0, 1], color='b', alpha=0.5)
data.plot.bar(ax=axes[0, 0], color='b', alpha=0.5)
#  ax=[1,1] 即位置是第2行、第二列。(python從0開始計數,所以“1”代表第2的)
data.plot.barh(ax=axes[1, 1], color='k', alpha=0.5)
data.plot.barh(ax=axes[1, 0], color='k', alpha=0.5)
plt.show()

四、面向對象API:add_subplots與add_axes新增子圖或區域

4.1 add_subplots

  add_subplot新增子圖

  add_subplot的參數與subplots的相似

import numpy as np
import matplotlib.pyplot as plt
x = np.arange(0, 100)
#新建figure對象
fig=plt.figure()
#新建子圖1
ax1=fig.add_subplot(2,2,1)
ax1.plot(x, np.sin(x))
#新建子圖2
ax2=fig.add_subplot(2,2,2)
ax2.plot(x, x+2)
#新建子圖3
ax3=fig.add_subplot(2,2,3)
ax3.plot(x, x ** 2)
ax3.grid(color='r', linestyle='--', linewidth=1,alpha=0.3)
#新建子圖4
ax4=fig.add_subplot(2,2,4)
ax4.plot(x, np.log(x))
plt.show()

4.2 add_axes

  add_axes爲新增子區域,該區域可以座落在figure內任意位置,且該區域可任意設置大小

import numpy as np
import matplotlib.pyplot as plt

#新建figure
fig = plt.figure()
# 定義數據

x = [1, 2, 3, 4, 5, 6, 7]
y = [1, 3, 4, 2, 5, 8, 6]
#新建區域ax1

#figure的百分比,從figure 10%的位置開始繪製, 寬高是figure的80%
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_title('area1')


#新增區域ax2,嵌套在ax1內
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_title('area2')


left, bottom, width, height = 0.7, 0.4, 0.1, 0.1
# 獲得繪製的句柄
ax3 = fig.add_axes([left, bottom, width, height])
ax3.plot(x, y, 'r')
ax3.set_title('area3')

plt.show()

五、Axes3D函數詳解

5.1 Axes3D(fig)

  Axes3D對象與使用projection ='3d’關鍵字的任何其他軸一樣創建。 創建一個新的matplotlib.figure.Figure併爲其添加一個類型爲Axes3D的新軸:Axes3D(fig)

  • 畫線–Axes3D.plot(xs, ys, *args, **kwargs)

注:xs,ys x,y座標的頂點 ,zs z值(s),對於所有點或者每個點都有一個值。zdir繪製二維集時,將哪個方向用作z(‘x’,‘y’或’z’)。

  • 畫散點圖–Axes3D.scatter(xs, ys, zs=0, zdir=‘z’, s=20, c=None, depthshade=True, *args, **kwargs)

注:xs,ys爲數據點的位置。, zs與xs和ys具有相同長度的數組或將所有點放在同一平面中的單個值。 缺省值是0。

  • 畫曲面圖—Axes3D.plot_surface(X, Y, Z, *args, **kwargs)
import numpy as np
import matplotlib.pyplot as plt
from mpl_toolkits.mplot3d import Axes3D

np.random.seed(19680801)
def randrange(n, vmin, vmax):
    return (vmax - vmin)*np.random.rand(n) + vmin
fig = plt.figure()
# 將figure變爲3d
ax = Axes3D(fig)
# ax = fig.add_subplot(111, projection='3d')
n = 100
# For each set of style and range settings, plot n random points in the box
# defined by x in [23, 32], y in [0, 100], z in [zlow, zhigh].
for c, m, zlow, zhigh in [('r', 'o', -50, -25), ('b', '^', -30, -5)]:
    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()

  zdir繪製二維集時,將哪個方向用作z(‘x’,‘y’或’z’)。c一種顏色。 c可以是單色格式字符串,也可以是長度爲N的顏色規範序列,也可以是使用通過kwargs指定的cmap和範數映射到顏色的N個數字序列。 請注意,c不應該是單個數字RGB或RGBA序列,因爲它與要進行彩色映射的值數組難以區分。 c可以是其中行是RGB或RGBA的二維數組,但是,包括單行的情況下爲所有點指定相同的顏色。

5.2 contourf 填充等高線圖

ax.contourf(X, Y, Z, *args, zdir='z', offset=None, **kwargs)
ax.contour(
    X,
    Y,
    Z,
    *args,
    extend3d=False,
    stride=5,
    zdir='z',
    offset=None,
    **kwargs,
)

參數:
X,Y:類似數組,可選爲Z中的座標值,當 X,Y,Z 都是 2 維數組時,它們的形狀必須相同。如果都是 1 維數組時,len(X)是 Z 的列數,而 len(Y) 是 Z 中的行數。(例如,經由創建numpy.meshgrid())
Z:類似矩陣,繪製輪廓的高度值
levels:int或類似數組,可選,確定輪廓線/區域的數量和位置
alpha:float ,可選,alpha混合值,介於0(透明)和1(不透明)之間。
cmap:str或colormap ,可選,Colormap用於將數據值(浮點數)從間隔轉換爲相應Colormap表示的RGBA顏色。用於將數據縮放到間隔中看 。

import numpy as np
import matplotlib.pyplot as plt
from mpl_toolkits.mplot3d import Axes3D
fig=plt.figure()
ax=Axes3D(fig)
x=np.arange(-5,5,0.25)
y=np.arange(-5,5,0.25)
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'))
#填充rainbow顏色
ax.contourf(x,y,z,zdir='z',offset=-2,cmap='rainbow')
#繪製3D圖形,zdir表示從哪個座標軸上壓下去
plt.show()
#顯示圖片

5.3 contour 繪製輪廓線

#把上面代碼的 ax.contourf(x,y,z,zdir='z',offset=-2,cmap='rainbow')換成
ax.contour(x,y,z,zdir='z',offset=-2,cmap='rainbow')

有趣的事,Python永遠不會缺席

歡迎關注小婷兒的博客

    文章內容來源於小婷兒的學習筆記,部分整理自網絡,若有侵權或不當之處還請諒解

    如需轉發,請註明出處:小婷兒的博客python    https://www.cnblogs.com/xxtalhr/

博客園 https://www.cnblogs.com/xxtalhr/

CSDN https://blog.csdn.net/u010986753

有問題請在博客下留言或加作者:
     微信:tinghai87605025 聯繫我加微信羣
     QQ :87605025
     python QQ交流羣:py_data 483766429

培訓說明

OCP培訓說明連接 https://mp.weixin.qq.com/s/2cymJ4xiBPtTaHu16HkiuA

OCM培訓說明連接 https://mp.weixin.qq.com/s/7-R6Cz8RcJKduVv6YlAxJA

     小婷兒的python正在成長中,其中還有很多不足之處,隨着學習和工作的深入,會對以往的博客內容逐步改進和完善噠。重要的事多說幾遍。。。。。。

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