I have an issue as follow, i have coordinates x, y, z and r. Each of point is a Frame. Based on Frames want to generate heat-map with python. What i did till now, i imported the following frames:
-1.52588e-05 -1.52588e-05 8.17212e-06 300
-220.414 -220.305 217.847 79.5859
-220.899 220.54 -219.881 79.1004
219.275 218.495 -221.124 78.8756
-216.911 220.674 218.582 78.848
218.126 -219.362 221.977 78.0233
-222.961 -224.281 -204.107 75.7191
225.267 222.614 221.81 74.7329
parse it as well. From here i know is actually nothing really.
as far as i'm concerned, generating heat-map based on frames.
I don't know how should i do after importing frames.I'm really lost in context.
Could someone give tips or way of doing i.e steps...
thanks
the code below is not work as well
import csv
import seaborn as sns
result = [[]]
with open("data.csv") as csvfile:
reader = csv.reader(csvfile, quoting=csv.QUOTE_NONNUMERIC)
for row in reader:
result.append(row)
print(result)
Try following code:
import pandas as pd
import seaborn as sns
import matplotlib.pyplot as plt
with open('data.txt', 'r') as f:
data = f.read().replace(' ', ' ')
with open('data.txt', 'w') as f:
f.write(data)
df = pd.read_csv('data.txt', sep=' ', header=None)
sns.heatmap(df, annot=True)
plt.show()
Output:
Related
Sorry for all mistakes, English is not my native language. I have a CSV file with pose data in X,Y,Z coordinates. I managed to visualize it as dots using matplotlib.pyplot and pandas, but what I want is direction, so that dots was linked and first and last dots was of a different color. This is code I use for pose visualization:
import pandas as pd
import csv
import matplotlib.pyplot as plt
import numpy as np
str_filename = 'sensor_data.csv'
fh = open(str_filename, encoding="utf-8")
csv_reader = csv.reader(fh, delimiter = ";")
csv_header = next(csv_reader)
fh.close()
df_sig = pd.read_csv(str_filename, delimiter=";", header=None, skiprows=1, names=csv_header)
x = np.array(df_sig["Pose X"])
y = np.array(df_sig["Pose Y"])
plt.plot(x, y, 'd', color = 'black')
plt.title('Pose')
plt.savefig('Pose Visualization.pdf')
Appreciate any help.
I have the following code:
import matplotlib.pyplot as plt
import csv
with open('results simulation.csv', newline='') as f:
reader = csv.reader(f)
data = list(reader)
Generation = data[0]
Points = data[1]
plt.plot(Generation, Points)
plt.title('Points for every Generation')
plt.xlabel('Generation')
plt.ylabel('Points')
plt.show()
And this is the chart that I get:
As you can I get a straight line, even though the values are different 😅
Any ideas?
I'm trying to plot a curve using the data from a csv file using:
import matplotlib.pyplot as plt
from csv import reader
with open('transmission_curve_HST_ACS_HRC.F606W.csv', 'rw') as f:
data = list(reader(f))
wavelength_list = [i[0] for i in data[1::]]
percentage = [i[1] for i in data[1::]]
plt.plot(wavelength_list, percentage)
plt.show()
But all it make is opening a completely blank window and I can't close it unless I close the terminal.
The csv file looks like this:
4565,"0,00003434405472044760"
4566,"0,00004045191689260860"
4567,"0,00004656394357747830"
4568,"0,00005267963655205460"
4569,"0,00005879949856084820"
Do you have any idea why?
You need to modify three things in your code:
Change 'rw' to 'r' when you read from the file
Correct the way you iterate over data
Convert the numbers from the second column to float
import matplotlib.pyplot as plt
from csv import reader
with open('transmission_curve_HST_ACS_HRC.F606W.csv', 'r') as f:
data = list(reader(f))
wavelength_list = [i[0] for i in data]
percentage = [float(str(i[1]).replace(',','.')) for i in data]
plt.plot(wavelength_list, percentage)
plt.show()
Content of the csv file:
4564,"0,00002824029270045730"
4565,"0,00003434405472044760"
4566,"0,00004045191689260860"
4567,"0,00004656394357747830"
4568,"0,00005267963655205460"
4569,"0,00005879949856084820"
I've been with .cvs files to generate Histogram from the data. It has data something like this
102.919 103.36
102.602 103.05
104.106 104.57
108.791 109.26
104.045 104.52
104.324 104.77
105.106 105.57
102.619 103.08
102.124 102.6
Here's the code I have written
# histplot.py
import numpy as np
import matplotlib.pyplot as plt
import csv
with open('datafile.csv', 'rU') as data:
reader = csv.DictReader(data, delimiter=' ', quoting=csv.QUOTE_NONNUMERIC)
for line in reader:
t = float(line)
data.append(t)
reader.close()
# generate the histogram
hist, bin_edges=np.histogram(data, bins=50, range=[80,135])
# generate histogram figure
plt.hist(data, bin_edges)
plt.savefig('chart_file', format="pdf")
plt.show()
Running this code give me an error ValueError: could not convert string to float: '102.919,103.36'
Can someone help me in giving few ideas regarding converting strings to float using csv file.
Thank you in advance.
First of all with open('datafile.csv', 'rU') as data: means that you obtain data as a filehandle to the file. You can use this filehandle as an iterable but you cannot append anything to it.
Second csv.DictReader provides access to the data as a dictionary. In this case here, I would recommend using csv.reader, which gives access to the data as a list.
Third, you cannot convert the whole line, may it be a dictionary or a list, to a float. You can only do that with a single element of the list. (This is where the error comes from.) Conversion to float isn't even necessary, since the reader already takes care of that.
Now, you can simply append the elements line by line to an initially empty list and supply this list to the histogram function.
import numpy as np
import matplotlib.pyplot as plt
import csv
data = [] #create empty list
with open('datafile.csv', 'rU') as f:
reader = csv.reader(f, delimiter=' ', quoting=csv.QUOTE_NONNUMERIC)
for line in reader:
data.extend(line)
# generate the histogram
hist, bin_edges=np.histogram(data, bins=50, range=[80,135])
# generate histogram figure
plt.hist(data, bin_edges)
#plt.savefig('chart_file', format="pdf")
plt.show()
Let me just mention that the whole data reading can be done in a much simpler way, using numpy.loadtxt.
Also, plotting the histogram may be simplified, in case no further data processing needs to take place.
import numpy as np
import matplotlib.pyplot as plt
data = np.loadtxt('datafile.csv').flatten()
plt.hist(data, bins=50, range=[80,135])
plt.show()
I am trying to plot a graph after having imported data from a CSV file and stored it as two separate lists. Is it possible for matplotlib to plot a graph using a list of strings, or is it necessary for the lists two be lists of "int" ?
If not, why is the following code not working?
Error prompted:
invalid literal for int() with base 10: '02_13_2014'
My code:
import csv
import numpy as np
from numpy import genfromtxt
import matplotlib.pyplot as plt
import matplotlib.dates as mdates
with open('pytdef.csv','r') as f:
reader = csv.reader(f)
for row in reader:
print (row)
first_column= ['Date']
second_column = ['Value']
Date_list = []
Value_list = []
with open('pytdef.csv') as f:
reader = csv.reader(f)
for row in reader:
Date_list.append(row[0])
with open('pytdef.csv') as f:
reader = csv.reader(f)
for row in reader:
Value_list.append(row[1])
print (Date_list)
print (Value_list)
Date_list = list(map(int,Date_list))
print (Date_list)
print (Value_list)
fig = plt.figure()
plt.plot_date(x=Date_list, y=Value_list)
plt.show()
I think the problem here is your date. The code here is a lot simpler if you just use pandas
#!/usr/bin/env python
%matplotlib inline
import pandas as pd
import matplotlib.pyplot as plt
from datetime import date
columns = ['dates', 'value']
data = pd.read_csv('/location_of_file/file_name.csv', header=False, names=columns)
I'm assuming your data looks something like this...
then format the date
data['dates'] = [date(int(x.split('_')[2]), int(x.split('_')[0]),
int(x.split('_')[1])) for x in data.dates]
plt.plot(data.dates, data.value);