亚洲在线久爱草,狠狠天天香蕉网,天天搞日日干久草,伊人亚洲日本欧美

為了賬號安全,請及時綁定郵箱和手機立即綁定
已解決430363個問題,去搜搜看,總會有你想問的

Matplotlib,當圖大小bi時matshow與gridspec不對齊

Matplotlib,當圖大小bi時matshow與gridspec不對齊

繁花不似錦 2022-10-18 17:10:49
我嘗試使用 gridspec 將多種類型的圖一起繪制。我將它與 Jupyter Notebook 一起使用,我意識到當圖形寬度大于單元格寬度時。matshow 縮小了,不再與其他人對齊。例如,當 figsize 的寬度小于單元格寬度時,一切正常。.import matplotlib.pyplot as pltimport numpy as npimport matplotlib.gridspec as grdduration = 1data1 = np.sin(2*np.pi*np.linspace(0, duration, 10000))data2 = np.random.random((100,12))fig = plt.figure(figsize=[15, 5], constrained_layout=True)grid = grd.GridSpec(2, 2, figure=fig, height_ratios=[1, 1], width_ratios=[40, 1])ax = plt.subplot(grid[0])ax.plot(data1)ax = plt.subplot(grid[2])im = ax.matshow(data2.T, cmap=plt.get_cmap('inferno'), origin='lower')ax = plt.subplot(grid[3])cb = plt.colorbar(im, cax=ax)然后當寬度大于單元格時。.fig = plt.figure(figsize=[20, 5], constrained_layout=True)grid = grd.GridSpec(2, 2, figure=fig, height_ratios=[1, 1], width_ratios=[40, 1])ax = plt.subplot(grid[0])ax.plot(data1)ax = plt.subplot(grid[2])im = ax.matshow(data2.T, cmap=plt.get_cmap('inferno'), origin='lower')ax = plt.subplot(grid[3])cb = plt.colorbar(im, cax=ax)是什么導致 matshow() 縮小,我該如何解決?我在 Python 3.7 和 Matplotlib 3.1.3 上
查看完整描述

1 回答

?
慕妹3146593

TA貢獻1820條經驗 獲得超9個贊

constrained_layout 的全部要點之一是顏色條的處理更加優雅。即你不需要width_ratios=[40, 1]破解。


import matplotlib.pyplot as plt

import numpy as np


fig, ax = plt.subplots(2, 1, constrained_layout=True)

pc = ax[0].matshow(np.random.rand(20, 20), aspect='auto')

fig.colorbar(pc, ax=ax[0])

ax[1].plot(np.random.rand(20))

plt.show()

http://img1.sycdn.imooc.com//634e6de00001264917131274.jpg

查看完整回答
反對 回復 2022-10-18
  • 1 回答
  • 0 關注
  • 150 瀏覽
慕課專欄
更多

添加回答

舉報

0/150
提交
取消
微信客服

購課補貼
聯系客服咨詢優惠詳情

幫助反饋 APP下載

慕課網APP
您的移動學習伙伴

公眾號

掃描二維碼
關注慕課網微信公眾號