Labelling multiple points from CSV in basemap - python

I'm having issues labeling all the points plotted with the code below,
each row in the CSV file is name, lat,lon.
Points are plotting just fine.
Everything I've tried to have names be plotted along x,y has borked everything in one way or another.
Anyone have any tips?
with open('/.../geo/ALL.csv') as csvfile:
reader = csv.reader(csvfile,delimiter=',')
for data in reader:
names.append(str(data[0]))
lats.append(float(data[1]))
lons.append(float(data[2]))
x,y = map(lons,lats)
map.plot(x,y,'r*',markersize=0.02,color='yellow',marker='D')

You goal is to plot (lons,lats) points in the xy-plane with a label on each. This is one way to do it:
import csv, matplotlib.pyplot as plt
names, lats, lons = [], [], []
with open('file.csv') as csvfile:
reader = csv.reader(csvfile,delimiter=',')
for data in reader:
names.append(str(data[0]))
lats.append(float(data[1]))
lons.append(float(data[2]))
fig, ax = plt.subplots()
ax.scatter(lats, lons, marker='*', s=400) # play with 's' to control the dot size
pad = " " # play with this to pad the label from the dot
for i, n in enumerate(names):
ax.annotate(pad+str(n), (lats[i], lons[i]))
plt.savefig("scatter.png") # you will find scatter.png file in the same directory
This is the scatter.png I get:
with this file.csv CSV file:
n1,1,8
n4,7,2
n2,3,14
n7,13,4
n5,9,6
n6,11,12
n3,5,10
n8,15,18
n9,17,16

Related

how to plot a single column graph of a CSV file in python?

I am trying to plot a graph with the train data that I have from this website. The train data consists of many column many rows data, but I wanted to plot the graph column by column.
I managed to figure out a working code to only print out a column, however I do not know how to plot graph for that particular column. For my below code, the last two lines are my attempt to try plot the single column graph but it is not working. Can anyone help me on how I can successfully plot the graph of that column?
https://archive.ics.uci.edu/ml/datasets/Parkinson+Speech+Dataset+with++Multiple+Types+of+Sound+Recordings
import csv
import matplotlib.pyplot as plt
with open("C://Users/RichardStone/Pycharm/Projects/train_data.csv", "r") as csv_file:
csv_reader = csv.reader(csv_file, delimiter=',')
for lines in csv_reader:
print(lines[1])
plt.plot(lines[1])
plt.show()
Why not read data into a pandas dataframe and then plot it using matplotlib?
Something like this should work:
import pandas as pd
import matplotlib.pyplot as plt
file_path = "path\to\file"
df = pd.read_csv(file_path)
for column in df.columns:
print(df[column])
plt.figure()
plt.title(column)
plt.plot(df[column])
plt.show()
If you're using matplotlib, you've already got numpy, so you could do something like this:
import csv
import matplotlib.pyplot as plt
import numpy
with open('C://Users/RichardStone/Pycharm/Projects/train_data.csv', 'r') as csvfile:
reader = csv.reader(csvfile)
# convert strings to numbers, collect everything in a list of lists
data_list = [[float(item) for item in row] for row in reader if row]
# convert to numpy array for convenient indexing
data = numpy.array(data_list)
column_index = 1
plt.plot(data[:, column_index])
# or plt.plot(data) to show all columns
# or plt.scatter(data[:, 0], data[:, 1]) for a scatter plot
plt.show()

Y-axis labels aren't showing properly on scatter plot on Python with matplotlib

I'm trying to plot some data using the matlib package on Python. However, when I try to plot a set of data then set the y axis limits, the labels on the y-axis do not show the full range that I'd like. In fact, it only shows values for that are written in the data.
I've tried changing the y-axis limits. I've tried plotting it with another set of data, but the labels don't seem to change. When I plot with the temp Vec that has data from 80-100, it'll show the entire axis range from 0, 100. When I try to plot the relative humidity vector, it only plots data from the range of the available data from around 0 - 40.
with open('April_26.csv') as csvfile:
readCSV = csv.reader(csvfile, delimiter =',')
next(readCSV) #skips the first row which is the headers
#Initialize arrays
timeVec = []
tempVec = []
relHumidityVec = []
#loop through each row
for row in readCSV: #for each row in the CSV file
##Storing specific data
#Note: CSV data is in strings so convert the integers if they are to be
treated as such
time = int(row[0])
temp = int(row[2])
relHum =row[3]
#append to array
timeVec.append(time/1000)
tempVec.append(temp)
relHumidityVec.append(relHum)
#initialize plot
fig, ax = plt.subplots()
#plt.scatter(timeVec, tempVec)
plt.scatter(timeVec, relHumidityVec, s = 4, marker = 'o', c = 'blue' , alpha = 0.4)
plt.scatter(timeVec, tempVec, s = 4, marker = 'o', c = 'red' , alpha = 0.4)
plt.ylim(0, 100)
I expect the plot to plot my two plots (temp vs time, relative humidity vs time) while plotting the full range for time and full range on the y-axis from 0 to 100.
I figured out the problem. It's because from reading the .csv file, all the values are string types. My relative humidity vector was still in string type so I just had to convert to int with the code.
relHum = int(row[3])

