如何使用python中的matplotlib在循环中绘制来自多个文件的数据?

时间:2016-09-02 08:52:42

标签: python loops csv matplotlib plot

我有超过1000个文件,它们是.CSV(data_1.csv ...... data1000.csv),每个文件都包含X和Y值!

x1  y1   x2  y2
5.0 60  5.5 500
6.0 70  6.5 600
7.0 80  7.5 700
8.0 90  8.5 800
9.0 100 9.5 900

我在python中创建了一个subplot程序,它可以使用一个文件一次给出两个图(plot1-X1vsY1,Plot2-X2vsY2)。 我需要帮助循环所有文件,(打开一个文件,读取它,绘制它,选择另一个文件,打开它,读取它,绘制它,然后下一个.....直到文件夹中的所有文件被绘制)

我做了一个小代码:

import pandas as pd
import matplotlib.pyplot as plt

df1=pd.read_csv("data_csv",header=1,sep=',')
fig = plt.figure()
plt.subplot(2, 1, 1)
plt.plot(df1.iloc[:,[1]],df1.iloc[:,[2]])

plt.subplot(2, 1, 2)
plt.plot(df1.iloc[:,[3]],df1.iloc[:,[4]])

plt.show()

任何循环结构能更有效地完成这项任务吗?

5 个答案:

答案 0 :(得分:2)

