Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/276.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
如何使用Python并排绘制两个绘图?_Python_Matplotlib - Fatal编程技术网

如何使用Python并排绘制两个绘图?

如何使用Python并排绘制两个绘图?,python,matplotlib,Python,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.

我在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()
我的问题是:我需要改变什么,才能让绘图并排进行?

查看此页面:

plt.子批次
类似。我认为这样更好,因为设置图形的参数更容易。前两个参数定义布局(在您的案例中为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()

查看本页:

plt.子批次
类似。我认为这样更好,因为设置图形的参数更容易。前两个参数定义布局(在您的案例中为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()

将子批次设置更改为:

plt.subplot(1, 2, 1)

...

plt.subplot(1, 2, 2)
子批次
的参数包括:行数、列数以及当前所在的子批次。因此
1,2,1
表示“一行两列图形:转到第一个子地块”。然后
1,2,2
表示“一行两列图形:转到第二个子地块。”

您当前要求的是2行1列(即一个在另一个之上)布局。您需要使用一行两列的布局。当您这样做时,结果将是:

为了最大限度地减少子批次的重叠,您可能需要加入一个:

plt.tight_layout()
演出前。屈服:


将子批次设置更改为:

plt.subplot(1, 2, 1)

...

plt.subplot(1, 2, 2)
子批次
的参数包括:行数、列数以及当前所在的子批次。因此
1,2,1
表示“一行两列图形:转到第一个子地块”。然后
1,2,2
表示“一行两列图形:转到第二个子地块。”

您当前要求的是2行1列(即一个在另一个之上)布局。您需要使用一行两列的布局。当您这样做时,结果将是:

为了最大限度地减少子批次的重叠,您可能需要加入一个:

plt.tight_layout()
演出前。屈服:


您可以使用-matplotlib.gridspec.gridspec

检查-

下面的代码在右侧显示热图,在左侧显示图像

#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()

您可以使用-matplotlib.gridspec.gridspec

检查-

下面的代码在右侧显示热图,在左侧显示图像

#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()

在一个方向上堆叠子地块时,如果您只是创建了几个轴,则会立即解包

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()

在一个方向上堆叠子地块时,如果您只是创建了几个轴,则会立即解包

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()

还有什么方法可以使绘图更宽更高吗?我不是说推荐的plt.xlim/ylim会建议使用
plt.subplot
。您可以指定参数
figsize
来定义图形的大小。如
plt.subplot(1,2,figsize=(20,4))
中所示。否则,您可以首先定义图形和图形大小:
plt.figure(figsize=(20,4))
。由于某种原因,我在尝试保存子批次“plt.savefig(“…png”)时会得到一个空白数据。我在开始时使用完全相同的代码(我只添加了plt.figure(figsize=(7,3))。您是在
plt.show
之后调用
plt.savefig
?如果是这样,那很可能是你的问题。还有什么方法可以使绘图更宽、更高?我不是说推荐的plt.xlim/ylim会建议使用
plt.subplot
。您可以指定参数
figsize
来定义图形的大小。如
plt.subplot(1,2,figsize=(20,4))
中所示。否则,您可以首先定义图形和图形大小:
plt.figure(figsize=(20,4))
。由于某种原因,我在尝试保存子批次“plt.savefig(“…png”)时会得到一个空白数据。我在开始时使用完全相同的代码(我只添加了plt.figure(figsize=(7,3))。您是在
plt.show
之后调用
plt.savefig
?如果是这样,那很可能是你的问题。