How to filter csv with matplotlib using dates on the x-axis and rainfall on the y-axis?

I have a csv file with all two columns one that says 'Date' and the other that has the rainfall amount in inches called 'Rainfall'. I am not sure how to go about this, so far my approach has not been working. I also need to skip the first 5 lines before I enter into the 'Date' and 'Rainfall' column.
Here is the code I have so far:
import matplotlib.pyplot as plt
import csv
x = []
y = []
with open('1541553208_et.csv','r') as csvfile:
plots = csv.reader(csvfile, delimiter=',')
for row in plots:
for i in row:
x.append(row[0])
y.append(row[1])
plt.plot(x,y, label='Loaded from file!')
plt.xlabel('Dates')
plt.ylabel('Evaporation (inches)')
plt.title('Eden_7')
plt.legend()
plt.show()
When I run the code I get the following incorrect results:
I want to have it so that each months rainfall data is clustered into one
Here is an example of what I am going on:
I am trying to get the same effect as the top. How could this be done?
Thank you
You may have a simpler time using the pandas library instead of the csv library.
For instance, pandas allows you to store the csv file directly into a data structure called a dataframe. This will allow you to group on dates or rainfall and plot the data.
import pandas as pd
# rain will be an dataframe instance
rain = pd.read_csv(csvfile)
rain = rain.groupby(rain['rainfall'])
rain.plot(kind='bar')
plt.show()
Play around with it, pandas is very powerful.
You can find the pandas documentation here: https://pandas.pydata.org/pandas-docs/stable/
While this may not be an immediate solution, it may help in the long run.
Using pandas library will be easier as previously mentioned. Following your csv library can you try to run this,
import matplotlib.pyplot as plt
import csv
x = []
y = []
f = open('1541553208_et.csv')
csv_f = csv.reader(f,delimiter=',')
for row in csv_f:
x.append(row[0])
for row in csv_f:
y.append(row[1])
plt.plot(x,y, label='Loaded from file!')
plt.xlabel('Dates')
plt.ylabel('Evaporation (inches)')
plt.title('Eden_7')
plt.legend()
plt.show()

Reading list from CSV and convert to float list

