阅读笔记

作者:方跃文

Email: fyuewen@gmail.com

时间:始于2017年11月14日, 结束写作于

KYOTO-U

Chapter 5. plot and visulization

时间: 2017 November 14

A brief matplotlib API Primer


In [2]:
%matplotlib inline

import matplotlib.pyplot as plt
import numpy as np

Figure and Subplot

Plots in matplotlib reside within a Figure object. We can create a new figure with plt.figure:


In [3]:
fig = plt.figure() 
#if ipython --pylab was used in terminal, a new windows will show up


<Figure size 432x288 with 0 Axes>

We can number scheme in matplotlib as that in MATLB using plt.figure(2). Through plt.gcf(), we can get a reference to the active figure.

We cannot make a plot without subplots. Hence, we need create one more subplot by add_subplot:


In [4]:
ax1 = fig.add_subplot(2,2,1)

It means that the figure should be 2 * 2, and we're selecting the first of 4 subplots (numbered from 1). If you create the next two subplots, you'll und up with a figure that looks like the figure as shown below


In [5]:
ax2=fig.add_subplot(2,2,2)
ax3=fig.add_subplot(2,2,3)

In [6]:
from numpy.random import randn
plt.plot(randn(50).cumsum(), 'k--')
#fig.show()


Out[6]:
[<matplotlib.lines.Line2D at 0x11ba78320>]

In [7]:
%matplotlib inline

import matplotlib.pyplot as plt
import numpy as np
from numpy.random import randn
fig = plt.figure() 
ax1 = fig.add_subplot(2,2,1)
ax2=fig.add_subplot(2,2,2)
ax3=fig.add_subplot(2,2,3)
plt.plot(randn(50).cumsum(), 'k--')


Out[7]:
[<matplotlib.lines.Line2D at 0x11baf3048>]

In [15]:
%matplotlib inline

import matplotlib.pyplot as plt
import numpy as np
from numpy.random import randn
fig = plt.figure() 
ax1 = fig.add_subplot(2,2,1)
ax2=fig.add_subplot(2,2,2)
ax3=fig.add_subplot(2,2,3)
plt.plot(randn(50).cumsum(), 'o-', color='red')

# The objects returned by subplot are AxesSubplot, hence we can use it by calling each one’s instance methods
# _ = ax1.hist(randn(100), bins = 20, color = 'k', alpha = 0.3)
ax1.hist(randn(100), bins = 20, color = 'k', alpha = 0.3)
ax2.scatter(np.arange(30), np.arange(30) + 3*randn(30))


Out[15]:
<matplotlib.collections.PathCollection at 0x11c6b1438>

In [ ]:


In [ ]:


In [29]:
randn(3)


Out[29]:
array([-1.63616889,  1.21520836, -1.82926063])

In [30]:
np.arange(3)


Out[30]:
array([0, 1, 2])

Since creating Figure and subplot is a very common task, hence a easier command 'plt.subplots' can be used to create a new Figure, and returns a NumPy array containing the created subplot objects:


In [29]:
fig, axes = plt.subplots(nrows=2,ncols=3, sharex=True, sharey=True) #sharex=True, sharey=True在比较相同范围数据时,非常有用
#for short, we can write as
# fig, axes = plt.subplots(2,3, sharex=True, sharey=True)
axes[0,0].hist(randn(100), bins=20, color = 'b', alpha = 0.8)
axes[1,1].hist(randn(100), bins =20, color = 'k', alpha = 0.4)

plt.subplots_adjust(wspace=0.2,hspace=0.2) # plt.subplots_adjust is used to adjust the spacing between the subfigures
plt.show()



In [23]:



<Figure size 432x288 with 0 Axes>

In [ ]:

Table pyplot.subplots optins

arguments descriptions
nrows number of rows of subplots
ncols number of columns of subplots
sharex or sharey all subplots should use the same x-axis ticks
subplot_kw Dictionary of keywords for creating
**fig_kw additional keywords to subplots are used when creating the figure, such as plt.subplots(2,2, figsize=(8,6))

Scattering and liens


In [60]:
# For the reference of the default color map of matplotlib, see
# https://matplotlib.org/users/dflt_style_changes.html

# This example is from CHAPTER 3 of the book "
# matplotlib for developpers", 
# but I made many revisions

import matplotlib.style
import matplotlib as mpl
mpl.style.use('classic')
mpl.rcParams['figure.facecolor'] = '1' 
#if choose the grey backgroud, use 0.75
mpl.rcParams['figure.figsize'] = [6.4,4.8]
mpl.rcParams['lines.linewidth'] = 1.5
mpl.rcParams['legend.fancybox'] = True


import matplotlib.pyplot as plt
import numpy as np
x = np.random.randn(1000)
y = np.random.randn(1000)
size=abs(50*x)
color=np.random.rand(1000)
plt.scatter(x, y, s=size, c=color)
plt.legend('x')
plt.grid()
plt.show()



In [67]:
import numpy as np
print(np.random.randn(40))
print(np.random.rand(40))
#here, we can observe some difference, because rand() functions 
# in the range of [0,1]


[ 0.59613898  0.20407875 -0.48232356 -0.27652808  0.35010528  2.30066687
  0.41115863  1.16768839  0.36042864  0.07059419 -0.54246535 -0.42659555
  1.24309652 -0.60374436  0.50562412 -0.54608642  1.03676394  0.21858241
  0.12535744  0.04530488  0.74146025  0.89089613  0.35035843 -0.13658593
 -0.8105004   0.66018404  1.2905758  -1.29992302 -0.9076506  -0.5368362
  1.02537137  0.3570286  -0.12535213  1.04787877  1.18587881  0.43865812
  0.56029807 -2.90376516 -0.17164046 -0.21593285]
[ 0.4244379   0.42000537  0.26394154  0.93743745  0.36849496  0.61092651
  0.59607165  0.43523366  0.39837581  0.46634567  0.82805535  0.90566463
  0.54647733  0.43733061  0.57979593  0.69929666  0.34922955  0.88572835
  0.90842702  0.07172583  0.37085588  0.35790659  0.08436258  0.98297161
  0.05386844  0.59499903  0.73431309  0.98328976  0.71685443  0.38363176
  0.80012498  0.00182326  0.51818249  0.68040629  0.05122102  0.57179848
  0.46534793  0.72543029  0.95172831  0.40122216]

In [19]:
import matplotlib.style
import matplotlib as mpl
mpl.style.use('classic')
mpl.rcParams['figure.facecolor'] = '1' 
#if choose the grey backgroud, use 0.75
mpl.rcParams['figure.figsize'] = [6.4,4.8]
mpl.rcParams['lines.linewidth'] = 2.
#mpl.rcParams['legend.fancybox'] = True
mpl.rcParams['axes.linewidth'] = 1.5 #set the value globally

fig = plt.figure()
ax = fig.add_subplot(111)
import matplotlib.pyplot as plt
ax.plot([1,3,2,4],label='line')
ax.set_title('A domo function')
ax.legend(shadow=False, fancybox=True, framealpha=0.5)
ax.text(1.0,3.5, 'artificial function', family=['Serif'], fontsize=12)
ax.text(1.0,1.5, 'artificial function', family=['Arial'], fontsize=12)
#See https://matplotlib.org/users/recipes.html to find more
# details on the legend: transparancey, title
plt.show()


Polar charts

Polar plots use a compeletely different coordinate system, so we have dedicated a separate section to them.

For all the previous images, we used the Cartesian system--two perpendicular lines meet at a point (the origin of axes) with precise axies directions to determine positive and negative values on both, the X and Y axes.

A polar system is a 2D coordinate system, in which the position of a point is expressed in terms of a radius and an angle. This system is used where the relation between two points is better expressed using those information.

注解以及在subplot上绘图

legend中可以既包含文本也可以包含箭头。现在我们以金融数据为例,来做一些实际演练。


In [33]:
from datetime import datetime 
import pandas as pd

fig = plt.figure()
ax = fig.add_subplot(111)

data = pd.read_csv('pydata-book/ch08/spx.csv', index_col = 0, parse_dates=True)
spx = data['SPX']

spx.plot(ax=ax, style='k-')


Out[33]:
<matplotlib.axes._subplots.AxesSubplot at 0x11770fda0>

In [31]:
data


Out[31]:
SPX
1990-02-01 328.79
1990-02-02 330.92
1990-02-05 331.85
1990-02-06 329.66
1990-02-07 333.75
1990-02-08 332.96
1990-02-09 333.62
1990-02-12 330.08
1990-02-13 331.02
1990-02-14 332.01
1990-02-15 334.89
1990-02-16 332.72
1990-02-20 327.99
1990-02-21 327.67
1990-02-22 325.70
1990-02-23 324.15
1990-02-26 328.67
1990-02-27 330.26
1990-02-28 331.89
1990-03-01 332.74
1990-03-02 335.54
1990-03-05 333.74
1990-03-06 337.93
1990-03-07 336.95
1990-03-08 340.27
1990-03-09 337.93
1990-03-12 338.67
1990-03-13 336.00
1990-03-14 336.87
1990-03-15 338.07
... ...
2011-09-02 1173.97
2011-09-06 1165.24
2011-09-07 1198.62
2011-09-08 1185.90
2011-09-09 1154.23
2011-09-12 1162.27
2011-09-13 1172.87
2011-09-14 1188.68
2011-09-15 1209.11
2011-09-16 1216.01
2011-09-19 1204.09
2011-09-20 1202.09
2011-09-21 1166.76
2011-09-22 1129.56
2011-09-23 1136.43
2011-09-26 1162.95
2011-09-27 1175.38
2011-09-28 1151.06
2011-09-29 1160.40
2011-09-30 1131.42
2011-10-03 1099.23
2011-10-04 1123.95
2011-10-05 1144.03
2011-10-06 1164.97
2011-10-07 1155.46
2011-10-10 1194.89
2011-10-11 1195.54
2011-10-12 1207.25
2011-10-13 1203.66
2011-10-14 1224.58

5472 rows × 1 columns


In [ ]: