I'm just trying to graph some simple data and whether I try to do it with plot or subplot it comes out the same. All values in my lists are positive but the y axis is acting like a number line with only positives.
import matplotlib.pyplot as plt
xVal = []
yVal1 = []
yVal2 = []
yVal3 = []
data = []
# load data
with open(r"path", 'r') as f:
data = f.readlines()
yVal1 = data[0].split(",")
yVal2 = data[1].split(",")
yVal3 = data[2].split(",")
del yVal1[-1]
del yVal2[-1]
del yVal3[-1]
print(yVal1)
print(yVal2)
print(yVal3)
# graph dem bois
xVal = [*range(0, len(yVal1))]
'''fig, ax = plt.subplots(3)
ax[0].plot(xVal, yVal1)
ax[0].set_title("pm5")
ax[1].plot(xVal, yVal2)
ax[1].set_title("pm7.5")
ax[2].plot(xVal, yVal3)
ax[2].set_title("pm10")
fig.suptitle("Particulate Levels over time")'''
plt.plot(xVal, yVal3)
plt.show()
As per the comment by Jody Klymak I converted the string lists into float lists and it worked.
fyVal1 = [float(x) for x in yVal1]
Related
Hi I am trying to extract data from a netCDF file, but the data is upside down. How can I reverse the database:
The data I want to extract is the height data from the (netcdf) at the points I have in the CSV file. my Data:
import numpy as np
from netCDF4 import Dataset
import matplotlib.pyplot as plt
import pandas as pd
from mpl_toolkits.basemap import Basemap
from matplotlib.patches import Path, PathPatch
csv_data = np.loadtxt('CSV with target coordinates',skiprows=1,delimiter=',')
num_el = csv_data[:,0]
lat = csv_data[:,1]
lon = csv_data[:,2]
value = csv_data[:,3]
data = Dataset("elevation Data",'r')
lon_range = data.variables['x_range'][:]
lat_range = data.variables['y_range'][:]
topo_range = data.variables['z_range'][:]
spacing = data.variables['spacing'][:]
dimension = data.variables['dimension'][:]
z = data.variables['z'][:]
lon_num = dimension[0]
lat_num = dimension[1]
etopo_lon = np.linspace(lon_range[0],lon_range[1],dimension[0])
etopo_lat = np.linspace(lat_range[0],lat_range[1],dimension[1])
topo = np.reshape(z, (lat_num, lon_num))
height = np.empty_like(num_el)
desired_lat_idx = np.empty_like(num_el)
desired_lon_idx = np.empty_like(num_el)
for i in range(len(num_el)):
tmp_lat = np.abs(etopo_lat - lat[i]).argmin()
tmp_lon = np.abs(etopo_lon - lon[i]).argmin()
desired_lat_idx[i] = tmp_lat
desired_lon_idx[i] = tmp_lon
height[i] = topo[tmp_lat,tmp_lon]
height[height<-10]=0
print(len(desired_lat_idx))
print(len(desired_lon_idx))
print(len(height))
dfl= pd.DataFrame({
'Latitude' : lat.reshape(-1),
'Longitude': lon.reshape(-1),
'Altitude': height.reshape(-1)
});
print(dfl)
# but the Lat should not be changed here (the dfl must be correct)
df =dfl
lat=np.array(df['Latitude'])
lon=np.array(df['Longitude'])
val=np.array(df['Altitude'])
m = basemap.Basemap(projection='robin', lon_0=0, lat_0=0, resolution='l',area_thresh=1000)
m.drawcoastlines(color = 'black')
x,y = m(lon,lat)
colormesh= m.contourf(x,y,val,100, tri=True, cmap = 'terrain')
plt.colorbar(location='bottom',pad=0.04,fraction=0.06)
plt.show()
I have already tried:
lat = csv_data[:,1]
lat= lat*(-1)
But this didnĀ“t work
It's a plotting artifact().
Just do:
colormesh= m.contourf(x,y[::-1],val,100, tri=True, cmap = 'terrain')
y[::-1] will reverse the order of the y latitude elements (as opposed to the land-mass outlines; and while keeping the x longitude coordinates the same) and hence flip them.
I've often had this problem with plotting numpy image data in the past.
Your raw CSV data are unlikely to be flipped themselves (why would they be?). You should try sanity-checking them [I am not a domain expert I'm afraid]! Overlaying an actual coordinate grid can help with this.
Another way to do it is given here: Reverse Y-Axis in PyPlot
You could also therefore just do
ax = plt.gca()
ax.invert_yaxis()
I am currently writing a program where I can project a hologram video on my computer screen, I had written the code below and I do not know how to specifically rotate a subplot, I had created a 3*3 subplot and I need to rotate subplot 4 by 270 clockwise, subplot 6 by 90 clockwise and subplot 8 by 180.
Second question is how to get rid of all of the axis label... So that the hologram projected will be nice and neatly....
import pandas as pd
import serial
import numpy as np
import matplotlib.pyplot as plt
ser = serial.Serial("COM5", 115200) # define the serial port that we are communicating to and also the baud rate
plt.style.use('dark_background') #define the black background
plt.ion() # tell pyplot we need live data
fig,[[ax1,ax2,ax3],[ax4,ax5,ax6],[ax7,ax8,ax9]] = plt.subplots(3,3) # plotting a figure with 9 subplot
Xplot = []
Yplot = []
Zplot = []
blankx = []
blanky = []
fig = [ax1,ax2,ax3,ax4,ax5,ax6,ax7,ax8,ax9]
while True: #always looping this sequence
while(ser.inWaiting()==0): #if no input from the serial, wait and do nothing
pass
data = ser.readline() #obtain the input from COM 5
data_processed = data.decode('utf-8') #to get rid of the unnecessary string part
data_split = data_processed.split(",") # split the incoming string into a list
x = float(data_split[0]) #to obtain seperate float values for x,y,z
y = float(data_split[1])
z = float(data_split[2])
reset = int(data_split[3]) # reset will output 1
draw = int(data_split[4]) # draw will output 2
if(draw == 2):
Xplot.append(x) #if draw is given instruction, add the x,y,z value into the list to be plot on the graph
Yplot.append(y)
Zplot.append(z)
ax1.plot(blankx,blanky) # subplotting
ax2.plot(Xplot,Yplot,"ro")
ax3.plot(blankx,blank)
ax4.plot(Xplot,Yplot,"ro")
ax5.plot(blankx,blank)
ax6.plot(Xplot,Yplot,"ro")
ax7.plot(blankx,blanky)
ax8.plot(Xplot,Yplot,"ro")
ax9.plot(blankx,blanky)
if(reset == 1):
for f in fig: #if reset is given instruction, clear all figure and clear the elements in the plotting list
f.clear()
Xplot = []
Yplot = []
Zplot = []
plt.pause(.000001)
I might have found a solution, but not a perfect one, I use math instead of code to rotate the plotting, just multiple it by negative value to flip at x and y axis, I have also added a denoiser function to lower the deviation, here is the code that I use, if anyone had any idea about how to rotate a subplot freely, please enlight me.
import pandas as pd
import serial
import matplotlib.pyplot as plt
ser = serial.Serial("COM5", 115200) # define the serial port that we are communicating to and also the baud rate
plt.style.use('dark_background') #define the black background
plt.ion() # tell pyplot we need live data
fig,[[ax1,ax2,ax3],[ax4,ax5,ax6],[ax7,ax8,ax9]] = plt.subplots(3,3) # plotting a figure with 9 subplot
rx = [0]
ry = [0]
rz = [0]
Xplot2 = []
Xplot4 = []
Xplot6 = []
Xplot8 = []
Zplot2 = []
Zplot4 = []
Zplot6 = []
Zplot8 = []
blankx = []
blankz = []
fig = [ax1,ax2,ax3,ax4,ax5,ax6,ax7,ax8,ax9]
def switch(x):
return x*-1
def denoiser(x):
return (x[-1] +x[-2])/4
while True: #always looping this sequence
while(ser.inWaiting()==0): #if no input from the serial, wait and do nothing
pass
data = ser.readline() #obtain the input from COM 5
data_processed = data.decode('utf-8') #to get rid of the unnecessary string part
data_split = data_processed.split(",") # split the incoming string into a list
rx.append(float(data_split[0])) #to obtain seperate float values for x,y,z
ry.append(float(data_split[1]))
rz.append(float(data_split[2]))
reset = int(data_split[3]) # reset will output 1
draw = int(data_split[4]) # draw will output 2
x = denoiser(rx)
y = denoiser(ry)
z = denoiser(rz)
if(draw == 2):
Xplot8.append(x) #if draw is given instruction, add the x,y,z value into the list to be plot on the graph
Zplot8.append(z)
Xplot2.append(switch(x))
Zplot2.append(switch(z))
Xplot4.append(x)
Zplot4.append(switch(z))
Xplot6.append(switch(x))
Zplot6.append(z)
ax1.plot(blankx,blankz) # subplotting
ax1.axis("off")
ax2.plot(Xplot2,Zplot2,"ro")
ax2.axis("off")
ax3.plot(blankx,blankz)
ax3.axis("off")
ax4.plot(Xplot4,Zplot4,"ro")
ax4.axis("off")
ax5.plot(blankx,blankz)
ax5.axis("off")
ax6.plot(Xplot6,Zplot6,"ro")
ax6.axis("off")
ax7.plot(blankx,blankz)
ax7.axis("off")
ax8.plot(Xplot8,Zplot8,"ro")
ax8.axis("off")
ax9.plot(blankx,blankz)
ax9.axis("off")
if(reset == 1):
for f in fig: #if reset is given instruction, clear all figure and clear the elements in the plotting list
f.clear()
Xplot2 = []
Xplot4 = []
Xplot6 = []
Xplot8 = []
Zplot2 = []
Zplot4 = []
Zplot6 = []
Zplot8 = []
plt.pause(.000001)
Can anyone help me to get a better fit on this curve? The plot shows test data for compression on an elastomer. The blue dots are the data, the red line is the 3rd order fit. If I increase the order of the fit the situation doesn't improve much.
I can supply the test data file if desired (There are 236 rows and 3 columns).
My code is given here:
import csv
import numpy as np
from matplotlib import pyplot as plt
import numpy.polynomial.polynomial as poly
from itertools import islice
with open('neoprene1.csv') as f:
readCSV = csv.reader(f, delimiter=',')
Cs = []
Rs = []
for row in islice(readCSV,1,None):
R = row[1] if row[1] != '' else 0.0 # Strain
C = row[2] if row[2] != '' else 0.0 # Stress
Rs.append(R)
Cs.append(C)
q = np.array([Rs],dtype = float).transpose()
s = np.array([Cs],dtype = float).transpose()
q1 = q[:,0]
s1 = s[:,0]
plt.cla()
z = poly.polyfit(q1,s1,3)
zz = poly.polyval(q,z)
plt.title('Neoprene True Stress-Strain')
plt.xlabel('Strain (%)')
plt.ylabel('Stress (MPa)')
aa = plt.plot(s1,zz,'r-', label = 'Fitting function')
bb = plt.plot(s1,q1,'bo', label = 'Raw data')
I am working with python and trying to implement a heatmap. I have data that is being stored into a 2d array. the array is roughly array[1000][1000] but sometimes the xvalues are greater than 1000.
I am looking to make a heatmap of this array, and I cannot figure out how to do it. I have no idea how to get my data to conform since it is a ragged array not a standard size.
Here is a rough copy of my code I have been attempting
sum_data = None
#count = 0
#for in_file in file_values:
print "summing file: data"
data = []
with open('data', 'r') as in_data:
for line in in_data:
line_no_end= line.rstrip('\n')
list_container = []
list_container = line_no_end.split(",")
#print "eachRow: %s" % list_container
data.append(list_container)
if sum_data == None:
sum_data = data
else:
sum_data = [[int(sum_data[y][x]) + int(data[y][x]) for x in range(len(data[y]))] for y in range(len(data))] #makes 2d array from file
and then I was trying to get it to conform to this heatmap example
heatmap, xedges, yedges = np.histogram2d(x,y,bins = 50)
extent = [xedges[0], xedges[-1], yedges[0], yedges[-1]]
plt.clf()
plt.imshow(heatmap,extent=extent)
plt.show()
plt.savefig('output.png')
I have a set of latitude, longitude points with a data-variable e.g. drive-time from an address. These points have been created by sampling a structured grid and then cutting out a circle.
As such I don't think I can have a matrix of data because some columns will have more zeros/missing than others (the top and bottom parts of the circle) which may confuse the algorithm?
Ideally, I would like to fill in the circle with more points; e.g. at 5 decimal places such that instead of having 51.5454 and 51.5455 I have 51.54540, 51.54541, .... , 51.54550.
My data looks like this:
And I would like to fill in the gaps:
I have tried using:
from scipy.interpolate import RectSphereBivariateSpline
In the following fashion - (test-case), however I am not sure if this is the correct approach in general?
def geointerp(lats, lons, data, grid_size_deg, mesh=False):
deg2rad = np.pi/180.
new_lats = np.linspace(50, 51, 180/grid_size_deg)
new_lons = np.linspace(-1, 1, 360/grid_size_deg)
new_lats, new_lons = np.meshgrid(new_lats*deg2rad, new_lons*deg2rad)
#We need to set up the interpolator object
lut = RectSphereBivariateSpline(lons*deg2rad, lats*deg2rad, data)
new_lats = new_lats.ravel()
new_lons = new_lons.ravel()
data_interp = lut.ev(new_lats,new_lons)
if mesh == True:
data_interp = data_interp.reshape((360/grid_size_deg, 180/grid_size_deg)).T
return new_lats/deg2rad, new_lons/deg2rad, data_interp
# Read in-data
lats_in = []
lons_in = []
data_in = []
with open('interpolation_test.csv') as f:
for x in csv.reader(f):
lats_in.append(float(x[0]))
lons_in.append(float(x[1]))
data_in.append(float(x[2]))
# Interpolate:
lats_in = np.asarray(lats_in)
lons_in = np.asarray(lons_in)
data_in = np.asarray(data_in)
output_list = geointerp(lats_in, lons_in, data_in, 0.01)
# Output
f = open('interpolation_test_out.csv', 'w', newline='')
w = csv.writer(f)
for out in output_list:
w.writerow([out])
f.close()
Not to mention errors such as:
"if not v.size == r.shape[1]:
IndexError: tuple index out of range"