您可以使用package test; import com.google.common.collect.HashMultimap; import com.google.common.collect.Lists; import com.google.common.collect.Multimaps; import com.google.common.collect.SetMultimap; import java.util.List; import java.util.Objects; import java.util.Optional; import java.util.stream.Collectors; public class Test { public static SetMultimap<String, Image> mm; public static void main(String args[]) { mm = Multimaps.synchronizedSetMultimap(HashMultimap.<String, Image>create()); Image i1 = new Image("1", ImageState.DONE, "original", "path1"); Image i2 = new Image("2", ImageState.DONE, "original", "path1"); Image i3 = new Image("3", ImageState.DONE, "original", "path1"); Image i4 = new Image("2", ImageState.DONE, "original", "path2"); Image i5 = new Image("2", ImageState.DONE, "original", "path2"); mm.put("1", i1); mm.put(i2.getId(), i2); mm.put("3", i3); mm.put("2", i4); mm.put(i5.getId(), i5); System.out.println("original multimap:" + mm.toString()); // when altering an object everything goes down the drain and // objects are not removed correctly as expected. getImageByMetadata(i2).setStatus(ImageState.PENDING); Image i6 = getImageByMetadata(i2); System.out.println(i2); System.out.println(i6); mm.remove(i6.getId(), i6); System.out.println("multimap should be smaller 1:" + mm.toString()); mm.get(i6.getId()).remove(i6); System.out.println("multimap should be smaller 2:" + mm.toString()); mm.asMap().get(i2.getId()).remove(i2); System.out.println("multimap should be smaller 3:" + mm.toString()); removeByPath("path2"); removeByPath("path1"); System.out.println("should remove 2 elements:" + mm.toString()); } public static Image getImageByMetadata(Image imageMetadata) { List<Image> images = Lists.newArrayList(mm.values()); List<Image> filteredImage = mm.values().stream() .filter(Objects::nonNull) .filter(image -> imageMetadata.getId().equals(image.getId()) && imageMetadata.getFormat().equals(image.getFormat()) && Optional.ofNullable(image.getPath()) .filter(thisPath -> thisPath.contains(imageMetadata.getPath())) .isPresent() ).collect(Collectors.toList()); if (filteredImage.size() == 0) { return new Image(); } else { return filteredImage.get(0); } } public static void removeByPath(String path) { mm.get("2").stream() .filter(Objects::nonNull) .filter(image -> Optional.ofNullable(image.getPath()) .filter(imagePath -> imagePath.contains(path) && image.getStatus().equals(ImageState.DONE)) .isPresent() ).forEach(img -> { mm.values().remove(img); }); } } class Image { private String id; private ImageState status; private String format; private String path; public Image() { } public Image(String id, ImageState status, String format, String path) { this.id = id; this.status = status; this.format = format; this.path = path; } public ImageState getStatus() { return status; } public Image setStatus(ImageState status) { this.status = status; return this; } public String getPath() { return path; } public Image setPath(String path) { this.path = path; return this; } public String getId() { return id; } public void setId(String thatId) { id = thatId; } public String getFormat() { return format; } public void setFormat(String thatFormat) { format = thatFormat; } @Override public boolean equals(Object o) { if (this == o) return true; if (!(o instanceof Image)) return false; Image image = (Image) o; return com.google.common.base.Objects.equal(id, image.id) && status == image.status && com.google.common.base.Objects.equal(format, image.format) && com.google.common.base.Objects.equal(path, image.path); } @Override public int hashCode() { return com.google.common.base.Objects.hashCode(id, status, format, path); } @Override public String toString() { return "Image{" + "id='" + id + '\'' + ", status=" + status + ", format='" + format + '\'' + ", path='" + path + '\'' + '}'; } } enum ImageState { DONE, PENDING, ABORTED, NOT_FOUND } 生成文件名列表,然后在for循环中绘制它们。

glob

答案 1 :(得分:0)

以下是在工作中使用的基本设置。此代码将分别绘制每个文件和每个文件的数据。只要列名保持不变,这将适用于任意数量的文件。只需将其指向正确的文件夹即可。

import os
import csv

def graphWriterIRIandRut():
    m = 0
    List1 = []
    List2 = []
    List3 = []
    List4 = []
    fileList = []
    for file in os.listdir(os.getcwd()):
        fileList.append(file)
    while m < len(fileList):
        for col in csv.DictReader(open(fileList[m],'rU')):
            List1.append(col['Col 1 Name'])
            List2.append(col['Col 2 Name'])
            List3.append(col['Col 3 Name'])
            List4.append(col['Col 4 Name'])

        plt.subplot(2, 1, 1)
        plt.grid(True)
        colors = np.random.rand(n)
        plt.plot(List1,List2,c=colors)
        plt.tick_params(axis='both', which='major', labelsize=8)

        plt.subplot(2, 1, 2)
        plt.grid(True)
        colors = np.random.rand(n)
        plt.plot(List1,List3,c=colors)
        plt.tick_params(axis='both', which='major', labelsize=8)

        m = m + 1
        continue

    plt.show()
    plt.gcf().clear()
    plt.close('all')

答案 2 :(得分:0)

pod install

答案 3 :(得分:0)

我们想要做的是为每次迭代或文件创建一个新的空列表。因此,对于每次迭代,将绘制数据,但是一旦绘制了该数据,将创建新的空列表并绘制。一旦绘制了每个文件的所有数据,那么你最终想要plt.show(),它将显示所有的图。以下是我遇到的类似问题的链接:Traceback lines on plot of multiple files。祝你好运!

import csv 
import matplotlib.pyplot as plt

def graphWriter():

    for file in os.listdir(os.getcwd()):
        List1 = []
        List2 = []
        List3 = []
        List4 = []

        with open(filename, 'r') as file:
            for col in csv.DictReader(file):            
                List1.append(col['x1'])
                List2.append(col['y1'])
                List3.append(col['x2'])
                List4.append(col['y2'])

        plt.subplot(2, 1, 1)
        plt.grid(True)
        colors = np.random.rand(2)
        plt.plot(List1,List2,c=colors)
        plt.tick_params(axis='both', which='major', labelsize=8)

        plt.subplot(2, 1, 2)
        plt.grid(True)
        colors = np.random.rand(2)
        plt.plot(List1,List3,c=colors)
        plt.tick_params(axis='both', which='major', labelsize=8)

    plt.show()
    plt.gcf().clear()
    plt.close('all')

答案 4 :(得分:0)

为了防止有人对使用NetCDF数据感兴趣,我使用了NetCDF(.nc)。另外,您也可以用.txt替换它,想法是一样的。我将其用于轮廓绘图循环。

path_to_folder='#type the path to the files'

count=0
fig = plt.figure(figsize=(10,5))

files = []
for i in os.listdir(path_to_folder):
    if i.endswith('.nc'):
        count=count+1
        files.append(open(i))
        data=xr.open_dataset(i)
        prec=data['tp']
        plt.subplot(1, 2, count) # change 1 and 2 to the shape you want
        prec.groupby('time.month').mean(dim=('time','longitude')).T.plot.contourf(cmap='Purples') *#this is to plot contour plot but u can replace with any plot command

print(files)
plt.savefig('try,png',dpi=500,orientation='landscape',format='png')