-
Notifications
You must be signed in to change notification settings - Fork 0
/
plot.py
47 lines (40 loc) · 1.18 KB
/
plot.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
import sys
import tkinter as tk
from tkinter import filedialog
import pandas as pd
import matplotlib.pyplot as plt
# Use tkinter to open a file browser and select a file
root = tk.Tk()
root.withdraw()
filepath = filedialog.askopenfilename()
# Load the file using pandas
if filepath.endswith(".xlsx"):
# Excel file
df = pd.read_excel(filepath)
elif filepath.endswith(".csv"):
# CSV file
df = pd.read_csv(filepath)
elif filepath.endswith(".xy"):
# .xy file
df = pd.read_csv(filepath, sep=" ", names=["x", "y"])
else:
# Unrecognized file type
print("Error: Unrecognized file type")
sys.exit(1)
# Check if the first row contains column headers
if df.iloc[0].dtype == object:
# First row is a header row, skip it
data = df.iloc[1:]
else:
# First row is not a header row, use all rows
data = df
# Plot all of the data
plt.plot(data["x"], data["y"])
# Prompt user to use a GUI to change x, y limits and axes titles
plt.xlim(xmin=0)
plt.ylim(ymin=0)
plt.xlabel("X data")
plt.ylabel("Y data")
plt.show()
# Save the plot to the same folder as the input file
plt.savefig(filepath.rsplit("/", 1)[0] + "/plot.png")