SymPy Plot Resolution - python-3.x

I just recently started learning Python (Platform: Python 3.7) for my Signal processing and communications class and so far it has been great. However, I'm having issues reproducing the same resolutional quality that's achievable with MatPlotLib and linspace (shown in the code below) when using the SymPy library. I was wondering if there is any way of achieving the same resolution?
I understand that SymPy works off the MatPlotLib library, on the back-end, but is limited in how much it actually uses. I tried adding sampling rates to the time-domain limits in the sym.plot call, like you can do with linspace, but that doesn't work. Is there anyway to call the linspace function prior to plotting, to improve the plot's resolution, or even without using linspace?
Step 1 shows the necessary plots, using MatPlotLib. Step 2 shows the code I'm trying to develop to produce the same results, but the quality of the waveform is nowhere near the same as Step 1.
import sympy as sym
import matplotlib.pyplot as plt
import numpy as np
# Step 1 & Section Identifiers
fc, fm = 10**9,10**6
wc, wm, Ac, Am = 2*np.pi*fc, 2*np.pi*fm, 8, 2
# Carrier Signal Plot
t = np.linspace(0, 5/fc, 500)
ct = Ac*np.cos(wc*t)
plt.xlabel('Time')
plt.ylabel('c(t)')
plt.plot(t,ct)
plt.show()
# Step 2 & Section Identifiers
t = sym.Symbol('t')
fc, fm = 10**7, 10**4
wc, wm = 2*sym.pi*fc, 2*sym.pi*fm
# Carrier Plot
ct = Ac*sym.cos(wc*t)
sym.plot(ct, (t, 0, 5/fc), ylabel = "C(t)")
Edit
I Found that I could turn off adaptive sampling and manually specifying the number of data points, to smooth out the signal. I have added the edited line(s) necessary to show this!
sym.plot(ct, (t, 0, 5/fc), ylabel = "C(t)", adaptive = False, nb_of_points = 500)

Related

How to produce a polar contour plot that reads data from an excel spreadsheet?

I am currently doing my internship program in a company and my educational background is actually Petroleum Geoscience, nothing related to programming. So I apologize for any mistakes that I have made or I am about to make.
I was tasked by my supervisor to produce a polar contour plot just like the example below. The example below was generated from the OriginPro (Trial), after the trial period expires I couldn't use the commercial software anymore to produce polar contour plots so I really need help in producing the exactly the same plot in python with different set of data in the future.
The data imported for this plot are from an excel spreadsheet, there is no problem in importing data from the spreadsheet and plot heat map and contour map, the problem only arises when I attempted to produce a polar contour plot from the data given. From what I read, it is because for heat map and contour map it is projected on Cartesian plane which makes it pretty straight forward, but for polar plots you need a certain form of calculation to change from Cartesian coordination to polar coordination? please correct me if I'm wrong.
This is what happened when i tried in python, it should be looking like the example ive given above
]2
and this is the script that I used for the failed plotting
import numpy as np
import matplotlib.pyplot as plt
x = df_ODD.loc[:, 'Azimuth'].values.reshape(19,74)
y = df_ODD.loc[:, 'Inclination'].values.reshape(19,74)
z = df_ODD.loc[:, 'Values'].values.reshape(19,74)
f, ax = plt.subplots(subplot_kw=dict(projection='polar'))
plt.contour(x,y,z)
ax.set_theta_zero_location("N")
ax.set_theta_direction(-1)
cb = fig.colorbar(cax)
cax = ax.contourf(theta, r, values, 30)
cb.set_label("Normalized deviatoric stress")
plt.show()
Below is the form of data imported from the excel spreadsheet if you are wondering, only the columns labelled as "X" , "Y" and "Z", ignore the column labelled as "β, Azimuth". The rows of data stretches down until 1400++ rows.
]3
I really need help in solving this problem, I hope any of you could give me a hand. Thanks
Below is the plot i get after changing the degrees to radians.
and here is the script, i added the mathematical function of converting degrees to radians
import numpy as np
import matplotlib.pyplot as plt
import math
x = df_ODD.loc[:, 'Azimuth'].values.reshape(19,74)
y = df_ODD.loc[:, 'Inclination'].values.reshape(19,74)
z = df_ODD.loc[:, 'Values'].values.reshape(19,74)
xi = x * math.pi/180
yi = y * math.pi/180
zi = z * math.pi/180
f, ax = plt.subplots(subplot_kw=dict(projection='polar'))
plt.contour(xi,yi,zi) # choose 20 contour levels, just to show how good its interpolation is
#ax[1].plot(x,y, 'ko ')
ax.set_theta_zero_location("N")
ax.set_theta_direction(-1)
cb = fig.colorbar(cax)
cb.set_label("Normalized deviatoric stress")
#plt.savefig('attempt polar contour.png')
plt.show()

