日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程资源 > 编程问答 >内容正文

编程问答

【绘图】3D点图 及绘图关系matplotlib中plt系列

發布時間:2024/3/13 编程问答 49 豆豆
生活随笔 收集整理的這篇文章主要介紹了 【绘图】3D点图 及绘图关系matplotlib中plt系列 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

文章目錄

  • 1. 報錯及解決方案:
  • 2. matplotlib中plt繪圖關系描述
    • 2.1 plt.figure()
    • 2.2 plt.subplot()
    • 2.3 plt.subplots()
    • 2.4 add_subplots
    • 2.5 add_axes
  • 3. 圖層疊加

1. 報錯及解決方案:

報錯:

首先需要知道,這是由于沒有使用繪制3D圖,但沒有加入3D庫引起的。
所以需要添加如下的庫:

from mpl_toolkits.mplot3d import Axes3D

觸發語句:

ax = fig.add_subplot(111, projection='3d')

2. matplotlib中plt繪圖關系描述

然后發現其實自己也沒有搞明白繪圖當中的一些關系,所以干脆順便學習了一下,分享在這里:
庫matplotlib中的plt相關繪圖

2.1 plt.figure()

作用:繪制一張圖
函數
figure(num=None, figsize=None, dpi=None, facecolor=None, edgecolor=None, frameon=True)
參數說明

  • num:圖像編號或名稱,數字為編號 ,字符串為名稱
  • figsize:指定figure的寬和高,單位為英寸
  • dpi參數指定繪圖對象的分辨率,即每英寸多少個像素,缺省值為80
  • facecolor:背景顏色
  • edgecolor:邊框顏色
  • frameon:是否顯示邊框

一般用法

import matplotlib.pyplot as plt fig=plt.figure() plt.show()

2.2 plt.subplot()

作用:創建單個子圖
函數:subplot(nrows,ncols,sharex,sharey,subplot_kw,**fig_kw)
參數說明

  • nrows:子圖的行數
  • ncols:子圖的列數
  • sharex:所有子圖使用相同的X軸刻度(調節xlim將會影響所有的subplot)
  • sharey:所有子圖使用相同的Y軸刻度(調節ylim將會影響所有subplot)
  • subplot_kw:用于創建個子圖的關鍵字字典
  • **fig_kw:創建figure時的其他關鍵字。

一般用法

# 繪制一行兩個子圖,一個是y=2x,一個是y=x import matplotlib.pyplot as plt import numpy as np x = np.arange(0,10) y = 2 * x plt.subplot(121) plt.plot(x, y) plt.subplot(122) plt.plot(x, x) plt.show()

2.3 plt.subplots()

作用:創建多個子圖
說明:subplots參數與subplots類似。區別在于每條subplot命令只會創建一個子圖,而一條subplots就可以將所有子圖創建好。
一般用法

import numpy as np import matplotlib.pyplot as plt x = np.arange(0, 10) y = 2 * x #劃分子圖 fig,axes=plt.subplots(1,2) ax1=axes[0,0]# 左圖 ax2=axes[0,1]# 右圖 #作圖1 ax1.plot(x, y) #作圖2 ax2.plot(x, x) plt.show()

2.4 add_subplots

作用:給figure新添加子圖,與subpot類似,一般可以配合生成3D數據。
用法

import numpy as np import matplotlib.pyplot as plt from mpl_toolkits.mplot3d import Axes3D # 為了3D繪圖 x = np.arange(0, 10) y = 2 * x z = 2 * y fig = plt.figure() ax = fig.add_subplot(111, projection='3d') # 這里需要加參數projection='3d' ax.scatter(x, y, z, marker='+', color='blue', label='Real') plt.show()

2.5 add_axes

作用:新添加子區域
函數: fig.add_axes([left, bottom, width, height])
參數說明:left 與bottom表示左邊以及下邊的起始位置,width和height表示寬高占原fig的比例。
用法

import matplotlib.pyplot as plt #創建figure fig = plt.figure() x = np.arange(0, 10) y = 2 * x #新建區域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') plt.show()

3. 圖層疊加

參數:zorder
或者使用matplotlib的set_zorder這個屬性.
但是發現如果使用Axes3D,其竟然是失效的。。。甚是無語啊,所以看到一種方法是放棄使用該方法使用mayavi庫。

參考

https://blog.csdn.net/C_chuxin/article/details/83994457
多圖層:
https://blog.csdn.net/sunsoda/article/details/82431770

總結

以上是生活随笔為你收集整理的【绘图】3D点图 及绘图关系matplotlib中plt系列的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。