简体   繁体   English

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

[英]How can I generate multiple plots from reading a file in the same window using matplotlib - Pyhton

I have two .txt files and with them, I would like to plot the graphs corresponding to each file in the same window.我有两个 .txt 文件,我想用它们在同一个窗口中绘制与每个文件对应的图形。 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我是python的初学者

Try using the subplot function as shown.尝试使用 subplot 函数,如图所示。 After calling it make your graph the repeat, at the end do show(), you can look at the matplotlib documentation for specific parameters.调用它使您的图形重复后,最后执行 show(),您可以查看 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()

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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