我正在嘗試使用matplotlib.pyplot的subplot在一個窗格中繪制多個數字。以下是我目前的代碼。
import matplotlib.pyplot as plt
import pandas as pd
import numpy as np
df = pd.DataFrame({"col1"/span>: [1,2], "col2": [3,4], "col3": [5,6], "col4": [7,8], "target": [9,10]})
f, axs = plt.subplots(nrows = 2, ncols = 2, sharey = True)
# for ax in axs.flat:.
# ax.label_outer()。
for k, col in enumerate(df.columns)。
if col != "target":
idx = np.unravel_index(k, (2,2)
axs[idx].scatter(df[col], df.target)
axs[idx].set_xlabel(col)
按照目前的情況,在注釋了這兩行后,這將列印出所有的xticks,但只有底部兩個圖的xlabel。
如果我取消這兩行的注釋,那么所有的xlabels都會出現,但是最上面一行的xticks會消失。我認為這是因為空間已經被[label_outer][2]函式所 "釋放 "出來了
我不明白我怎么能在最上面一行同時使用這兩個函式。如果列印出所有的xlabels,那么它們確實都在那里。
如果有任何幫助,我們將不勝感激!
。uj5u.com熱心網友回復:
你只需要在你的回圈之后呼叫plt.tight_layout()。參考
轉載請註明出處,本文鏈接:https://www.uj5u.com/ruanti/312366.html
標籤:


