如何使用彼此相邻的matplotlib绘制两个不同的子图?
How to draw two different subplots using matplotlib adjacent to each other?
我有两个不同的图,第一个是 Frequency
vs Features
,另一个是 Frequency
vs Yield
Loss.
每个图都是带有趋势线的条形图,在 Y 轴右侧显示累积百分比或产量损失。
因此,对于每个图表,X 轴显示特征名称,左侧 Y 轴显示频率,右侧 Y 轴显示累积百分比或产量损失。
这是我编写的代码,它显示了以下输出。
#Trendline_change_2
import matplotlib.pyplot as plt
fig, ax1 = plt.subplots()
ax1 = df.iloc[:HOW_MANY,:]['FailCount'].plot(kind="bar",stacked = True,legend=False,figsize=(8,8), label = 'frequency plot',color = 'green',title="Frequency of failure vs Feature") #Graph naming changes
ax1.set_ylabel('Frequency', color='b')
ax1.tick_params('y', colors='b')
ax1.set_xticklabels(np.asarray(df['Feature'])[:HOW_MANY])
ax2 = ax1.twinx()
ax2.plot(np.asarray(df['Cummulative_aggr'])[:HOW_MANY],linestyle='--', marker='s',label ='cummulative %', color='r')
ax2.set_ylabel('Cummulative %', color='r')
plt.legend(loc='best')
#Plot 2
fig, ax1 = plt.subplots()
ax1 = df.iloc[:HOW_MANY,:]['FailCount'].plot(kind="bar",stacked = True,legend=False,figsize=(8,8), label = 'frequency plot',color = 'green',title="Frequency of failure vs Feature") #Graph naming changes
ax1.set_ylabel('Frequency', color='b')
ax1.tick_params('y', colors='b')
ax1.set_xticklabels(np.asarray(df['Feature'])[:HOW_MANY])
ax2 = ax1.twinx()
ax2.plot(np.asarray(df['Yield_Loss'])[:HOW_MANY],linestyle='--', marker='s',label ='Yield Loss', color='black')
ax2.set_ylabel('Yield Loss', color='black')
plt.legend(loc='best')
plt.tight_layout()
plt.show()
一切都很好除了我想并排更改两个图而不是一个在另一个下方。
我尝试了一些方法来让它们并排显示 plt.subplot(1,2,1)
和其他参数,但结果并不令人满意。
谁能帮我调整一下?我知道我很接近但无法正确处理。
通常 fig, (ax1, ax2) = plt.subplots(1, 2)
应该可以正常工作。然后像以前一样使用 ax1 和 ax2 创建图形。
Documentation
使用
fig, (ax1, ax2) = plt.subplots(1,2)
plt.subplots(a,b)
a - 每列中的图数
b - 每行中的地块数量
https://matplotlib.org/3.3.2/api/_as_gen/matplotlib.pyplot.subplots.html
查看以上文档以获得更好的理解。
似乎带有内联图像显示的 Jupyter 环境让人很难理解为什么图形和子图不一样。您的问题的 matplotlib 答案已经给出 - create subplots 并填写它们:
import matplotlib.pyplot as plt
import numpy as np
#Figure 1, 2 subplots
fig, (ax1, ax3) = plt.subplots(1, 2, figsize=(10,5))
#subplot 1
ax1.plot(np.arange(10), np.random.randint(1, 5, 10), c="blue")
ax2 = ax1.twinx()
ax2.plot(np.arange(5, 15), np.random.randint(10, 100, 10), c="red", label="Subplot 1")
plt.legend(loc="best")
#subplot 2
ax4 = ax3.twinx()
ax3.plot(np.arange(10), np.random.randint(2,20, 10), c="grey")
ax4.plot(np.arange(5, 15), np.random.randint(-10, 0, 10), c="yellow", label="Subplot 2")
#now plot both subplots into one figure
plt.legend(loc="best")
plt.tight_layout()
plt.show()
这将创建一个图(想想一个 window 或一页),其中包含两个子图:
您的方法是创建两个单独的图形,每个图形包含一个子图:
import matplotlib.pyplot as plt
import numpy as np
#Figure 1
fig, ax1 = plt.subplots()
ax1.plot(np.arange(10), np.random.randint(1, 5, 10), c="blue")
ax2 = ax1.twinx()
ax2.plot(np.arange(5, 15), np.random.randint(10, 100, 10), c="red", label="Figure 1")
plt.legend()
#Figure 2
fig, ax1 = plt.subplots()
ax2 = ax1.twinx()
ax1.plot(np.arange(10), np.random.randint(2,20, 10), c="grey")
ax2.plot(np.arange(5, 15), np.random.randint(-10, 0, 10), c="yellow", label="Figure 2")
plt.legend()
plt.show()
正如预期的那样,这将创建两个图形。在我的环境中,有两个 windows:
在您的环境中(此处猜测,因为我不了解 Jupyter),内联打印将两个图形放在不同的行上 - 因此,它们看起来就像您在 matplotlib 中创建了两个垂直排列的子图。
总而言之,这似乎是一个 Jupyter 问题——“如何并排绘制内联图像?”,而不是 matplotlib 问题,如果我理解正确的话。话虽如此 - 回答“我如何在 matplotlib 中安排子图?”这个问题不是更容易吗? (你已经得到了几个答案),然后只绘制一个数字?很难理解为什么需要两个数字然后并排显示。
我有两个不同的图,第一个是 Frequency
vs Features
,另一个是 Frequency
vs Yield
Loss.
每个图都是带有趋势线的条形图,在 Y 轴右侧显示累积百分比或产量损失。
因此,对于每个图表,X 轴显示特征名称,左侧 Y 轴显示频率,右侧 Y 轴显示累积百分比或产量损失。
这是我编写的代码,它显示了以下输出。
#Trendline_change_2
import matplotlib.pyplot as plt
fig, ax1 = plt.subplots()
ax1 = df.iloc[:HOW_MANY,:]['FailCount'].plot(kind="bar",stacked = True,legend=False,figsize=(8,8), label = 'frequency plot',color = 'green',title="Frequency of failure vs Feature") #Graph naming changes
ax1.set_ylabel('Frequency', color='b')
ax1.tick_params('y', colors='b')
ax1.set_xticklabels(np.asarray(df['Feature'])[:HOW_MANY])
ax2 = ax1.twinx()
ax2.plot(np.asarray(df['Cummulative_aggr'])[:HOW_MANY],linestyle='--', marker='s',label ='cummulative %', color='r')
ax2.set_ylabel('Cummulative %', color='r')
plt.legend(loc='best')
#Plot 2
fig, ax1 = plt.subplots()
ax1 = df.iloc[:HOW_MANY,:]['FailCount'].plot(kind="bar",stacked = True,legend=False,figsize=(8,8), label = 'frequency plot',color = 'green',title="Frequency of failure vs Feature") #Graph naming changes
ax1.set_ylabel('Frequency', color='b')
ax1.tick_params('y', colors='b')
ax1.set_xticklabels(np.asarray(df['Feature'])[:HOW_MANY])
ax2 = ax1.twinx()
ax2.plot(np.asarray(df['Yield_Loss'])[:HOW_MANY],linestyle='--', marker='s',label ='Yield Loss', color='black')
ax2.set_ylabel('Yield Loss', color='black')
plt.legend(loc='best')
plt.tight_layout()
plt.show()
一切都很好除了我想并排更改两个图而不是一个在另一个下方。
我尝试了一些方法来让它们并排显示 plt.subplot(1,2,1)
和其他参数,但结果并不令人满意。
谁能帮我调整一下?我知道我很接近但无法正确处理。
通常 fig, (ax1, ax2) = plt.subplots(1, 2)
应该可以正常工作。然后像以前一样使用 ax1 和 ax2 创建图形。
Documentation
使用
fig, (ax1, ax2) = plt.subplots(1,2)
plt.subplots(a,b)
a - 每列中的图数
b - 每行中的地块数量
https://matplotlib.org/3.3.2/api/_as_gen/matplotlib.pyplot.subplots.html
查看以上文档以获得更好的理解。
似乎带有内联图像显示的 Jupyter 环境让人很难理解为什么图形和子图不一样。您的问题的 matplotlib 答案已经给出 - create subplots 并填写它们:
import matplotlib.pyplot as plt
import numpy as np
#Figure 1, 2 subplots
fig, (ax1, ax3) = plt.subplots(1, 2, figsize=(10,5))
#subplot 1
ax1.plot(np.arange(10), np.random.randint(1, 5, 10), c="blue")
ax2 = ax1.twinx()
ax2.plot(np.arange(5, 15), np.random.randint(10, 100, 10), c="red", label="Subplot 1")
plt.legend(loc="best")
#subplot 2
ax4 = ax3.twinx()
ax3.plot(np.arange(10), np.random.randint(2,20, 10), c="grey")
ax4.plot(np.arange(5, 15), np.random.randint(-10, 0, 10), c="yellow", label="Subplot 2")
#now plot both subplots into one figure
plt.legend(loc="best")
plt.tight_layout()
plt.show()
这将创建一个图(想想一个 window 或一页),其中包含两个子图:
您的方法是创建两个单独的图形,每个图形包含一个子图:
import matplotlib.pyplot as plt
import numpy as np
#Figure 1
fig, ax1 = plt.subplots()
ax1.plot(np.arange(10), np.random.randint(1, 5, 10), c="blue")
ax2 = ax1.twinx()
ax2.plot(np.arange(5, 15), np.random.randint(10, 100, 10), c="red", label="Figure 1")
plt.legend()
#Figure 2
fig, ax1 = plt.subplots()
ax2 = ax1.twinx()
ax1.plot(np.arange(10), np.random.randint(2,20, 10), c="grey")
ax2.plot(np.arange(5, 15), np.random.randint(-10, 0, 10), c="yellow", label="Figure 2")
plt.legend()
plt.show()
正如预期的那样,这将创建两个图形。在我的环境中,有两个 windows:
在您的环境中(此处猜测,因为我不了解 Jupyter),内联打印将两个图形放在不同的行上 - 因此,它们看起来就像您在 matplotlib 中创建了两个垂直排列的子图。
总而言之,这似乎是一个 Jupyter 问题——“如何并排绘制内联图像?”,而不是 matplotlib 问题,如果我理解正确的话。话虽如此 - 回答“我如何在 matplotlib 中安排子图?”这个问题不是更容易吗? (你已经得到了几个答案),然后只绘制一个数字?很难理解为什么需要两个数字然后并排显示。