我有一張用 imshow 顯示的圖片。然後我添加所有行並顯示最大值。我對列也做同樣的事情。在顯示圖中,我想要讓影像的 x 軸和 y 軸與新增列的 x 軸和新增行的 y 軸重合。然而,儘管分別設定了 sharex
和 sharey
,但它似乎不起作用。我希望我一次只能做一個:
import numpy as np import matplotlib.pyplot as plt from scipy.signal import argrelextrema import matplotlib.animation as animation fig= plt.figure() gs= fig.add_gridspec(2,2, height_ratios=[1, 0.1], width_ratios=[1, 0.1], hspace=0, wspace=0) ax1= fig.add_subplot(gs[0,0]) ax2= fig.add_subplot(gs[1,0], sharex=ax1) ax3= fig.add_subplot(gs[0,1], sharey=ax1) frameNumber= 10 imgs= [] for i in range(frameNumber): np.random.seed(i) randomImage= np.random.random((5,5)) sumX= np.sum(randomImage, axis=0) sumY= np.sum(randomImage, axis=1) dataRange= np.arange(len(sumX)) randomDataSet= np.random.random((10)) randomMaximalX= argrelextrema(sumX, np.greater) randomMaximalY= argrelextrema(sumY, np.greater) img1= ax1.imshow(randomImage, animated=True) img2= ax2.plot(dataRange, sumX,animated=True)[0] img3= ax3.plot(sumY,dataRange,animated=True)[0] img4= ax2.vlines(x=randomMaximalX, ymin=0, ymax=5, animated=True, linestyles="dashed") img5= ax3.hlines(y=randomMaximalY, xmin=0, xmax=5, animated=True, linestyles="dashed") imgs.append([img1, img2, img3, img4, img5]) ani= animation.ArtistAnimation(fig, imgs, interval=1000, blit=False) plt.show()
目前結果是這樣的:
實際上我想要這樣的東西:
其中兩張圖的 h 值相同。非常感謝!
正確答案
有兩種方法可以解決此問題:
- 使用
axes.pcolormesh
取代axes.imshow
- #或更新相鄰圖的縱橫比。
① 軸.pcolormesh
axes.pcolormesh
不會強制產生的圖像為正方形(1:1 縱橫比),因此您的單元格將是矩形,但它們會適當地填充所提供的空間。
from numpy.random import default_rng import matplotlib.pyplot as plt rng = default_rng(0) image = rng.uniform(1, 10, size=(5, 5)) mosaic = [ ['main', 'right'], ['bottom', '.' ], ] fig, axd = plt.subplot_mosaic( mosaic, gridspec_kw={ 'height_ratios': [1, .1], 'width_ratios': [1, .1], 'wspace': .05, 'hspace': .05, }, sharex=true, sharey=true, ) axd['main'].pcolormesh(image) plt.show()
② 方面更新
如果您想堅持使用 axes.imshow
,那麼您需要調整
手動調整每個圖的縱橫比。為了獲得正確的比率,您需要
根據提供給 gridspec
的 height_ratio
和 width_ratio
進行計算
from numpy.random import default_rng import matplotlib.pyplot as plt rng = default_rng(0) image = rng.uniform(1, 10, size=(5, 5)) mosaic = [ ['main', 'right'], ['bottom', '.' ], ] fig, axd = plt.subplot_mosaic( mosaic, sharex=True, sharey=True, gridspec_kw={ 'height_ratios': [1, .1], 'width_ratios': [1, .1], # change values to move adjacent plots closer to the main 'wspace': .05, 'hspace': .05, }, ) axd['main'].imshow(image) axd['main'].set_anchor('SE') # move main plot to bottom-right of bounding-box # calculate the width and height scales gs = axd['main'].get_gridspec() # you can also save these values from your `gridspec_kw` width_scale = gs.get_width_ratios()[0] / gs.get_width_ratios()[1] height_scale = gs.get_height_ratios()[0] / gs.get_height_ratios()[1] # update the aspect ratios of the adjacent plots # set their anchors so they correctly align with the main plot axd['right'].set_aspect(width_scale, anchor='SW') axd['bottom'].set_aspect(1/height_scale, anchor='NE') plt.show()
以上是影像同時共享x軸和y軸的詳細內容。更多資訊請關注PHP中文網其他相關文章!

每天學習Python兩個小時是否足夠?這取決於你的目標和學習方法。 1)制定清晰的學習計劃,2)選擇合適的學習資源和方法,3)動手實踐和復習鞏固,可以在這段時間內逐步掌握Python的基本知識和高級功能。

