[英]Multiple plots with matplotlib in Python
我要使用Python并从中学到它。 我想用Python中的matplotlib绘制两个图。 第二个情节保留了第一个情节的极限。 想知道如何改变上一个下一个图的极限。 请帮忙。 推荐的方法是什么?
X1 = [80, 100, 120, 140, 160, 180, 200, 220, 240, 260]
Y1 = [70, 65, 90, 95, 110, 115, 120, 140, 155, 150]
from matplotlib import pyplot as plt
plt.plot(
X1
, Y1
, color = "green"
, marker = "o"
, linestyle = "solid"
)
plt.show()
X2 = [80, 100, 120, 140, 160, 180, 200]
Y2 = [70, 65, 90, 95, 110, 115, 120]
plt.plot(
X2
, Y2
, color = "green"
, marker = "o"
, linestyle = "solid"
)
plt.show()
这是使用subplot
一种方法,其中plt.subplot(1, 2, 1)
表示具有1行(第一个值)和2列(第二个值)和第一个子图(括号中的第三个值)的图形这个案例)。 plt.subplot(1, 2, 2)
表示第二列(在这种情况下为右列plt.subplot(1, 2, 2)
子图。
这样,每个图形都会根据数据调整x和y极限。 还有另一种方法可以做同样的事情。 这是您的SO链接。
from matplotlib import pyplot as plt
fig = plt.figure(figsize=(10, 4))
plt.subplot(1, 2, 1)
X1 = [80, 100, 120, 140, 160, 180, 200, 220, 240, 260]
Y1 = [70, 65, 90, 95, 110, 115, 120, 140, 155, 150]
plt.plot(X1, Y1, color = "green", marker = "o", linestyle = "solid")
# plt.plot(X1, Y1, '-go') Another alternative to plot in the same style
plt.subplot(1, 2, 2)
X2 = [80, 100, 120, 140, 160, 180, 200]
Y2 = [70, 65, 90, 95, 110, 115, 120]
plt.plot(X2, Y2, color = "green", marker = "o", linestyle = "solid")
# plt.plot(X2, Y2, '-go') Another alternative to plot in the same style
输出量
有两种方法:
快速简便的方法; 将每个图中的x和y限制设置为所需的值。
plt.xlim(60,200)
plt.ylim(60,200)
(例如)。 只需将这两行粘贴在plt.show()之前,它们将是相同的。
更困难但更好的方法是使用子图。
# create a figure object
fig = plt.figure()
# create two axes within the figure and arrange them on the grid 1x2
ax1 = fig.add_Subplot(121)
# ax2 is the second set of axes so it is 1x2, 2nd plot (hence 122)
# they won't have the same limits this way because they are set up as separate objects, whereas in your example they are the same object that is being re-purposed each time!
ax2 = fig.add_Subplot(122)
ax1.plot(X1,Y1)
ax2.plot(X2,Y2)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.