I want to do some math on my data and stuck with it.
I am reading string lists from csv files and can plot them so far.
Now I am trying to convert the string lists to float lists as i want to do some math on the list items and create new lists and plot them.
import numpy as np
import math
import matplotlib.pyplot as plt
with open("testlog.csv") as f:
data = f.read()
data = data.split('\n')
time = [row.split(',')[0] for row in data]
gyro_x_lsb = [row.split(',')[1] for row in data]
gyro_y = [row.split(',')[2] for row in data]
gyro_z = [row.split(',')[3] for row in data]
accel_x = [row.split(',')[4] for row in data]
accel_y = [row.split(',')[5] for row in data]
accel_z = [row.split(',')[6] for row in data]
comp_x = [row.split(',')[7] for row in data]
comp_y = [row.split(',')[8] for row in data]
comp_z = [row.split(',')[9] for row in data]
temp = [row.split(',')[10] for row in data]
gyro_x_lsb = float(gyro_x_lsb)# make floats in a new list
gyro_x_dps = [gyro_x_lsb / (32768*2000) for gyro_x_dps_f in gyro_x_lsb]
fig = plt.figure()
ax1 = fig.add_subplot(211)
ax1.set_title("Gyro X AR [LSB]")
ax1.set_xlabel('Time[ms]')
ax1.set_ylabel('AR [LSB]')
ax1.plot(time,gyro_x_lsb, c='r', label='X')
leg = ax1.legend()
ax2 = fig.add_subplot(212)
ax2.set_title("MPU9250_test_Accel")
ax2.set_xlabel('Time[ms]')
ax2.set_ylabel('Acceleration')
ax2.plot(time,accel_x, c='r', label='Accel_X')
leg = ax2.legend()
plt.show()
I am trying to calculate every item in "gyro_x_lsb"/32768*2000. But it will not work.
I have tried map also already.
Thanks in advance...
TMP36
BTW: I use Anaconda 3(2.5.0) with Python 3.5
I am new to Python and to this forum.
List comprehensions will do a lot for you.
Like:
gyro_x_lsb_sum = sum(float(item) for item in gyro_x_lsb)
Here I am using a generator expression which is the same syntax as a list comprehension, but better performance in this case.
Hope it helps a bit.
PS. Consider using the Python csv module to read CSV files.
Thanks for the hints with the CSV module. This read my data now as tuple. This was also new to me, and I found a possibility to make some math on my data. Here is the code now that works for me, maybe not perfect, but i am happy for now.
import csv
import matplotlib.pyplot as plt
with open("testlog.csv") as data:
#reader = csv.reader(data) #read columns as strings
reader = csv.reader(data, quoting=csv.QUOTE_NONNUMERIC) #read columns as numbers
time, gyro_x, gyro_y, gyro_z, accel_x, accel_y, accel_z, comp_x, comp_y, comp_z, temp = zip(*reader)
#gyro_x_dps = gyro_x
def gyro_x_dps(gyro_x):
return tuple( [e / 65536000 for e in gyro_x])
fig = plt.figure()
ax1 = fig.add_subplot(211)
ax1.set_title("Gyro X AR [LSB]")
ax1.set_xlabel('Time[ms]')
ax1.set_ylabel('AR [LSB]')
ax1.plot(time,gyro_x, c='r', label='X')
leg = ax1.legend()
ax2 = fig.add_subplot(212)
ax2.set_title("Gyro X AR [dps]")
ax2.set_xlabel('Time[ms]')
ax2.set_ylabel('AR [dps]')
ax2.plot(time,gyro_x_dps(gyro_x), c='r', label='X')
leg = ax2.legend()
plt.show()

Python plot dates using matplotlib

I'm very beginner at Python and matplotlib but trying to learn! I would like to use matplotlib to plot some simple data from a CSV containing dates with a frequency. The X axis containing dates and Y containing the frequency. Example data from CSV:
2011/12/15,5
2011/12/11,4
2011/12/19,2
I checked the "matplotlib.sf.net/examples" out but appears all the test data is downloaded from a http get. I would really appreciate if someone could guide me with some example code of how to read in (presumably using CSV reader) and display data in chart.
Thank you!!
Maybe you look for something like:
import csv
import datetime as dt
import matplotlib.pyplot as plt
arch = 'C:\\Python26\\programas\\test.csv'
data = csv.reader(open(arch))
data = [(dt.datetime.strptime(item, "%Y/%m/%d"), float(value)) for item, value in data]
data.sort()
[x, y] = zip(*data)
fig = plt.figure()
ax = fig.add_subplot(111)
ax.plot(x, y)
ax.grid(True)
fig.autofmt_xdate()
plt.show()
I've tried to keep my code as simple as possible and this is by no means elegant, but here you go:
import csv
import matplotlib.pyplot as plt
### Making test CSV file ###
data = [['2011/12/15,5'],['2011/12/11,4'],['2011/12/19,2'],['2011/12/16,3'],['2011/12/20,8'],['2011/12/14,4'],['2011/12/10,10'],['2011/12/9,7']]
with open('test.csv', 'wb') as f:
writer = csv.writer(f)
for i in data:
writer.writerow(i)
### Extract data from CSV ###
with open('test.csv', 'rb') as n:
reader = csv.reader(n)
dates = []
freq = []
for row in reader:
values = row[0].split(',')
dates.append(values[0])
freq.append(values[1])
### Do plot ###
false_x = [x for x in range(len(dates))]
plt.plot(false_x,freq, 'o-')
plt.xticks(range(len(dates)), (dates), rotation=45)
# plt.axis([xmin, xmax, ymin, ymax]) - sets axes limits on graph
plt.axis([-1, 8, 0, 11])
plt.show()
This makes:

Categories

Resources