使用Python中的matplotlib进行多个绘图 [英] Multiple plots with matplotlib in Python

查看:45
本文介绍了使用Python中的matplotlib进行多个绘图的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我要使用Python并边做边学.我想用Python中的matplotlib绘制两个图.第二个情节保留了第一个情节的极限.想知道如何改变上一个下一个图的极限.请帮忙.推荐的方法是什么?

I'm to Python and learning it by doing. I want to make two plots with matplotlib in Python. 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()

推荐答案

有两种方法:

快速简便的方法;将每个图中的x和y限制设置为所需的值.

The quick and easy way; set the x and y limits in each plot to what you want.

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

(例如).只需将这两行粘贴在plt.show()之前,它们将是相同的.

(for example). Just paste those two lines just before both plt.show() and they'll be the same.

更困难但更好的方法,这是使用子图.

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)

这篇关于使用Python中的matplotlib进行多个绘图的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