[英]How to make two plots side-by-side
I found the following example on matplotlib:我在 matplotlib 上找到了下面的例子:
import numpy as np
import matplotlib.pyplot as plt
x1 = np.linspace(0.0, 5.0)
x2 = np.linspace(0.0, 2.0)
y1 = np.cos(2 * np.pi * x1) * np.exp(-x1)
y2 = np.cos(2 * np.pi * x2)
plt.subplot(2, 1, 1)
plt.plot(x1, y1, 'ko-')
plt.title('A tale of 2 subplots')
plt.ylabel('Damped oscillation')
plt.subplot(2, 1, 2)
plt.plot(x2, y2, 'r.-')
plt.xlabel('time (s)')
plt.ylabel('Undamped')
plt.show()
My question is: What do i need to change, to have the plots side-by-side?我的问题是:我需要更改什么才能并排显示这些图?
Change your subplot settings to:将您的子图设置更改为:
plt.subplot(1, 2, 1)
...
plt.subplot(1, 2, 2)
The parameters for subplot
are: number of rows, number of columns, and which subplot you're currently on. subplot
的参数是:行数、列数以及您当前所在的子图。 So 1, 2, 1
means "a 1-row, 2-column figure: go to the first subplot."所以
1, 2, 1
意思是“一个 1 行 2 列的图形:转到第一个子图。” Then 1, 2, 2
means "a 1-row, 2-column figure: go to the second subplot."然后
1, 2, 2
表示“一个 1 行 2 列的图形:转到第二个子图。”
You currently are asking for a 2-row, 1-column (that is, one atop the other) layout.您目前要求 2 行 1 列(即一个在另一个之上)布局。 You need to ask for a 1-row, 2-column layout instead.
您需要改为要求 1 行 2 列布局。 When you do, the result will be:
当你这样做时,结果将是:
In order to minimize the overlap of subplots, you might want to kick in a:为了最大限度地减少子图的重叠,您可能需要启动:
plt.tight_layout()
before the show.演出前。 Yielding:
产量:
Check this page out: http://matplotlib.org/examples/pylab_examples/subplots_demo.html查看此页面: http : //matplotlib.org/examples/pylab_examples/subplots_demo.html
plt.subplots
is similar. plt.subplots
是类似的。 I think it's better since it's easier to set parameters of the figure.我认为它更好,因为设置图形参数更容易。 The first two arguments define the layout (in your case 1 row, 2 columns), and other parameters change features such as figure size:
前两个参数定义布局(在您的情况下为 1 行 2 列),其他参数用于更改图形大小等功能:
import numpy as np
import matplotlib.pyplot as plt
x1 = np.linspace(0.0, 5.0)
x2 = np.linspace(0.0, 2.0)
y1 = np.cos(2 * np.pi * x1) * np.exp(-x1)
y2 = np.cos(2 * np.pi * x2)
fig, axes = plt.subplots(nrows=1, ncols=2, figsize=(5, 3))
axes[0].plot(x1, y1)
axes[1].plot(x2, y2)
fig.tight_layout()
When stacking subplots in one direction, the matplotlib documentation advocates unpacking immediately if you are just creating a few axes.当在一个方向上堆叠子图时,如果您只是创建几个轴, matplotlib 文档提倡立即解包。
fig, (ax1, ax2) = plt.subplots(1,2, figsize=(20,8))
sns.histplot(df['Price'], ax=ax1)
sns.histplot(np.log(df['Price']),ax=ax2)
plt.show()
You can use - matplotlib.gridspec.GridSpec您可以使用 - matplotlib.gridspec.GridSpec
Check - https://matplotlib.org/stable/api/_as_gen/matplotlib.gridspec.GridSpec.html检查 - https://matplotlib.org/stable/api/_as_gen/matplotlib.gridspec.GridSpec.html
The below code displays a heatmap on right and an Image on left.下面的代码在右侧显示一个热图,在左侧显示一个图像。
#Creating 1 row and 2 columns grid
gs = gridspec.GridSpec(1, 2)
fig = plt.figure(figsize=(25,3))
#Using the 1st row and 1st column for plotting heatmap
ax=plt.subplot(gs[0,0])
ax=sns.heatmap([[1,23,5,8,5]],annot=True)
#Using the 1st row and 2nd column to show the image
ax1=plt.subplot(gs[0,1])
ax1.grid(False)
ax1.set_yticklabels([])
ax1.set_xticklabels([])
#The below lines are used to display the image on ax1
image = io.imread("https://images-na.ssl-images- amazon.com/images/I/51MvhqY1qdL._SL160_.jpg")
plt.imshow(image)
plt.show()
Basically we have to define how many rows and columns we require.基本上我们必须定义我们需要多少行和多少列。 Lets Say we have total 4 categorical columns to be plotted.
假设我们总共要绘制 4 个分类列。 Lets have total 4 plots in 2 rows and 2 columns.
让我们在 2 行和 2 列中总共有 4 个图。
import matplotlib.pyplot as plt
import matplotlib
import seaborn as sns
sns.set_style("darkgrid")
%matplotlib inline
#15 by 15 size set for entire plots
plt.figure(figsize=(15,15));
#Set rows variable to 2
rows = 2
#Set columns variable to 2, this way we will plot 2 by 2 = 4 plots
columns = 2
#Set the plot_count variable to 1
#This variable will be used to define which plot out of total 4 plot
plot_count = 1
cat_columns = [col for col in df.columns if df[col].dtype=='O']
for col in cat_columns:
plt.subplot(rows, columns, plot_count)
sns.countplot(x=col, data=df)
plt.xticks(rotation=70);
#plot variable is incremented by 1 till 4, specifying which plot of total 4 plots
plot_count += 1
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.