This is a training to become a data scientist using Python. Here, we will do Matplotlib, three kinds of sacred treasures of Python data analysis.
I've been able to run Python so far, but from now on I'll use ipython notebook.
** PREV ** → [Python] Road to the Snake Charmer (4) Tweaking Numpy ** NEXT ** → [Python] Road to a snake charmer (6) Manipulate Pandas
IPython Notebook IPython is a more powerful interactive shell than the python shell. The IPython Notebook allows you to do this in your browser. It is also recommended for Python beginners as you can execute, edit and save code with the GUI. It also works with major libraries such as Matplotlib and Pandas, so you can embed the output as an image.
You can start it with the following command. The IPython Notebook is included with Anaconda and should already be installed. If the startup is successful, the browser will start automatically.
Terminal
$ ipython notebook
When you start it, it looks like this. Select [New]> [Notebooks]> [Python] on the right to create a new Notebook.
Once you open the Notebook, you can write the code as you like.
You can also write comments in Markdown.
You can do this by pressing the playmark above or by pressing shift + enter
.
Matplotlib
First, import pyplot of mattplotlib as plt. This is a standard writing style.
% matplotlib inline
is required to display matplotlib graphs inline in IPython Notebook.
import matplotlib.pyplot as plt
%matplotlib inline
Let's draw an appropriate graph for the time being.
plt.plot([1, 3, 2, 4])
You can display it inline like this.
Next, I will write sine wave and cos wave.
import numpy as np
from math import pi
#Generate an array divided into 100 from 0 to 2π
x = np.linspace(1, 2*pi, 100)
y = np.sin(x)
plt.plot(x, y)
#Draw grid lines
plt.grid()
#Draw label
plt.legend(['sin(x)'])
If you enter % pylab
, numpy, matplotlib, etc. will be already imported.
%pylab
To draw multiple Lines, divide them like plot (x1, y1); plot (x2, y2), or plot them together like plot (x1, y1, x2, y2). If x is common, y can use np.array.
#0 to 1 0.Generate array in 01 increments
x = arange(0, 1, 0.01)
#Create a 2D array by stacking three 1D arrays vertically
a1 = vstack((x, x**2, x**3)); a1
#Transpose and draw
plot(x, a1.T)
grid()
legend('X X^2 X^3'.split(), loc='best')
Output the above figure to a file.
savefig('lines.png')
You can also use shell commands by adding!
>>> !ls *.png
lines.png
As explained above, Matplotlib can be used without specifying Objects, but when operating multiple graphs and Lines, it is better to operate these Objects explicitly.
x = linspace(0, 2*pi, 100)
figure1 = gcf()
axis1 = gca()
line1, line2, = axis1.plot(x, sin(x), x, cos(x))
line3, = axis1.plot(x, sin(x)+cos(x))
axis1.legend([line1, line2, line3], ['sin', 'cos', 'sin+cos'])
x = linspace(-10, 10, 200)
y = exp(-x ** 2)
plot(x, y)
grid()
#Write x-axis label
xlabel('position')
#Write a y-axis label
ylabel('density')
plot(x, y)
#Examine the range of automatically set axes
xmin, xmax, ymin, ymax = gca().axis()
#Adjust the axis range to make it a little easier to see
xlim([xmin*0.5, xmax*0.5])
ylim([ymin-0.1, ymax+0.1])
grid()
xlabel('position')
ylabel('density')
Line style
You can change the color and shape of the line.
from numpy.random import random
for i, style in enumerate(['r-o', 'g:x', 'b--^', 'm-.s']):
plot(random(10)+i, style)
left = np.array([1, 2, 3, 4, 5])
height1 = np.array([100, 200, 300, 400, 500])
height2 = np.array([1000, 800, 600, 400, 200])
p1 = plt.bar(left, height1, color="green")
p2 = plt.bar(left, height2, bottom=height1, color="orange")
a1 = random((4,10))
x = range(10)
colors = list('rgbm')
stackplot(x, a1, colors=colors)
bars=[bar([0], [0], color=color) for color in colors]
Scatter graph in Cartesian coordinates
t = linspace(0, 2*pi, 1000) #angle
x = sin(3*t)
y = cos(5*t)
scatter(x, y, s=10) #s is the size of the point
Graph in polar coordinates
r = sin(2*t)
polar(t, r)
z(x,y)=x^2+y^3
Draw the color-coded contour lines of.
x1 = y1 = linspace(-1, 1, 10)
x, y = meshgrid(x1, y1)
z = x**2 + y**3
n = linspace(-2, 2, 20) #Contour density
contourf(x, y, z, n) #The z-axis is represented by color
grid()
#Import modules for 3D
from mpl_toolkits.mplot3d import Axes3D
figure1 = gcf()
axis1 = Axes3D(figure1)
x1 = y1 = linspace(-5, 5, 50)
x, y = meshgrid(x1, y1)
z = exp(-(x**2 + y**2))
axis1.plot_surface(x, y, z, rstride=1, cstride=2)
** NEXT ** → [Python] Road to a snake charmer (6) Manipulate Pandas
Recommended Posts