如何使用matplotlib -pyhton在同一窗口中读取文件来生成多个图

发布于 2025-02-04 14:11:48 字数 634 浏览 4 评论 0原文

我有两个.txt文件,与它们一起,我想绘制与同一窗口中每个文件相对应的图形。我设法只绘制一个。使用以下代码:

import matplotlib.pyplot as plt

timecap = []
qtd = []

# f = open('dataset.txt', 'r')

for line in open('pkts_by_src.txt', 'r'):
    lines = [i for i in line.split(',')]
    timecap.append(lines[0])
    qtd.append(int(lines[1]))

plt.title("Capture")
plt.xlabel('time cap')
plt.ylabel('qtd')
plt.yticks(qtd)
plt.plot(timecap, qtd, marker='o', c='g')

plt.show()

文件以这种格式:

22:40:16,2
22:40:20,1
22:40:20,2
22:40:23,1
22:40:23,4
22:40:23,6
22:40:23,8

您能给我一个提示吗?我是Python的初学者

I have two .txt files and with them, I would like to plot the graphs corresponding to each file in the same window. I managed to plot only one. Using the code below:

import matplotlib.pyplot as plt

timecap = []
qtd = []

# f = open('dataset.txt', 'r')

for line in open('pkts_by_src.txt', 'r'):
    lines = [i for i in line.split(',')]
    timecap.append(lines[0])
    qtd.append(int(lines[1]))

plt.title("Capture")
plt.xlabel('time cap')
plt.ylabel('qtd')
plt.yticks(qtd)
plt.plot(timecap, qtd, marker='o', c='g')

plt.show()

The file is in this format:

22:40:16,2
22:40:20,1
22:40:20,2
22:40:23,1
22:40:23,4
22:40:23,6
22:40:23,8

Can you give me a tip? I'm a beginner in python

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

最舍不得你 2025-02-11 14:11:48

尝试使用如图所示的子图函数。调用它使您的图形重复后,最后显示(),您可以查看Matplotlib文档中的特定参数。

plt.subplot(211)
for line in open('pkts_by_src.txt', 'r'):
    lines = [i for i in line.split(',')]
   ...

plt.subplot(212)
for line in open('wordlist2.txt', 'r'):
    lines = [i for i in line.split(',')]
   ...

plt.show()

Try using the subplot function as shown. After calling it make your graph the repeat, at the end do show(), you can look at the matplotlib documentation for specific parameters.

plt.subplot(211)
for line in open('pkts_by_src.txt', 'r'):
    lines = [i for i in line.split(',')]
   ...

plt.subplot(212)
for line in open('wordlist2.txt', 'r'):
    lines = [i for i in line.split(',')]
   ...

plt.show()
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文