Drawing a graph using matplotlib - python

I have a text file that looks the following way:
14:49:15
0.00152897834778
14:49:22
0.00193500518799
14:49:29
0.00154614448547
14:49:36
0.0024299621582
14:49:43
0.00161910057068
14:49:50
0.00165987014771
14:49:57
0.00150108337402
I want to create a graph using the plot() method in which i wish every odd line from the text file to be a coordinate on the x axis and every non-odd line to be a y-axis coordinate to it's respective x(the line before the non-odd)
In this particular case 14:49:15 would be the first x and 0.00152897834778 the first y

You could convert the datetimes to numeric and plot them
import numpy as np
import matplotlib.pyplot as plt
import matplotlib
import datetime
string = """14:49:15
0.00152897834778
14:49:22
0.00193500518799
14:49:29
0.00154614448547
14:49:36
0.0024299621582
14:49:43
0.00161910057068
14:49:50
0.00165987014771"""
x = string.split('\n')[::2]
x = matplotlib.dates.date2num([datetime.datetime.strptime(xi, '%H:%M:%S') for xi in x])
y = np.array(string.split('\n')[1::2], dtype=float)
plt.plot(x, y)

You may split the input by linebreaks .split("\n") and convert every second one to a datetime object. Then using plt.plot_date() gives you a plot showing the times.
import datetime
import numpy as np
import matplotlib.pyplot as plt
u = u"""14:49:15
0.00152897834778
14:49:22
0.00193500518799
14:49:29
0.00154614448547
14:49:36
0.0024299621582
14:49:43
0.00161910057068
14:49:50
0.00165987014771"""
# split the string by linebreaks
l = u.split("\n")
# take every second substring and convert it to datetime
x = [datetime.datetime.strptime(i, "%H:%M:%S") for i in l[::2] ]
# take every second substring starting at the second one
y = l[1::2]
plt.plot_date(x,y)
plt.show()

Related

pandas.read_csv() returns strings from columns instead numbers

I am trying to find linear regression plot for the data provided
import pandas
from pandas import DataFrame
import matplotlib.pyplot
data = pandas.read_csv('cost_revenue_clean.csv')
data.describe()
X = DataFrame(data,columns=['production_budget_usd'])
y = DataFrame(data,columns=['worldwide_gross_usd'])
when I try to plot it
matplotlib.pyplot.scatter(X,y)
matplotlib.pyplot.show()
the plot was completely empty
and when I printed the type of X
for element in X:
print(type(element))
it shows the type is string.. Where am I standing wrong???
No need to make new DataFrames for X and y. Try astype(float) if you want them as numeric:
X = data['production_budget_usd'].astype(float)
y = data['worldwide_gross_usd'].astype(float)

Only apply changes to table inside unknown boundaries (walls)

I want my loop to only change the table cell from 0 to 5 inside the "walls".
The "walls" are user defined and could be of any shape, based on coordinates.
The plot is only for visualization.
import matplotlib.pyplot as plt
import pandas as pd
wallPointsX = [5,5,30,30,55,55,5]
wallPointsY = [5,30,30,55,55,5,5]
df = pd.DataFrame(0, index=range(60), columns=range(60))
for x in range(0, 60):
for y in range(0, 60):
df[x][y] = 5 #Should only apply inside "walls"
plt.plot(wallPointsX, wallPointsY)
plt.pcolor(df)
plt.show()
Result plot
Ok, took me some time but it was fun doing it. The idea here is to first create a continuous path out of the coordinates which define the walls. Next, create an object Path. Now you loop through each point in the DataFrame and then look if the created Path contains that (x,y) point using contains_point. I additionally had to use the condition x==55 and (5<y<=55) in the if statement so as to include the column adjacent to the
right most wall.
import matplotlib.path as mplPath
import numpy as np
wallPointsX = [5,5,30,30,55,55,5]
wallPointsY = [5,30,30,55,55,5,5]
# Create a continuous path across the wall coordinates
path = np.array([list(i) for i in zip(wallPointsX, wallPointsY)])
Path = mplPath.Path(path)
df = pd.DataFrame(0, index=range(60), columns=range(60))
for x in range(0, 60):
for y in range(0, 60):
if Path.contains_point((x,y)) or (x==55 and (5<y<=55)):
df[x-1][y-1] = 5 #Should only apply inside "walls"
plt.plot(wallPointsX, wallPointsY)
plt.pcolor(df)
Output

