How to plot data from multiple two column text files with legends in Matplotlib?

前端 未结 3 1597
眼角桃花
眼角桃花 2020-11-28 08:52

How do I open multiple text files from different directories and plot them on a single graph with legends?

3条回答
  •  [愿得一人]
    2020-11-28 09:35

    I feel the simplest way would be

     from matplotlib import pyplot;
     from pylab import genfromtxt;  
     mat0 = genfromtxt("data0.txt");
     mat1 = genfromtxt("data1.txt");
     pyplot.plot(mat0[:,0], mat0[:,1], label = "data0");
     pyplot.plot(mat1[:,0], mat1[:,1], label = "data1");
     pyplot.legend();
     pyplot.show();
    
    1. label is the string that is displayed on the legend
    2. you can plot as many series of data points as possible before show() to plot all of them on the same graph This is the simple way to plot simple graphs. For other options in genfromtxt go to this url.

提交回复
热议问题