在 Matplotlib 中放置具有不同列和行大小的子图
Placing Subplots in Matplotlib with Occupying Different column and row sizes
我正在尝试创建 6 个子图。 2 个线图位于同一行,其下方的行将具有累积图,它将占据 2 列的 space。它将为 col1
和 col2
执行此操作,总共创建 6 个图(每个范围 3 个)。然而,这些图重叠了,我该如何修复它并使其符合 Format of the Plots
.
中提到的格式
图表格式:
-line plot `col1`(column=1, row=1), line plot `col1` (column=2, row=1)
-cumulative plot `col1`(column= 1 and 2, row=2)
-line plot `col2`(column=1, row=3), line plot `col2` (column=2, row=3)
-cumulative plot `col2`(column= 1 and 2, row=4)
代码:
import matplotlib.pyplot as plt
import pandas as pd
import numpy as np
df = pd.DataFrame({
'x': [1,2,3,4,5,6],
'col1': [5,3,4,8,9,11],
'col2': [2,6,3,11,3,6]
})
def Graphing():
# Size of the figure
fig = plt.figure(figsize=(16, 9))
for count, (y,plots,iterator,title) in enumerate(zip(
[df['col1'],df['col2']], # Range Columns
[[(4, 2, 1), (4, 2, 2), (4, 1, 2)], [(4, 2, 1), (4, 2, 2), (4, 1, 4)]], #Columns
[0,3], #iterator used to give different names to subplot_
['Column 1', 'Column 2'])): #Column used
for subplot_,add_plot,y,title2 in zip(
[f'sub{iterator+0}',f'sub{iterator+1}',f'sub{iterator+2}'], #Name of the subplot sub0, sub1..
[plots[0],plots[1],plots[2]], # cordinates of the subplot 4, 2, 1), (4, 2, 2) ...
[y,y,np.cumsum(y)],
['Line Plot','Line Plot', 'Cumulative Plot']): # Range values for the plot
#Adds Subplot
subplot_ = fig.add_subplot(*add_plot)
#Title
subplot_.set(title=f'{title} {title2}')
# Sets up the x,y, color of the plot
getattr(subplot_, 'plot')(df['x'],y,color='r', marker='o')
# Show Graph
plt.show()
Graphing()
输出:
将它移到第一个 for 循环中
fig = plt.figure(figsize=(16, 9))
其次,在你的内部循环完成后显示情节
plt.show()
三、修改sub-plot索引
[[(4, 2, 1), (4, 2, 2), (4, 1, 3)], [(4, 2, 1), (4, 2, 2), (4, 1, 3)]], #Columns
这里是修改后的绘图函数
def Graphing():
# Size of the figure
for count, (y,plots,iterator,title) in enumerate(zip(
[df['col1'],df['col2']], # Range Columns
[[(4, 2, 1), (4, 2, 2), (4, 1, 3)], [(4, 2, 1), (4, 2, 2), (4, 1, 3)]], #Columns
[0,3], #iterator used to give different names to subplot_
['Column 1', 'Column 2'])): #Column used
fig = plt.figure(figsize=(16, 9))
for subplot_,add_plot,y,title2 in zip(
[f'sub{iterator+0}',f'sub{iterator+1}',f'sub{iterator+2}'], #Name of the subplot sub0, sub1..
[plots[0],plots[1],plots[2]], # cordinates of the subplot 4, 2, 1), (4, 2, 2) ...
[y,y,np.cumsum(y)],
['Line Plot','Line Plot', 'Cumulative Plot']): # Range values for the plot
#Adds Subplot
subplot_ = fig.add_subplot(*add_plot)
#Title
subplot_.set(title=f'{title} {title2}')
# Sets up the x,y, color of the plot
getattr(subplot_, 'plot')(df['x'],y,color='r', marker='o')
plt.show()
# Show Graph
# plt.show()
我正在尝试创建 6 个子图。 2 个线图位于同一行,其下方的行将具有累积图,它将占据 2 列的 space。它将为 col1
和 col2
执行此操作,总共创建 6 个图(每个范围 3 个)。然而,这些图重叠了,我该如何修复它并使其符合 Format of the Plots
.
图表格式:
-line plot `col1`(column=1, row=1), line plot `col1` (column=2, row=1)
-cumulative plot `col1`(column= 1 and 2, row=2)
-line plot `col2`(column=1, row=3), line plot `col2` (column=2, row=3)
-cumulative plot `col2`(column= 1 and 2, row=4)
代码:
import matplotlib.pyplot as plt
import pandas as pd
import numpy as np
df = pd.DataFrame({
'x': [1,2,3,4,5,6],
'col1': [5,3,4,8,9,11],
'col2': [2,6,3,11,3,6]
})
def Graphing():
# Size of the figure
fig = plt.figure(figsize=(16, 9))
for count, (y,plots,iterator,title) in enumerate(zip(
[df['col1'],df['col2']], # Range Columns
[[(4, 2, 1), (4, 2, 2), (4, 1, 2)], [(4, 2, 1), (4, 2, 2), (4, 1, 4)]], #Columns
[0,3], #iterator used to give different names to subplot_
['Column 1', 'Column 2'])): #Column used
for subplot_,add_plot,y,title2 in zip(
[f'sub{iterator+0}',f'sub{iterator+1}',f'sub{iterator+2}'], #Name of the subplot sub0, sub1..
[plots[0],plots[1],plots[2]], # cordinates of the subplot 4, 2, 1), (4, 2, 2) ...
[y,y,np.cumsum(y)],
['Line Plot','Line Plot', 'Cumulative Plot']): # Range values for the plot
#Adds Subplot
subplot_ = fig.add_subplot(*add_plot)
#Title
subplot_.set(title=f'{title} {title2}')
# Sets up the x,y, color of the plot
getattr(subplot_, 'plot')(df['x'],y,color='r', marker='o')
# Show Graph
plt.show()
Graphing()
输出:
将它移到第一个 for 循环中
fig = plt.figure(figsize=(16, 9))
其次,在你的内部循环完成后显示情节
plt.show()
三、修改sub-plot索引
[[(4, 2, 1), (4, 2, 2), (4, 1, 3)], [(4, 2, 1), (4, 2, 2), (4, 1, 3)]], #Columns
这里是修改后的绘图函数
def Graphing():
# Size of the figure
for count, (y,plots,iterator,title) in enumerate(zip(
[df['col1'],df['col2']], # Range Columns
[[(4, 2, 1), (4, 2, 2), (4, 1, 3)], [(4, 2, 1), (4, 2, 2), (4, 1, 3)]], #Columns
[0,3], #iterator used to give different names to subplot_
['Column 1', 'Column 2'])): #Column used
fig = plt.figure(figsize=(16, 9))
for subplot_,add_plot,y,title2 in zip(
[f'sub{iterator+0}',f'sub{iterator+1}',f'sub{iterator+2}'], #Name of the subplot sub0, sub1..
[plots[0],plots[1],plots[2]], # cordinates of the subplot 4, 2, 1), (4, 2, 2) ...
[y,y,np.cumsum(y)],
['Line Plot','Line Plot', 'Cumulative Plot']): # Range values for the plot
#Adds Subplot
subplot_ = fig.add_subplot(*add_plot)
#Title
subplot_.set(title=f'{title} {title2}')
# Sets up the x,y, color of the plot
getattr(subplot_, 'plot')(df['x'],y,color='r', marker='o')
plt.show()
# Show Graph
# plt.show()