How to use matplotlib to plot a function graph if I have 2 prepared np.arrays of points?

I provide a python-code which solves Gauss equations and plots a function graph. I have a problem in plotting my function. When I try to plot a function graph for example - "2sin(2πx)" I see lines which connect point and it isn't that i would see.
import numpy as np
import math
import random
import matplotlib.pyplot as plt
import pylab
from matplotlib import mlab
print 'case1=2sin(2πx)'
print 'case2=cos(2πx)'
print 'case3=5x^3 + x^2 + 5'
Your_function=raw_input("Enter your choise of your function: ")
def Choising_of_function(x, Your_function):
if Your_function=='case1':
return 2*math.sin(2*x*math.pi)
elif Your_function=='case2':
return math.cos(2*x*math.pi)
elif Your_function=='case3':
return 5*x**3 + x**2 + 5
Dimension_of_pol=int(raw_input("Enter your degree of polynom: "))
Points=int(raw_input("Enter number of points: "))# I just need only limited numbers of points to plot a function graph
Interval=int(raw_input("Enter interval of your points: "))
dx=float(raw_input("Enter interval your dx: "))
X_val=[]
Y_val=[]
for i in range(Points):# First, i generate my values of x
x = random.uniform(-Interval, Interval)
X_val.append(x)
for x in X_val:
y=Choising_of_function(x, Your_function)
Y_val.append(y)
print X_val, Y_val
Arr_Xo=[[x**i for i in range(Dimension_of_pol)] for x in X_val]
print Arr_Xo
D_mod={}
D={}
for y, x in zip(Y_val, X_val):
D_mod[y]=x
Arr_X_o=np.array(Arr_Xo)
print Arr_X_o
Arr_X=np.array(X_val) #My array of x-values
print Arr_X
Arr_Y=np.array(Y_val) #My array of y-values
print Arr_Y
m = np.linalg.lstsq(Arr_X_o, Arr_Y)[0]
print m
pylab.plot(Arr_X, Arr_Y, 'go')
line=plt.plot(Arr_X, Arr_Y)
line.show()
How i can plot my function without using frange.
My array of x:
[-15.9836388 13.78848867 -3.39805316 12.04429943 -12.34344464
-19.66512508 6.8480724 -5.58674018 7.59985149 11.46357551
-4.96507337 -2.40178658 -1.71320151 -12.87164233 -3.26385184
-7.44683254 5.52525074 -9.16879057 3.70939966 -4.80486815
-10.35409227 6.72283255 2.00436008 8.68484529 -17.81750773]
My array of y:
[ 0.20523902 -1.941802 -1.19527441 0.54952271 -1.66506802 1.72228361
-1.63215286 1.03684409 -1.17406016 0.45373838 0.43538662 -1.15733373
1.94677887 1.44373207 -1.99242991 -0.65576448 -0.31598064 -1.74524107
-1.9352764 1.88232214 -1.58727561 -1.97093284 0.05478352 -1.83473627
1.8227666 ]
I paste all of it in :
line=plt.plot(Arr_X, Arr_Y)
plt.show()
And my function graph doesnt looks like 2*sin(2px)
The problem is that your x axis values are not in order, therefore when you plot them your points will not be joined to the next point on the x axis, giving a graph that looks like the one in the question. A test of this will be to use plt.scatter instead of plt.plot:
This shows that the points you are generating are in the correct shape as seen in the left most image, however you are just generating the x values slightly wrong.
In order to get a nice looking graph you need to change the way you generate the x values. This can be done using np.linspace, the documentation can be found here.
# for i in range(Points): # First, i generate my values of x
# x = random.uniform(-Interval, Interval)
# X_val.append(x)
# replace the above 3 lines with the one below
X_val = np.linspace(-Interval,Interval,Points)
In addition, there is no need to assign plt.plot to a variable, therefore the last 3 lines of your code should be replaced with:
# pylab.plot(Arr_X, Arr_Y, 'go')
# line=plt.plot(Arr_X, Arr_Y)
# line.show()
# replace the above 3 lines with the one below
pylab.plot(Arr_X, Arr_Y)
plt.show()
This produces the following graph:
I do not know what the reason is to
pylab.plot(Arr_X, Arr_Y, 'go')
as well as
line=plt.plot(Arr_X, Arr_Y)
Why do you need pylab to plot instead of just using pyplot?
Your
line.show() in line 63 gives me an attribute error
"list" object has no attribute "show"
only plt has show(), if you see in print dir(plt)
As I am to lazy to go trough your full code stick to this general plotting example:
import matplotlib.pyplot as plt
figure, axis = plt.subplots(figsize=(7.6, 6.1))
for x in range(0, 500):
axis.plot(x, x*2, 'o-')
plt.show()

