簡體   English   中英

在 matplotlib 子圖中的主 plot 的一側添加兩個較小的子圖

[英]Adding two smaller subplots to the side of my main plot in matplotlib subplots

目前我的圖表僅顯示左側的主要大圖表。

但是,我現在想將兩個較小的圖添加到主 plot 的右側; 與每個單獨的數據集。

我正在努力研究如何做到這一點。 下面的照片顯示了我想要的 output。

filenamesK = glob("C:/Users/Ke*.csv")
filenamesZ = glob("C:/Users/Ze*.csv")


K_Z_Averages = {'K':[], 'Z':[]}


# We will create a function for plotting, instead of nesting lots of if statements within a long for-loop.
def plot_data(filename, fig_ax, color):
    
    df = pd.read_csv(f, sep=',',skiprows=24) # Read in the csv.
    df.columns=['sample','Time','ms','Temp1'] # Set the column names
    df=df.astype(str) # Set the data type as a string.

    df["Temp1"] = df["Temp1"].str.replace('\+ ', '').str.replace(' ', '').astype(float) # Convert to float
    # Take the average of the data from the Temp1 column, starting from sample 60  until sample 150.
    avg_Temp1 = df.iloc[60-1:150+1]["Temp1"].mean()
    
    # Append this average to a K_Z_Averages, containing a column for average from each K file and the average from each Z file.
    # Glob returns the whole path, so you need to replace 0 for 10.
    K_Z_Averages[os.path.basename(filename)[0]].append(avg_Temp1)
    
    fig_ax.plot(df[["Temp1"]], color=color)

fig, ax = plt.subplots(figsize=(20, 15))

for f in filenamesK:
    plot_data(f, ax, 'blue')

for f in filenamesZ:
    plot_data(f, ax, 'red')
    

plt.show()

在此處輸入圖像描述

@max 的答案很好,但你也可以做 matplotlib>=3.3 是

import matplotlib.pyplot as plt
fig = plt.figure(constrained_layout=True)
axs = fig.subplot_mosaic([['Left', 'TopRight'],['Left', 'BottomRight']],
                          gridspec_kw={'width_ratios':[2, 1]})
axs['Left'].set_title('Plot on Left')
axs['TopRight'].set_title('Plot Top Right')
axs['BottomRight'].set_title('Plot Bottom Right')

請注意,重復的名稱'Left'被使用了兩次,表示該子圖在布局中占據了兩個位置。 還要注意width_ratios的使用。

在此處輸入圖像描述

這是一個棘手的問題。 本質上,您可以在圖形上放置一個網格 ( add_gridspec() add_subplot() ),然后在不同的網格元素中打開子圖 ( add_subplot() )。

import matplotlib.pyplot as plt

# open figure
fig = plt.figure()
# add grid specifications
gs = fig.add_gridspec(2, 3)
# open axes/subplots
axs = []
axs.append( fig.add_subplot(gs[:,0:2]) ) # large subplot (2 rows, 2 columns)
axs.append( fig.add_subplot(gs[0,2]) )   # small subplot (1st row, 3rd column)
axs.append( fig.add_subplot(gs[1,2]) )   # small subplot (2nd row, 3rd column)

在此處輸入圖像描述

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM