[Python] Road to a snake charmer (5) Play with Matplotlib

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. f0ee9d36d4f62936bfc61e7bb14747ad.png

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. 2c0f791bbadaebc727640eea69f67acb.png

Matplotlib

How to draw a graph

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])

output_4_1.png

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)'])

output_7_1.png

Multiple Lines

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_12_1.png

Output to file

Output the above figure to a file.

savefig('lines.png')

You can also use shell commands by adding!

>>> !ls *.png
lines.png

Object explicit method

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'])

output_20_1.png

Axis settings

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')

output_22_1.png

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')

output_23_1.png

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)

output_26_0.png

bar graph

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")

output_28_0.png

Stacked graph

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]

output_30_0.png

Scatter plot

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

output_33_1.png

Graph in polar coordinates

r = sin(2*t)
polar(t, r)

output_35_1.png

contour

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()

output_38_0.png

3D graph

#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)

output_40_1.png

** NEXT ** → [Python] Road to a snake charmer (6) Manipulate Pandas

Recommended Posts

[Python] Road to a snake charmer (5) Play with Matplotlib
[Python] Road to a snake charmer (4) Tweak Numpy
[Python] Road to a snake charmer (6) Manipulate Pandas
[Python] Road to snake charmer (3) Python class
[Python] Road to snake charmer (1) Environment construction
A road to intermediate Python
[Python] How to draw a line graph with Matplotlib
[Python] How to draw a scatter plot with Matplotlib
[Road to Python Intermediate] Call a class instance like a function with __call__
Fractal to make and play with Python
I want to play with aws with python
[ROS2] How to play a bag file with python format launch
The road to compiling to Python 3 with Thrift
How to read a CSV file with Python 2/3
Send a message to LINE with Python (LINE Notify)
[REAPER] How to play with Reascript in Python
Try to draw a life curve with python
[Python] How to draw multiple graphs with Matplotlib
I want to make a game with Python
Decide to assign a laboratory with Python (fiction)
Steps to create a Twitter bot with python
[Python] How to draw a histogram in Matplotlib
I want to write to a file with Python
Visualize grib2 on a map with python (matplotlib)
Heatmap with Python + matplotlib
[Python] Created a class to play sin waves in the background with pyaudio
How to convert / restore a string with [] in python
A memo connected to HiveServer2 of EMR with python
Try to make a command standby tool with python
Python Ver. To introduce WebPay with a little code.
I tried to draw a route map with Python
[Let's play with Python] Make a household account book
From buying a computer to running a program with python
[Python] A memo to write CSV vertically with Pandas
I want to manually create a legend with matplotlib
[Python, ObsPy] I wrote a beach ball with Matplotlib + ObsPy
[Piyopiyokai # 1] Let's play with Lambda: Creating a Python script
I want to run a quantum computer with Python
The road to download Matplotlib
Connect to Wikipedia with Python
Post to slack with Python 3
[Python] Play with Discord's Webhook.
Make a fortune with Python
Switch python to 2.7 with alternatives
Write to csv with Python
How to install NPI + send a message to line with python
[Road to intermediate Python] Install packages in bulk with pip
How to convert an array to a dictionary with Python [Application]
Create a Mastodon bot with a function to automatically reply with Python
I made a package to filter time series with python
A collection of competitive pro techniques to solve with Python
Probably the easiest way to create a pdf with Python3
[Python] How to play with class variables with decorator and metaclass
Experiment to make a self-catering PDF for Kindle with Python
Try to bring up a subwindow with PyQt5 and Python
[Let's play with Python] Image processing to monochrome and dots
How to build a python2.7 series development environment with Vagrant
[Road to intermediate Python] Enables comparison operations with original classes
[Python] The first step to making a game with Pyxel
Create a message corresponding to localization with python translation string
[Python] I want to make a 3D scatter plot of the epicenter with Cartopy + Matplotlib!