在Matplotlib 建立不連續軸
簡介:
簡介:
import matplotlib.pyplot as plt x1 = np.linspace(0, 5, 100) y1 = np.sin(x1) x2 = np.linspace(10, 15, 100) y2 = np.cos(x2) plt.subplot(1, 2, 1) plt.plot(x1, y1) plt.subplot(1, 2, 2) plt.plot(x2, y2) plt.show()建立不連續軸的一種方法是使用子圖。每個子圖都可以分配不同範圍的 x 軸值,從而導致子圖之間存在間隙。這是一個簡單的範例:
自訂軸轉換:
import matplotlib.pyplot as plt from matplotlib.transforms import Transform from matplotlib.ticker import LogLocator class DiscontinuousTransform(Transform): def __init__(self, breaks): Transform.__init__(self) self.breaks = breaks def transform(self, values): new_values = values.copy() for break in self.breaks: new_values[values > break] += 1 return new_values def inverted(self): return InvertedDiscontinuousTransform(self.breaks) class InvertedDiscontinuousTransform(Transform): def __init__(self, breaks): Transform.__init__(self) self.breaks = breaks def transform(self, values): new_values = values.copy() for break in self.breaks: new_values[values >= break] -= 1 return new_values def inverted(self): return DiscontinuousTransform(self.breaks) x = np.linspace(0, 10, 100) y = np.sin(x) trans = DiscontinuousTransform([5]) locator = LogLocator(base=10) locator.set_params(minor_locator=None) plt.plot(x, y, transform=trans) plt.gca().xaxis.set_major_locator(locator) plt.gca().xaxis.set_major_formatter(plt.FormatStrFormatter("%0.0f\n(pert)")) plt.show()建立不連續軸的另一種方法是使用自訂軸轉換。透過定義新的轉換類,我們可以指定資料如何映射到軸。以下程式碼示範了這種方法:
結論:在 Matplotlib 中建立不連續軸可以使用子圖或自訂軸轉換來實現。自訂轉換方法提供了更大的靈活性和對軸行為的控制。這兩種方法都可以有效地視覺化有間隙或不連續的資料。
以上是如何在 Matplotlib 中建立不連續軸?的詳細內容。更多資訊請關注PHP中文網其他相關文章!

Tomergelistsinpython,YouCanusethe操作員,estextMethod,ListComprehension,Oritertools

在Python3中,可以通過多種方法連接兩個列表:1)使用 運算符,適用於小列表,但對大列表效率低;2)使用extend方法,適用於大列表,內存效率高,但會修改原列表;3)使用*運算符,適用於合併多個列表,不修改原列表;4)使用itertools.chain,適用於大數據集,內存效率高。

使用join()方法是Python中從列表連接字符串最有效的方法。 1)使用join()方法高效且易讀。 2)循環使用 運算符對大列表效率低。 3)列表推導式與join()結合適用於需要轉換的場景。 4)reduce()方法適用於其他類型歸約,但對字符串連接效率低。完整句子結束。

pythonexecutionistheprocessoftransformingpypythoncodeintoExecutablestructions.1)InternterPreterReadSthecode,ConvertingTingitIntObyTecode,whepythonvirtualmachine(pvm)theglobalinterpreterpreterpreterpreterlock(gil)the thepythonvirtualmachine(pvm)

Python的關鍵特性包括:1.語法簡潔易懂,適合初學者;2.動態類型系統,提高開發速度;3.豐富的標準庫,支持多種任務;4.強大的社區和生態系統,提供廣泛支持;5.解釋性,適合腳本和快速原型開發;6.多範式支持,適用於各種編程風格。

Python是解釋型語言,但也包含編譯過程。 1)Python代碼先編譯成字節碼。 2)字節碼由Python虛擬機解釋執行。 3)這種混合機制使Python既靈活又高效,但執行速度不如完全編譯型語言。

UseeAforloopWheniteratingOveraseQuenceOrforAspecificnumberoftimes; useAwhiLeLoopWhenconTinuingUntilAcIntiment.forloopsareIdealForkNownsences,而WhileLeleLeleLeleLeleLoopSituationSituationsItuationsItuationSuationSituationswithUndEtermentersitations。

pythonloopscanleadtoerrorslikeinfiniteloops,modifyingListsDuringteritation,逐個偏置,零indexingissues,andnestedloopineflinefficiencies


熱AI工具

Undresser.AI Undress
人工智慧驅動的應用程序,用於創建逼真的裸體照片

AI Clothes Remover
用於從照片中去除衣服的線上人工智慧工具。

Undress AI Tool
免費脫衣圖片

Clothoff.io
AI脫衣器

Video Face Swap
使用我們完全免費的人工智慧換臉工具,輕鬆在任何影片中換臉!

熱門文章

熱工具

MinGW - Minimalist GNU for Windows
這個專案正在遷移到osdn.net/projects/mingw的過程中,你可以繼續在那裡關注我們。 MinGW:GNU編譯器集合(GCC)的本機Windows移植版本,可自由分發的導入函式庫和用於建置本機Windows應用程式的頭檔;包括對MSVC執行時間的擴展,以支援C99功能。 MinGW的所有軟體都可以在64位元Windows平台上運作。

禪工作室 13.0.1
強大的PHP整合開發環境

Dreamweaver Mac版
視覺化網頁開發工具

DVWA
Damn Vulnerable Web App (DVWA) 是一個PHP/MySQL的Web應用程序,非常容易受到攻擊。它的主要目標是成為安全專業人員在合法環境中測試自己的技能和工具的輔助工具,幫助Web開發人員更好地理解保護網路應用程式的過程,並幫助教師/學生在課堂環境中教授/學習Web應用程式安全性。 DVWA的目標是透過簡單直接的介面練習一些最常見的Web漏洞,難度各不相同。請注意,該軟體中

Dreamweaver CS6
視覺化網頁開發工具