Thứ Tư, 13 tháng 11, 2019

Easy Python

(1) Matplotlib & ggplot (Link: Github)
#Display Matplotlib styles
%matplotlib inline
plt.style.available

#Plot ggplot style
import numpy as np
import matplotlib.pyplot as plt
def frecuencias(f1=10.0, f2=100.0):
    max_time = 0.5
    times = np.linspace(0, max_time, 1000)
    signal = np.sin(2 * np.pi * f1 * times) + np.sin(2 * np.pi * f2 * times)
    with plt.style.context("ggplot"):
        plt.plot(signal, label="Señal")
        plt.xlabel("Tiempo ($t$)")
        plt.title("Dos frecuencias")
        plt.legend()

frecuencias()

#Display Ipywidgets
from ipywidgets import interact
interact(frecuencias, f1=(10.0,200.0), f2=(10.0,200.0))

(2) Display HTML in Jupyter (link: Github)
from IPython.display import HTML
HTML('<iframe src="http://www.mambiente.munimadrid.es/sica/scripts/index.php" \
            width="700" height="400"></iframe>')
(2.1) Loading data file .CSV and plot (link: Github)

#Loading the data
# ./data/barrio_del_pilar-20160322.csv
data1 = np.genfromtxt('./data/barrio_del_pilar-20160322.csv', skip_header=3, delimiter=';', usecols=(2,3,4))
#Print type of csv file
!head -4 data/barrio_del_pilar-20160322.csv

#Dealing with missing value
np.mean(data1, axis=0)
np.nanmean(data1, axis=0)
# masking invalid data
data1 = np.ma.masked_invalid(data1)
np.mean(data1, axis=0)

data2 = np.genfromtxt('./data/barrio_del_pilar-20151222.csv', skip_header=3, delimiter=';', usecols=(2,3,4))
data2 = np.ma.masked_invalid(data2)
#Plotting the data
plt.plot(data1[:, 1], label='2016')
plt.plot(data2[:, 1], label='2015')

plt.legend()

plt.hlines(200, 0, 200, linestyles='--')
plt.ylim(0, 220)

#Máxima diaria de las medias móviles octohorarias: 10 mg/m³
# http://docs.scipy.org/doc/numpy-1.10.0/reference/generated/numpy.convolve.html
def moving_average(x, N=8):
    return np.convolve(x, np.ones(N)/N, mode='same')
plt.plot(moving_average(data1[:, 0]), label='2016')

plt.plot(moving_average(data2[:, 0]), label='2015')

plt.hlines(10, 0, 250, linestyles='--')
plt.ylim(0, 11)
plt.legend()
#Maximum/Minimum of array (Link)
numpy.amax(a, axis=None, out=None, keepdims=<no value>, initial=<no value>)
Arguments :
  • a : numpy array from which it needs to find the maximum value.
  • axis : It’s optional and if not provided then it will flattened the passed numpy array and returns the max value in it.
    • If it’s provided then it will return for array of max values along the axis i.e.
    • If axis=0 then it returns an array containing max value for each columns.
    • If axis=1 then it returns an array containing max value for each row.
Example:
# Get the maximum element from a Numpy array
maxElement = numpy.amax(arr)
print('Max element from Numpy Array : ', maxElement)

# Get the indices of maximum element in numpy array
result = numpy.where(arr == numpy.amax(arr))
print('Returned tuple of arrays :', result)
print('List of Indices of maximum element :', result[0])

# Create a 2D Numpy array from list of lists
arr2D = numpy.array([[11, 12, 13],
[14, 15, 16],
[17, 15, 11],
[12, 14, 15]])
#Some other sources in this file



(3) Continue (link: Github)

Không có nhận xét nào: