[英]Matplotlib 2 plots on the same figure with separate sizes
I need to plot 1 large plot at the top of a figure, and then a small plot UNDERNEATH it.我需要 plot 1 个大的 plot 在一个图的顶部,然后一个小的plot在它下面。 Currently I have this code:目前我有这个代码:
times = [1, 2, 3, 4]
temps1 = [1, 2, 3, 4]
temps2 = [10, 20, 30, 40]
temps3 = [100, 200, 300, 400]
plt.subplot(211)
plt.plot(times, temps1, c="#ff7f0e", label="1")
plt.plot(times, temps2, c="#2ca02c", label="2")
plt.plot(times, temps3, c="#1f77b4", label="3")
labels = ['G1', 'G2', 'G3', 'G4', 'G5']
men_means = [20, 35, 30, 35, 27]
women_means = [25, 32, 34, 20, 25]
width = 0.35 # the width of the bars: can also be len(x) sequence
plt.subplot(212)
plt.bar(labels, men_means, width, label='Men')
plt.bar(labels, women_means, width, bottom=men_means,
label='Women')
plt.show()
and this correctly plots the graphs underneath eachother.这正确地绘制了彼此下方的图形。 However, I can't set the size of the second figure (the one underneath) I need it to not be the same size as the other, and I have tried many variations such as changing it to fig,ax =..
and then changing the fig, this didn't work and many other variations.但是,我无法设置第二个图形(下面的那个)的大小,我需要它与另一个不同的大小,并且我尝试了许多变化,例如将其更改为fig,ax =..
然后改变无花果,这不起作用和许多其他变化。 There are some that work with plots next to eachother but not underneath.有些与彼此相邻但不在下方的地块一起使用。 How can I independently change the size of each of these plots?如何独立更改每个图的大小?
I'm not sure if the size of each graph is what you want, so I created them in the appropriate ratio.'我不确定每个图表的大小是否是您想要的,所以我以适当的比例创建了它们。 Use 'GridSpec()' to determine the number of graphs in rows and columns, and specify the row ratio and column ratio as parameters.使用 'GridSpec()' 确定图表的行数和列数,并指定行比和列比作为参数。 Once the placement is determined, associate the graphs to it.Kindly refer to this .确定位置后,将图表与其关联。请参阅此。
import matplotlib.pyplot as plt
times = [1, 2, 3, 4]
temps1 = [1, 2, 3, 4]
temps2 = [10, 20, 30, 40]
temps3 = [100, 200, 300, 400]
fig = plt.figure(figsize=(10, 8))
gs = plt.GridSpec(nrows=2, ncols=1, height_ratios=[3, 1])
ax1 = fig.add_subplot(gs[0, 0])
ax1.plot(times, temps1, c="#ff7f0e", label="1")
ax1.plot(times, temps2, c="#2ca02c", label="2")
ax1.plot(times, temps3, c="#1f77b4", label="3")
labels = ['G1', 'G2', 'G3', 'G4', 'G5']
men_means = [20, 35, 30, 35, 27]
women_means = [25, 32, 34, 20, 25]
width = 0.35
ax2 = fig.add_subplot(gs[1, 0])
ax2.bar(labels, men_means, width, label='Men')
ax2.bar(labels, women_means, width, bottom=men_means, label='Women')
plt.show()
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.