forked from CelestialGoonSquad/Plotting_Potato
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Plotting_Potato.py
183 lines (131 loc) · 4.72 KB
/
Plotting_Potato.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
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
'''
Plotting Potato is here to plot:
column data from a file in supported formats:
.xlsx, .xls (data taken from first sheet by default)
.csv, .txt, .dat (tab, comma, semicolon delimited)
linear, semilog, log funcitons
-To set filepath see PlotData()
-To set fit function see func()
-To edit various parameters of the graphs,
refer to main() or to a corresponding def:
[PlotData or PlotFunction or FitFunction or FitPoly]
By default, everything will be constructed on the same plot.
If you will try to change styles and want to revert to default,
make sure to re-run the code in a new console.
gl hf
-Platobob
'''
from matplotlib import style
import matplotlib.pyplot as plt
from numpy import genfromtxt
import xlrd
import sys
import numpy as np
try:
from Fitting import *
except ImportError:
pass
def main():
#size of the plot window
plt.figure(figsize=(10,6), dpi=80)
'''
#------Plot Style-----
#xkcd-styled plots...try it, I dare you :P
plt.xkcd()
#additional ones
print style.available
style.use('fivethirtyeight')
'''
#---------------------
#What do you want to plot?
#Comment out whichever you don't need here
x, y = PlotData('Smile') #plots the data; go to the function to indicta filename
PlotFunction('JokerEyes')
#FitPoly('PolyFit',x,y,2) #fits a polynomial [change the last parameter to the degree order]
#FitFunction('FuncFit',x, y, a=0, b=0, c=0) #lsqrfit a func() [edit below], must provide initial guess
#---------------------
#position of the legend, can be from 1 to 10
plt.legend(loc=4)
plt.grid(True)
#figure x and y ranges are automatic unless specified
#plt.xlim([0.0, 1.0])
plt.tick_params(axis='x', labelsize=12)
#plt.ylim([0.0, 6.0])
plt.tick_params(axis='y', labelsize=12)
plt.xlabel(r'Swag', fontsize=18)
plt.ylabel(r'Baba', fontsize=18)
plt.title(r'Yolo', fontsize=22)
#save the figure using next line; choose w/e format (ex: .png, .jpg, .eps)
#plt.savefig('BlazeIt.png', format = 'png')
plt.show()
# Desired fit Function
def func(x,a,b,c):
return a*x**2+b*x+c
def PlotData(name):
print("----------- Begin PlotData -----------")
#path to your data
filename = r'data1.xlsx'
#Sheet number in case it is .xlsx file; won't have any effect for .txt and such
sheetnumb = 0
values = ImportData(filename, sheetnumb)
x = values[0] #Column containing x-values
y = values[1] #Column containing y-values
#xerr = values[2] #Column containing error values for x
#yerr = values[3] #Column containing error values for y
#plot the data; index of values[i] corresponds to column #, starting with 0
#set linestyle="None" if want to see the data points only
plt.plot(x, y, marker = 'o', linestyle='-', label = name)
#plot the errorbars
#plt.errorbar(x, y, yerr, xerr, linestyle="None", color='black')
print("----------- End PlotData ----------- \n")
return x,y
def PlotFunction(name):
#assing what values of x to use in form np.linspace(min,max,number of points to take)
x = np.linspace(-1,1,100)
y = abs(np.sin(3*x))/4
plt.plot(x, y, label = name)
#semilog scale
#plt.semilogx(x, y, linestyle='None', marker = 'o', label = 'cookie')
#log scale
#plt.loglog(x, y, basex=10, basey=10, linestyle='-', marker = 'o', label = 'cookie')
#--------------No Need to Go beyond this point----------------------
def ImportData(filename, sheetnumb):
global content
global sheet
values = []
if filename.endswith('.xlsx') or filename.endswith('.xls'):
content = xlrd.open_workbook(filename, 'r')
sheet = content.sheet_by_index(sheetnumb)
column = sheet.ncols
row = sheet.nrows
excel = True
elif filename.endswith('.csv') or filename.endswith('.txt') or filename.endswith('.dat'):
content = genfromtxt(filename)
column = len(content[0])
row = len(content)
excel = False
else:
sys.exit("Wtf, mate?")
print('# of Columns: %d'% column)
print('# of Rows: %d'% row)
for j in range(column):
data = []
counter = 0
for i in range(0, row):
try:
data.append(float(point(i,j, excel)))
except:
if counter == 0:
print('Column %d title: %s'%(j,point(i,j, excel)))
counter +=1
else:
break
values.append(data)
return values
def point(i,j, excel):
if excel:
return sheet.cell(i,j).value
else:
return content[i,j]
if __name__ == '__main__':
main()