FFT on MPU6050 output signal

I want to perform FFT on data array that I have extracted from MPU6050 sensor connected to Arduino UNO using Python
Please find the data sample below
0.13,0.04,1.03
0.14,0.01,1.02
0.15,-0.04,1.05
0.16,0.02,1.05
0.14,0.01,1.02
0.16,-0.03,1.04
0.15,-0.00,1.04
0.14,0.03,1.02
0.14,0.01,1.03
0.17,0.02,1.05
0.15,0.03,1.03
0.14,0.00,1.02
0.17,-0.02,1.05
0.16,0.01,1.04
0.14,0.02,1.01
0.15,0.00,1.03
0.16,0.03,1.05
0.11,0.03,1.01
0.15,-0.01,1.03
0.16,0.01,1.05
0.14,0.02,1.03
0.13,0.01,1.02
0.15,0.02,1.05
0.13,0.00,1.03
0.08,0.01,1.03
0.09,-0.01,1.03
0.09,-0.02,1.03
0.07,0.01,1.03
0.06,0.00,1.05
0.04,0.00,1.04
0.01,0.01,1.02
0.03,-0.05,1.02
-0.03,-0.05,1.03
-0.05,-0.02,1.02
I have taken 1st column (X axis) and saved in an array
Reference:https://hackaday.io/project/12109-open-source-fft-spectrum-analyzer/details
from this i took a part of FFT and the code is as below
from scipy.signal import filtfilt, iirfilter, butter, lfilter
from scipy import fftpack, arange
import numpy as np
import string
import matplotlib.pyplot as plt
sample_rate = 0.2
accx_list_MPU=[]
outputfile1='C:/Users/Meena/Desktop/SensorData.txt'
def fftfunction(array):
n=len(array)
print('The length is....',n)
k=arange(n)
fs=sample_rate/1.0
T=n/fs
freq=k/T
freq=freq[range(n//2)]
Y = fftpack.fft(array)/n
Y = Y[range(n//2)]
pyl.plot(freq, abs(Y))
pyl.grid()
ply.show()
with open(outputfile1) as f:
string1=f.readlines()
N1=len(string1)
for i in range (10,N1):
if (i%2==0):
new_list=string1[i].split(',')
l=len(new_list)
if (l==3):
accx_list_MPU.append(float(new_list[0]))
fftfunction(accx_list_MPU)
I have got the output of FFT as shown FFToutput
I do not understand if the graph is correct.. This is the first time im working with FFT and how do we relate it to data
This is what i got after the changes suggested:FFTnew
Here's a little rework of your fftfunction:
def fftfunction(array):
N = len(array)
amp_spec = abs(fftpack.fft(array)) / N
freq = np.linspace(0, 1, num=N, endpoint=False)
plt.plot(freq, amp_spec, "o-", markerfacecolor="none")
plt.xlim(0, 0.6) # easy way to hide datapoints
plt.margins(0.05, 0.05)
plt.xlabel("Frequency $f/f_{sample}$")
plt.ylabel("Amplitude spectrum")
plt.minorticks_on()
plt.grid(True, which="both")
fftfunction(X)
Specifically it removes the fs=sample_rate/1.0 part - shouldn't that be the inverse?
The plot then basically tells you how strong which frequency (relative to the sample frequency) was. Looking at your image, at f=0 you have your signal offset or mean value, which is around 0.12. For the rest of it, there's not much going on, no peaks whatsoever that indicate a certain frequency being overly present in the measurement data.

Using python and networkx to find the probability density function

I'm struggling to draw a power law graph for Facebook Data that I found online. I'm using Networkx and I've found how to draw a Degree Histogram and a degree rank. The problem that I'm having is I want the y axis to be a probability so I'm assuming I need to sum up each y value and divide by the total number of nodes? Can anyone please help me do this? Once I've got this I'd like to draw a log-log graph to see if I can obtain a straight line. I'd really appreciate it if anyone could help! Here's my code:
import collections
import networkx as nx
import matplotlib.pyplot as plt
from networkx.algorithms import community
import math
import pylab as plt
g = nx.read_edgelist("/Users/Michael/Desktop/anaconda3/facebook_combined.txt","r")
nx.info(g)
degree_sequence = sorted([d for n, d in g.degree()], reverse=True)
degreeCount = collections.Counter(degree_sequence)
deg, cnt = zip(*degreeCount.items())
fig, ax = plt.subplots()
plt.bar(deg, cnt, width=0.80, color='b')
plt.title("Degree Histogram for Facebook Data")
plt.ylabel("Count")
plt.xlabel("Degree")
ax.set_xticks([d + 0.4 for d in deg])
ax.set_xticklabels(deg)
plt.show()
plt.loglog(degree_sequence, 'b-', marker='o')
plt.title("Degree rank plot")
plt.ylabel("Degree")
plt.xlabel("Rank")
plt.show()
You seem to be on the right tracks, but some simplifications will likely help you. The code below uses only 2 libraries.
Without access your graph, we can use some graph generators instead. I've chosen 2 qualitatively different types here, and deliberately chosen different sizes so that the normalization of the histogram is needed.
import networkx as nx
import matplotlib.pyplot as plt
g1 = nx.scale_free_graph(1000, )
g2 = nx.watts_strogatz_graph(2000, 6, p=0.8)
# we don't need to sort the values since the histogram will handle it for us
deg_g1 = nx.degree(g1).values()
deg_g2 = nx.degree(g2).values()
# there are smarter ways to choose bin locations, but since
# degrees must be discrete, we can be lazy...
max_degree = max(deg_g1 + deg_g2)
# plot different styles to see both
fig = plt.figure()
ax = fig.add_subplot(111)
ax.hist(deg_g1, bins=xrange(0, max_degree), density=True, histtype='bar', rwidth=0.8)
ax.hist(deg_g2, bins=xrange(0, max_degree), density=True, histtype='step', lw=3)
# setup the axes to be log/log scaled
ax.set_yscale('log')
ax.set_xscale('log')
ax.set_xlabel('degree')
ax.set_ylabel('relative density')
ax.legend()
plt.show()
This produces an output plot like this (both g1,g2 are randomised so won't be identical):
Here we can see that g1 has an approximately straight line decay in the degree distribution -- as expected for scale-free distributions on log-log axes. Conversely, g2 does not have a scale-free degree distribution.
To say anything more formal, you could look at the toolboxes from Aaron Clauset: http://tuvalu.santafe.edu/~aaronc/powerlaws/ which implement model fitting and statistical testing of power-law distributions.

Having trouble with multiple figures on pyplot

I am currently going through the Kaggle Titanic Machine Learning thing and using http://nbviewer.jupyter.org/github/donnemartin/data-science-ipython-notebooks/blob/master/kaggle/titanic.ipynb to figure it out as I am a relative beginner to Python. I thought I understood what the first few steps were doing and I am trying to recreate an earlier step by making a figure with multiple plots on it. I can't seem to get the plots to actually show up.
Here is my code:
`
import pandas as pd
import numpy as np
import pylab as plt
train=pd.read_csv("train.csv")
#Set the global default size of matplotlib figures
plt.rc('figure', figsize=(10, 5))
#Size of matplotlib figures that contain subplots
figsize_with_subplots = (10, 10)
# Size of matplotlib histogram bins
bin_size = 10
females_df = train[train['Sex']== 'female']
print("females_df", females_df)
females_xt = pd.crosstab(females_df['Pclass'],train['Survived'])
females_xt_pct = females_xt.div(females_xt.sum(1).astype(float), axis = 0)
males = train[train['Sex'] == 'male']
males_xt = pd.crosstab(males['Pclass'], train['Survived'])
males_xt_pct= males_xt.div(males_xt.sum(1).astype(float), axis = 0)
plt.figure(5)
plt.subplot(221)
females_xt_pct.plot(kind='bar', title='Female Survival Rate by Pclass')
plt.xlabel('Passenger Class')
plt.ylabel('Survival Rate')
plt.subplot(222)
males_xt_pct.plot(kind='bar', title= 'Male Survival Rate by Pclass')
plt.xlabel('Passenger Class')
plt.ylabel('Survival Rate')
`
And this is displaying two blank plots separately (one in the 221 location, and then next plot on a new figure in the 222 location) and then another plot with males that actually works at the end. What am I doing wrong here?
In order to plot the pandas plot to apreviously created subplot, you may use the ax argument of the pandas plotting function.
ax=plt.subplot(..)
df.plot(..., ax=ax)
So in this case the code may look like
plt.figure(5)
ax=plt.subplot(221)
females_xt_pct.plot(kind='bar', title='Female Survival Rate by Pclass',ax=ax)
ax2=plt.subplot(222)
males_xt_pct.plot(kind='bar', title= 'Male Survival Rate by Pclass',ax=ax2)

Matplotlib: personalize imshow axis

I have the results of a (H,ranges) = numpy.histogram2d() computation and I'm trying to plot it.
Given H I can easily put it into plt.imshow(H) to get the corresponding image. (see http://matplotlib.org/api/pyplot_api.html#matplotlib.pyplot.imshow )
My problem is that the axis of the produced image are the "cell counting" of H and are completely unrelated to the values of ranges.
I know I can use the keyword extent (as pointed in: Change values on matplotlib imshow() graph axis ). But this solution does not work for me: my values on range are not growing linearly (actually they are going exponentially)
My question is: How can I put the value of range in plt.imshow()? Or at least, or can I manually set the label values of the plt.imshow resulting object?
Editing the extent is not a good solution.
You can just change the tick labels to something more appropriate for your data.
For example, here we'll set every 5th pixel to an exponential function:
import numpy as np
import matplotlib.pyplot as plt
im = np.random.rand(21,21)
fig,(ax1,ax2) = plt.subplots(1,2)
ax1.imshow(im)
ax2.imshow(im)
# Where we want the ticks, in pixel locations
ticks = np.linspace(0,20,5)
# What those pixel locations correspond to in data coordinates.
# Also set the float format here
ticklabels = ["{:6.2f}".format(i) for i in np.exp(ticks/5)]
ax2.set_xticks(ticks)
ax2.set_xticklabels(ticklabels)
ax2.set_yticks(ticks)
ax2.set_yticklabels(ticklabels)
plt.show()
Expanding a bit on #thomas answer
import numpy as np
import matplotlib.pyplot as plt
import matplotlib.image as mi
im = np.random.rand(20, 20)
ticks = np.exp(np.linspace(0, 10, 20))
fig, ax = plt.subplots()
ax.pcolor(ticks, ticks, im, cmap='viridis')
ax.set_yscale('log')
ax.set_xscale('log')
ax.set_xlim([1, np.exp(10)])
ax.set_ylim([1, np.exp(10)])
By letting mpl take care of the non-linear mapping you can now accurately over-plot other artists. There is a performance hit for this (as pcolor is more expensive to draw than AxesImage), but getting accurate ticks is worth it.
imshow is for displaying images, so it does not support x and y bins.
You could either use pcolor instead,
H,xedges,yedges = np.histogram2d()
plt.pcolor(xedges,yedges,H)
or use plt.hist2d which directly plots your histogram.

Resources