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

歡迎訪問 生活随笔!

生活随笔

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

编程问答

14_面向对象API绘图、图中图 (A Plot inside of Another Plot)、设定绘图范围Setting the Plot Range、对数尺度Logarithmic Scale

發(fā)布時間:2024/9/27 编程问答 30 豆豆
生活随笔 收集整理的這篇文章主要介紹了 14_面向对象API绘图、图中图 (A Plot inside of Another Plot)、设定绘图范围Setting the Plot Range、对数尺度Logarithmic Scale 小編覺得挺不錯的,現(xiàn)在分享給大家,幫大家做個參考.

14.面向?qū)ο驛PI繪圖
14.1.圖中圖 (A Plot inside of Another Plot)
14.2.設定繪圖范圍 (Setting the Plot Range)
14.3.對數(shù)尺度(Logarithmic Scale)

14.面向?qū)ο驛PI繪圖

Matplotlib繪圖庫的操作是通過API實現(xiàn)的,一種操作方法是類似MATLAB的函數(shù)接口的API;另一種操作方法是面向?qū)ο蟮腁PI。這兩種API可以并行使用,不過函數(shù)接口的API的易用性明顯好于面向?qū)ο蟮腁PI。

就像Python本身一樣,Matplotlib是以面向?qū)ο蟮姆绞骄幊毯驮O計的。當使用多個圖形時,或者當一個圖形由多個子圖組成時,使用圖形對象方法的優(yōu)點就會顯現(xiàn)出來。

在下面的示例中,我們以面向?qū)ο蟮姆绞絼?chuàng)建一個繪圖。首先創(chuàng)建一個新的figure實例。將其引用存儲在一個Figure類實例,即變量figure實例。將其引用存儲在一個Figure類實例,即變量fig中。然后,我們fig中使用add_axes方法創(chuàng)建一個新的軸的實例。

import numpy as np import matplotlib.pyplot as plt fig = plt.figure() X = np.arange(0,10) Y = np.random.randint(1, 20, size=10) left, bottom, width, height = 0.1, 0.1, 0.8, 0.8 axes = fig.add_axes([left, bottom, width, height]) axes.plot(X, Y, 'b') axes.set_xlabel('x') axes.set_ylabel('y') axes.set_title('title') plt.show()

在不使用figure實例的情況下,代碼如下所示:

import numpy as np import matplotlib.pyplot as plt fig = plt.figure() X = np.arange(0,10) Y = np.random.randint(1, 20, size=10) left, bottom, width, height = 0.1, 0.1, 0.8, 0.8 axes = fig.add_axes([left, bottom, width, height]) axes.plot(X, Y, 'b') axes.set_xlabel('xxxxxxxxxxxxx') axes.set_ylabel('yyyyyyyyyyyyy') axes.set_title('title biaoti') plt.show()

14.1.圖中圖 (A Plot inside of Another Plot)

import numpy as np import matplotlib.pyplot as plt fig = plt.figure() X = [1, 2, 3, 4, 5, 6, 7] Y = [1, 3, 4, 2, 5, 8, 6] axes1 = fig.add_axes([0.1, 0.1, 0.9, 0.9]) # main axes # add_axes 參數(shù)rect The dimensions [left, bottom, width, height] of the new axes. # All quantities are in fractions of figure width and height. axes2 = fig.add_axes([0.2, 0.6, 0.4, 0.3]) # inside axes # main figure axes1.plot(X, Y, 'r') axes1.set_xlabel('x') axes1.set_ylabel('y') axes1.set_title('title') # insert axes2.plot(Y, X, 'g') axes2.set_xlabel('y') axes2.set_ylabel('x') axes2.set_title('title inside') plt.show()

14.2.設定繪圖范圍 (Setting the Plot Range)

配置軸的范圍可以通過在軸對象中使用set_ylim和set_xlim方法來完成。 使用axis(‘tight’),可以自動創(chuàng)建"tightly fitted" 的軸范圍:

import numpy as np import matplotlib.pyplot as plt fig, axes = plt.subplots(1, 3, figsize=(10, 4)) x = np.arange(0, 5, 0.25) axes[0].plot(x, x**2, x, x**3) axes[0].set_title("default axes ranges") axes[1].plot(x, x**2, x, x**3) axes[1].axis('tight') axes[1].set_title("tight axes") axes[2].plot(x, x**2, x, x**3) axes[2].set_ylim([0, 60]) axes[2].set_xlim([2, 5]) axes[2].set_title("custom axes range") plt.show()

14.3.對數(shù)尺度(Logarithmic Scale)

可以為一個或兩個軸設置對數(shù)尺度。使用接受一個參數(shù)(值為log)的set_xscale和set_yscale方法分別設置每個軸的scale:

import numpy as np import matplotlib.pyplot as plt fig = plt.figure() ax = fig.add_subplot(1, 1, 1) x = np.arange(0, 5, 0.25) ax.plot(x, x ** 2, x, x ** 3) ax.set_yscale("log") plt.show()

總結

以上是生活随笔為你收集整理的14_面向对象API绘图、图中图 (A Plot inside of Another Plot)、设定绘图范围Setting the Plot Range、对数尺度Logarithmic Scale的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網(wǎng)站內(nèi)容還不錯,歡迎將生活随笔推薦給好友。