I have the following exponential distribution, generated with the following code:
import numpy as np
import pandas as pd
import matplotlib.pyplot as plt
import random
np.random.seed(1)
tags_ratio = np.random.exponential(1/25, 1000)
plt.hist(tags_ratio, range=(0, 1), bins=100)
plt.show()
I'm trying to transform my data, resides in tags_ratio into normal distribution, but with no success.
Tried with the log function and square functions. it given decent results. But I'm interesting in more ideas. Maybe more sophisticated.
You can try to see if this helps:
from scipy.stats import boxcox
tags_ratio = boxcox(tags_ratio, 0.3)
plt.hist(tags_ratio)
plt.show()
result:
for more explanations and theory about Box-Cox click here.
Related
I want to get the parabolic signal using scipy in python but i am getting error
AttributeError: module 'scipy.signal' has no attribute 'special'
#Unit Parabolic Signal
from scipy import signal
import numpy as np
import matplotlib.pyplot as plt
plt.title('Unit Parabolic Signal')
plt.xlabel('Sample')
plt.ylabel('Amplitude')
t = signal.special.pbdv(0, 10)
plt.stem(t)
plt.show()
**this is the code for python i want to use only an scipy library how can i get signal like this shown in below **
I think you need to import special specifically. You don't seem to have imported it.
For example, if I run:
import scipy as sp
from scipy import signal
from scipy import special
import numpy as np
import matplotlib.pyplot as plt
plt.title('Unit Parabolic Signal')
plt.xlabel('Sample')
plt.ylabel('Amplitude')
t = special.pbdv(0, 10)
plt.stem(t)
plt.show()
I do, in fact, get a plot produced; though, not exactly the one you showed.
I am experimenting with Fourier transformations and the built-in NumPy.fft library. I was trying to see the difference between computing just fft2 of an image and fftshift on fft2 of an image. But for some reason, I am not getting the results that I was expecting. I have tried changing images as well but regardless of what I use, I get the same results as below. If someone could help me out here, it would be awesome. This is the code I used:
import numpy as np
import cv2
import matplotlib.pyplot as plt
from scipy import ndimage, fftpack
light = cv2.imread("go_light.jpeg")
dark = cv2.imread("go_dark.jpeg")
g_img = cv2.cvtColor(dark, cv2.COLOR_BGR2GRAY)
di = (np.abs((np.fft.fft2(g_img))))
dm = np.abs(np.fft.fftshift(np.fft.fft2(g_img)))
plt.figure(figsize=(6.4*5, 4.8*5), constrained_layout=False)
plt.subplot(151), plt.imshow(di, "gray"), plt.title("fft");
plt.subplot(152), plt.imshow(dm, "gray"), plt.title("fftshift");
plt.show()
di and dm are floating point values. Matplotlib can't do that. First, try di.astype(np.int8). However, many of the values are out of range. You may need to scale the array.
For the life of me, I can't get to render plots in RMarkdown using Python. The plots display in my Python Editor, but not when I switch to RStudio trying to build a PDF/HTML report. I've been all over the internet, everyone has an answer, but none has the solution to my issue. Here's the error message I get the attached error below:
Below I made up some data to show what I'm trying to achieve. Once the application hits the seaborn line it produces the attached error message in RStudio
knitr::opts_chunk$set(echo = TRUE)
library(reticulate)
Importing Required Packages
import pandas as pd
import numpy as np
from scipy.stats import uniform # for training-and-test split
import statsmodels.api as sm # statistical models (including regression)
import statsmodels.formula.api as smf # R-like model specification
import matplotlib.pyplot as plt # 2D plotting
import seaborn as sns
x = np.random.normal(0, 1, 20)
y = np.random.normal(0, 2, 20)
sns.scatterplot(x, y)
I found the answer here
All I needed was to add these two lines in the R setup chunk below the library(reticulate) call
matplotlib <- import("matplotlib")
matplotlib$use("Agg", force = TRUE)
Original question
I am new to Python and I am learning matplotlib. I am following the video tutorial recommended in the official User Manual of matplotlib: 'Plotting with matplotlib' by Mike Muller. The instructor does not show how he imports matplotlib but proceeds instantly with commands such as plot(x, linear, x, square), where x a sequence he has defined.
I tried to run
import matplotlib
from matplotlib import pyplot as plt
import numpy as np
import pandas as pd
x = np.arange(100)
lines = plot(x, 'linear', 'g:+', x, 'square','r-o')
and got the error
NameError: name 'plot' is not defined
Updated question after solving the import issue
When I try to replicate the examples shown I get an error. Here is the code:
from matplotlib.pyplot import *
import numpy as np
import pandas as pd
x = np.arange(100)
lines = plot(x, 'linear', 'g:+', x, 'square','r-o')
ValueError: x and y must have same first dimension
Since I am simply repeating the commands shown in the tutorial I can not understand what I am doing wrong.
Your advice will be appreciated.
Short answer
You need to prefix all plotting calls with plt. like
lines = plt.plot(x, 'linear', 'g:+', x, 'square','r-o')
Longer answer
In Python functions that are not "builtin", i.e. always present, must be imported from modules.
In this case the line
from matplotlib import pyplot as plt
is the same as
import matplotlib.pyplot as plt
and means that you are importing the pyplot module of matplotlib into your namespace under the shorter name plt.
The pyplot module is where the plot(), scatter(), and other commands live.
If you don't want to write plt. before every plot call you could instead do
from matplotlib.pyplot import *
which will import all functions (symbols) into the global namespace, and you can now use your original line:
lines = plot(x, 'linear', 'g:+', x, 'square','r-o')
Edit: Problem with the plot() call
Your call to plot() is wrong, and the ValueError is telling you so.
You are trying to plot the string 'linear' (6 elements) against x which has 100 elements. Since they don't match, plot() tells you so.
My guess: linear and square should be expressions like
linear = x
square = x**2
Hello I created a module in the ipython text editor and then called it in the ipython notebook and it freezes when I call show() from matplotlib. I am running on a surface pro windows 10. I have checked for answers and I found that using import matplotlib as mpl
mpl.use('TkAgg')
in the beginning of my module worked the first time and then every time I tried to run after it has killed my kernel. Here is my module which is called Take_Home.py:
import numpy as np
import matplotlib as mpl
import pdb
#mpl.use('TkAgg')
import matplotlib.pyplot as plt
import matplotlib.mlab as ml
def statistics(array,bin_size):
#calculate mean and standard deviation of array
mean=np.mean(array)
std=np.std(array)
#get the gaussian fit of the data
gaussian=ml.normpdf(array,mean,std)
#plot the normalized histogram
plt.hist(array,bins=np.arange(np.min(array), np.max(array) + bin_size, bin_size),normed=True)
#plot the gaussian function over the histogram
plt.plot(array,gaussian,'.k')
#this attempts to show the plot but doesn't work
plt.show()
#saves the plot to a PDF
plt.savefig('Statistics.pdf')
and here is the ipython notebook that calls the module:
import Take_Home as th
import numpy as np
import matplotlib.pyplot as plt
#initialize an array of 1000 elements and set them all to 1
rd=np.arange(0,1000)
rd[:]=1
#change the array to have all random numbers with a std of 2 and a mean of 1
rd=rd*(2*np.random.randn(1,1000)+1)
#transpose it because the error message said so
rd=np.transpose(rd)
#declare bin size
binsize=0.2
#use method inside module
th.statistics(rd,binsize)
I just need to show the plot and I don't know why it's working any help would be much appreciated thank you!