Showing posts with label matplotlib. Show all posts
Showing posts with label matplotlib. Show all posts

Wednesday, June 4, 2014

different signs(marker types) to draw points and lines in plot function in python

Here is a list of option you need to provide for plot function to draw what you want:

example: plt.plot(range(10), linestyle='--', marker='o', color='b')

================    ===============================
character           description
================    ===============================
``'-'``             solid line style
``'--'``            dashed line style
``'-.'``            dash-dot line style
``':'``             dotted line style
``'.'``             point marker
``','``             pixel marker
``'o'``             circle marker
``'v'``             triangle_down marker
``'^'``             triangle_up marker
``'<'``             triangle_left marker
``'>'``             triangle_right marker
``'1'``             tri_down marker
``'2'``             tri_up marker
``'3'``             tri_left marker
``'4'``             tri_right marker
``'s'``             square marker
``'p'``             pentagon marker
``'*'``             star marker
``'h'``             hexagon1 marker
``'H'``             hexagon2 marker
``'+'``             plus marker
``'x'``             x marker
``'D'``             diamond marker
``'d'``             thin_diamond marker
``'|'``             vline marker
``'_'``             hline marker
================    ===============================

Tuesday, April 29, 2014

Plot CDF in Python

perhaps the most easy way of plotting the cumilative distribution function in python:


import numpy as np
import statsmodels.api as sm # recommended import according to the docs
import matplotlib.pyplot as plt

sample = np.random.uniform(0, 1, 50)
sample=[1,2,2,3,2,3,3,3,3,3,3,4,4,4,4,4,60,3,3,3,10]
ecdf = sm.distributions.ECDF(sample)

dsum = sum(sample);
normalized_data = sample/dsum;

x = np.linspace(min(sample), max(sample),10000)
print x
y = ecdf(x)
plt.step(x, y)

plt.show()


Here is the plot:



Sunday, April 27, 2014

Fitting data with SciPy using curve_fit

Suppose that you have a data set consisting of Throughput vs Latency data for your experiment. We’ll start by importing the needed libraries and defining a fitting function. in this example we define a quadratic function. you may define your own function:

from scipy.optimize import curve_fit
def fitFunc(x, a, b, c):
    return a*(x**2)+b*x + c

now we create some points:
y=[25.6, 31.21, 36.82, 42.43, 44.67, 46.91, 48.04, 49.15, 51.2] 
x=[26.0, 41.0, 50.0, 62.5, 69.0, 74.0, 75.050000000000182, 109.0, 98.5]

The scipy.optimize module contains a least squares curve fit routine that requires as input a user-defined fitting function (in our case fitFunc ), the x-axis data (in our case, t) and the y-axis data (in our case, noisy). The curve_fit routine returns an array of fit parameters, and a matrix of covariance data.

tt = np.linspace(min(x),max(x),100)
x=numpy.asarray(x)
y=numpy.asarray(y)
fitParams, fitCovariances = curve_fit(fitFunc, x, y)
print fitParams
print fitCovariances
plt.ylabel('Throughput (%)', fontsize = 16)
plt.xlabel('Latency' , fontsize = 16)
'''plot the real data'''
plt.plot(t,noisy,'b+',markersize=19)   
'''plot the best curving fit'''
plt.plot(tt, fitFunc(tt, fitParams[0], fitParams[1], fitParams[2]))

output:
[-0.00420386  0.88363413  3.96364659]
[[  4.78028309e-07  -6.52166801e-05   1.92964390e-03]
 [ -6.52166801e-05   9.24484129e-03  -2.86615224e-01]
 [  1.92964390e-03  -2.86615224e-01   9.57342339e+00]]

result:


How to plot a function using matplotlib

In this example I show how to evaluate a function using numpy and how to plot the result:

import pylab
import numpy

x = numpy.linspace(-15,15,100) # 100 linearly spaced numbers
y = numpy.sin(x)/x # computing the values of sin(x)/x

# compose plot
pylab.plot(x,y) # sin(x)/x
pylab.plot(x,y,'co') # same function with cyan dots
pylab.plot(x,2*y,x,3*y) # 2*sin(x)/x and 3*sin(x)/x
pylab.show() # show the plot


Here is the plot:


Give it a try :)

Thursday, April 24, 2014

Subplot in python using matplotlib

Here is an example of how to create subplot in python:

import matplotlib.pyplot as plt
import numpy as np

fig=plt.figure()
data=np.arange(900).reshape((30,30))
for i in range(1,5):
    ax=fig.add_subplot(2,2,i)        
    ax.imshow(data)

plt.suptitle('Main title')
plt.show() 

Note that: the first two element in subplot(2,2,i) specify the number of subplot and the number of row and column. in this example it the plot has 2 rows and 2 columns. and i is the position of the subplot.
Also the title of the figure is defined by suptitle function .



Here is the result:

enter image description here




Monday, March 24, 2014

color the boxplot in python matplotlib

This is how you could color your plot in python using matplotlib library.. Basically, we should pass patch_artist=True to boxplot:

import matplotlib.pyplot as plt
import numpy as np

data = [np.random.normal(0, std, 1000) for std in range(1, 6)]
plt.boxplot(data, notch=True, patch_artist=True)

plt.show()

enter image description here

If you'd like to control the color, do something similar to this:
import matplotlib.pyplot as plt
import numpy as np

data = [np.random.normal(0, std, 1000) for std in range(1, 6)]

box = plt.boxplot(data, notch=True, patch_artist=True)

colors = ['cyan', 'lightblue', 'lightgreen', 'tan', 'pink']
for patch, color in zip(box['boxes'], colors):
    patch.set_facecolor(color)

plt.show()

enter image description here

Friday, March 21, 2014

Rotate axis text in python matplotlib

Here is na example of how to rotate the xaxis using step function in Pyploy library:

import matplotlib.pyplot as plt
x = range(len(time))
plt.xticks(x,  time)
locs, labels = plt.xticks()
plt.setp(labels, rotation=90)
plt.plot(x, delay)