Python在Web開發中的關鍵應用包括使用Django和Flask框架、API開發、數據分析與可視化、機器學習與AI、以及性能優化。 1.Django和Flask框架:Django適合快速開發複雜應用,Flask適用於小型或高度自定義項目。 2.API開發:使用Flask或DjangoRESTFramework構建RESTfulAPI。 3.數據分析與可視化:利用Python處理數據並通過Web界面展示。 4.機器學習與AI:Python用於構建智能Web應用。 5.性能優化:通過異步編程、緩存和代碼優

Python在開發效率上優於C ,但C 在執行性能上更高。 1.Python的簡潔語法和豐富庫提高開發效率。 2.C 的編譯型特性和硬件控制提升執行性能。選擇時需根據項目需求權衡開發速度與執行效率。

Python在現實世界中的應用包括數據分析、Web開發、人工智能和自動化。 1)在數據分析中,Python使用Pandas和Matplotlib處理和可視化數據。 2)Web開發中,Django和Flask框架簡化了Web應用的創建。 3)人工智能領域,TensorFlow和PyTorch用於構建和訓練模型。 4)自動化方面,Python腳本可用於復製文件等任務。

Python在數據科學、Web開發和自動化腳本領域廣泛應用。 1)在數據科學中,Python通過NumPy、Pandas等庫簡化數據處理和分析。 2)在Web開發中,Django和Flask框架使開發者能快速構建應用。 3)在自動化腳本中,Python的簡潔性和標準庫使其成為理想選擇。

Python的靈活性體現在多範式支持和動態類型系統,易用性則源於語法簡潔和豐富的標準庫。 1.靈活性:支持面向對象、函數式和過程式編程,動態類型系統提高開發效率。 2.易用性:語法接近自然語言,標準庫涵蓋廣泛功能,簡化開發過程。

Python因其簡潔與強大而備受青睞,適用於從初學者到高級開發者的各種需求。其多功能性體現在:1)易學易用,語法簡單;2)豐富的庫和框架,如NumPy、Pandas等;3)跨平台支持,可在多種操作系統上運行;4)適合腳本和自動化任務,提升工作效率。

可以,在每天花費兩個小時的時間內學會Python。 1.制定合理的學習計劃,2.選擇合適的學習資源,3.通過實踐鞏固所學知識,這些步驟能幫助你在短時間內掌握Python。


熱AI工具

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

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

Undress AI Tool
免費脫衣圖片

Clothoff.io
AI脫衣器

AI Hentai Generator
免費產生 AI 無盡。

熱門文章

熱工具

記事本++7.3.1
好用且免費的程式碼編輯器

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

SecLists
SecLists是最終安全測試人員的伙伴。它是一個包含各種類型清單的集合,這些清單在安全評估過程中經常使用,而且都在一個地方。 SecLists透過方便地提供安全測試人員可能需要的所有列表,幫助提高安全測試的效率和生產力。清單類型包括使用者名稱、密碼、URL、模糊測試有效載荷、敏感資料模式、Web shell等等。測試人員只需將此儲存庫拉到新的測試機上,他就可以存取所需的每種類型的清單。

Dreamweaver CS6
視覺化網頁開發工具

ZendStudio 13.5.1 Mac
強大的PHP整合開發環境