Plotting in python using matplotlib?

I have been trying to simulate the first order differential equation using the fourth order Runge-Kutta method, but I am having problems plotting it.
#simulation of ode using 4th order rk method dy/dx=-2y+1.3e^-x,y(0)=5,h=0.01 from sympy import*
import math
import numpy as np
import matplotlib.pyplot as plt
h=0.01;
ti=0;
x=0;
n=0;
y=5;
def f(x,y):
return 1.3*math.exp(-x)-2*y
while x < 10:
k1=f(x,5);
k2=f(x+h/2,y+(h/2)* k1);
k3=f(x+h/2,y+(h/2)* k2);
k4=f(x+h,y+h*k3);
y=y+h/6*(k1+2*(k2+k3)+k4);
x=x+h;
plt.plot(x,y);
I know that the problem is because of updating the x,y values every time the loop runs, but can somebody explain how to plot all the values of (x,y)?
As suggested in the comment, you can create two lists to store x and y values and plot it after the while loop:
import math
import numpy as np
import matplotlib.pyplot as plt
h=0.01;
ti=0;
x=0;
n=0;
y=5;
def f(x,y):
return 1.3*math.exp(-x)-2*y
xs = [x] # <<<
ys = [y] # <<<
while x < 10:
k1=f(x,5);
k2=f(x+h/2,y+(h/2)* k1);
k3=f(x+h/2,y+(h/2)* k2);
k4=f(x+h,y+h*k3);
y=y+h/6*(k1+2*(k2+k3)+k4);
x=x+h;
xs.append(x) # <<<
ys.append(y) # <<<
plt.plot(xs,ys);
Another source for wrong results is the first line in the RK4 loop. Instead of
k1=f(x,5);
use
k1=f(x,y);
since the value of y does not stay constant at the initial value.

Python Importing data with multiple delimiters

In Python, how can I import data that looks like this:
waveform [0]
t0 26/11/2014 10:53:03.639218
delta t 2.000000E-5
time[0] Y[0]
26/11/2014 10:53:03.639218 1.700977E-2
26/11/2014 10:53:03.639238 2.835937E-4
26/11/2014 10:53:03.639258 2.835937E-4
26/11/2014 10:53:03.639278 -8.079492E-3
There are two delimiters, : and white space. I want to get rid of the date 24/11/2014 and delete the semicolons so that the time array looks like 105303.639218, etc. So is there a way to specify two delimiters in the code, or is there a better way to analyse the data?
So far I have got:
import numpy as np
import matplotlib.pyplot as plt
_, time, y = np.loadtxt('data.txt', delimiter=':', skiprows=5)
plt.plot(time,y)
plt.show()
You can do this:
time = '10:34:20.454068'
list_ = time.split(':')
''.join(list_)
# '103420.454068'
for each row.
Maybe it's sort of a roundabout way of doing this, but...
import numpy as np
import matplotlib.pyplot as plt
mydata = np.loadtxt('data.txt', dtype='string', skiprows=5)
time = mydata[:,1]
time = np.array([s.replace(':','') for s in time])
y = np.array(mydata[:,2])
plt.plot(time,y)
plt.show()

Categories