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

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

如何使用 gridspec 指定子圖布局

如何使用 gridspec 指定子圖布局

墨色風雨 2023-11-09 21:45:24
我正在嘗試plt.GridSpec()設置兩個子圖,使左邊的子圖占據大約 67% 的空間,右邊的子圖占據 33% 的空間。我查看了文檔,但我似乎無法弄清楚如何設置索引——可能是由于缺乏 numpy 切片的經驗??芍貜偷睦?matplotlib inlineimport matplotlib.pyplot as pltimport pandas as pd## Dummy Datax = [0, 0.03, 0.075, 0.108, 0.16, 0.26, 0.37, 0.49, 0.76, 1.05, 1.64,    0.015, 0.04, 0.085, 0.11, 0.165, 0.29, 0.37, 0.6, 0.78, 1.1]y = [16.13, 0.62, 2.15, 41.083, 59.97, 13.30, 7.36, 6.80, 4.97, 3.53, 11.77,    30.21, 64.47, 57.64, 56.83, 46.69, 4.22, 30.35, 35.12, 5.22, 25.32]label = ['blue', 'blue', 'blue', 'blue', 'blue', 'blue', 'blue', 'blue', 'blue', 'blue', 'blue',        'red', 'red', 'red', 'red', 'red', 'red', 'red', 'red', 'red', 'red', 'red']df = pd.DataFrame(    list(zip(x, y, label)),     columns =['x', 'y', 'label']    ) ## Plottingfig = plt.figure(figsize=([10,8]))grid = plt.GridSpec(1, 3, wspace=0.4, hspace=0.3)ax1 = plt.subplot(grid[0, :1])ax2 = plt.subplot(grid[0, 2], sharey = ax1)ax1.scatter(x=df.y, y=df.x, color=df.label)df_red = df[df['label'] == "red"]df_blue = df[df['label'] == "blue"]myhist = ax2.hist([df_blue.x, df_red.x],         density=False,         edgecolor='black',         color=['blue', 'red'],         cumulative=False,         bins='auto',         orientation='horizontal',         stacked=True,         label=['Blue', 'Red'])ax1.set_xlabel('length')ax1.set_ylabel('value')ax2.set_xlabel('frequency')ax2.set_ylabel('value')當前結果所需結果相同的圖,只是左:右比例為 67% : 33%(因此左圖比右圖寬)。
查看完整描述

1 回答

?
POPMUISE

TA貢獻1765條經驗 獲得超5個贊

https://img1.sycdn.imooc.com/654ce44d0001918106040465.jpg

這是您需要進行的小修改:

# one position less than 3rd column
ax1 = plt.subplot(grid[0, :-1])


查看完整回答
反對 回復 2023-11-09
  • 1 回答
  • 0 關注
  • 198 瀏覽
慕課專欄
更多

添加回答

舉報

0/150
提交
取消
微信客服

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

幫助反饋 APP下載

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

公眾號

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