简体   繁体   English

使用Python中的matplotlib进行多个绘图

[英]Multiple plots with matplotlib in Python

I'm to Python and learning it by doing. 我要使用Python并从中学到它。 I want to make two plots with matplotlib in Python. 我想用Python中的matplotlib绘制两个图。 The second plot keeps the limits of first one. 第二个情节保留了第一个情节的极限。 Wonder how I can change the limits of each next plot from previous. 想知道如何改变上一个下一个图的极限。 Any help, please. 请帮忙。 What is the recommended method? 推荐的方法是什么?

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

Here is one way for you using subplot where plt.subplot(1, 2, 1) means a figure with 1 row (first value) and 2 columns (second value) and 1st subfigure (third value in the bracket, meaning left column in this case). 这是使用subplot一种方法,其中plt.subplot(1, 2, 1)表示具有1行(第一个值)和2列(第二个值)和第一个子图(括号中的第三个值)的图形这个案例)。 plt.subplot(1, 2, 2) means subplot in the 2nd column (right column in this case). plt.subplot(1, 2, 2)表示第二列(在这种情况下为右列plt.subplot(1, 2, 2)子图。

This way, each figure will adjust the x- and y-limits according to the data. 这样,每个图形都会根据数据调整x和y极限。 There are another ways to do the same thing. 还有另一种方法可以做同样的事情。 Here is a SO link for you. 是您的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

Output 输出量

在此处输入图片说明

There are two ways: 有两种方法:

The quick and easy way; 快速简便的方法; set the x and y limits in each plot to what you want. 将每个图中的x和y限制设置为所需的值。

plt.xlim(60,200)
plt.ylim(60,200)

(for example). (例如)。 Just paste those two lines just before both plt.show() and they'll be the same. 只需将这两行粘贴在plt.show()之前,它们将是相同的。

The harder, but better way and this is using subplots. 更困难但更好的方法是使用子图。

# 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.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM