Skip to content

Commit 78a9526

Browse files
author
luozhaohui
committed
add matplot samples.
1 parent e541046 commit 78a9526

12 files changed

Lines changed: 239 additions & 2 deletions

File tree

douban_reading_statistics/annualStatistics.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -19,11 +19,11 @@
1919

2020
import os
2121
import re
22+
import sys
2223
import string
2324
import matplotlib.pyplot as plt
24-
from pylab import *
25-
2625
from matplotlib.font_manager import FontManager
26+
from pylab import mpl
2727
import subprocess
2828

2929
datapath = "2017readings.txt"

matplot/axis.png

27.3 KB
Loading

matplot/axis_ex.png

30.2 KB
Loading

matplot/curve.png

30.3 KB
Loading

matplot/curve_ex.png

78.6 KB
Loading

matplot/fakedata.txt

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
0 0
2+
1 1
3+
2 4
4+
3 9
5+
4 16
6+
5 25
7+
6 36
8+
7 49
9+
8 64
10+
9 81
11+
0 0
12+
1 1
13+
2 4
14+
3 9
15+
4 16
16+
5 25
17+
6 36
18+
7 49
19+
8 64
20+
9 81

matplot/histograms.png

11.1 KB
Loading

matplot/matplot.py

Lines changed: 217 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,217 @@
1+
#!/usr/bin/env python
2+
#! encoding=utf-8
3+
#
4+
# sudo apt-get install python-matplotlib
5+
# sudo apt-get install python-numpy python-scipy python-matplotlib ipython ipython-notebook python-pandas python-sympy python-nose
6+
#
7+
8+
import numpy as np
9+
import matplotlib.pyplot as plt
10+
from mpl_toolkits.mplot3d import Axes3D
11+
from matplotlib.ticker import MultipleLocator, FuncFormatter
12+
13+
def make_axis():
14+
x1 = [1, 2, 3, 4, 5]# Make x, y arrays for each graph
15+
y1 = [1, 4, 9, 16, 25]
16+
x2 = [1, 2, 4, 6, 8]
17+
y2 = [2, 4, 8, 12, 16]
18+
19+
plt.plot(x1, y1, 'r')# use pylab to plot x and y
20+
plt.plot(x2, y2, 'g')
21+
22+
plt.title('Plot of y vs. x')# give plot a title
23+
plt.xlabel('x axis')# make axis labels
24+
plt.ylabel('y axis')
25+
26+
plt.xlim(0.0, 9.0)# set axis limits
27+
plt.ylim(0.0, 30.)
28+
29+
plt.savefig('axis.png')
30+
plt.show()# show the plot on the screen
31+
32+
def pi_formatter(x, pos):
33+
"""
34+
比较罗嗦地将数值转换为以pi/4为单位的刻度文本
35+
"""
36+
m = np.round(x / (np.pi/4))
37+
n = 4
38+
if m%2==0: m, n = m/2, n/2
39+
if m%2==0: m, n = m/2, n/2
40+
if m == 0:
41+
return "0"
42+
if m == 1 and n == 1:
43+
return "$\pi$"
44+
if n == 1:
45+
return r"$%d \pi$" % m
46+
if m == 1:
47+
return r"$\frac{\pi}{%d}$" % n
48+
return r"$\frac{%d \pi}{%d}$" % (m,n)
49+
50+
def make_axis_ex():
51+
x = np.arange(0, 4*np.pi, 0.01)
52+
y = np.sin(x)
53+
54+
fig = plt.figure(figsize=(8,4))
55+
fig.patch.set_color("g") # 属性背景颜色
56+
fig.canvas.draw() # 属性修改之后更新显示
57+
58+
plt.plot(x, y)
59+
ax = plt.gca()
60+
61+
# 设置两个坐标轴的范围
62+
plt.ylim(-1.5,1.5)
63+
plt.xlim(0, np.max(x))
64+
65+
# 设置图的底边距
66+
plt.subplots_adjust(bottom = 0.15)
67+
68+
plt.grid() #开启网格
69+
70+
# 主刻度为pi/4
71+
ax.xaxis.set_major_locator( MultipleLocator(np.pi/4) )
72+
73+
# 主刻度文本用pi_formatter函数计算
74+
ax.xaxis.set_major_formatter( FuncFormatter( pi_formatter ) )
75+
76+
# 副刻度为pi/20
77+
ax.xaxis.set_minor_locator( MultipleLocator(np.pi/20) )
78+
79+
# 设置刻度文本的大小
80+
for tick in ax.xaxis.get_major_ticks():
81+
tick.label1.set_fontsize(16)
82+
83+
# 刻度标签
84+
for label in ax.xaxis.get_ticklabels():
85+
label.set_color("yellow")
86+
label.set_rotation(45)
87+
label.set_fontsize(16)
88+
89+
# 刻度线
90+
for line in ax.xaxis.get_ticklines():
91+
line.set_color("red")
92+
line.set_markersize(25)
93+
line.set_markeredgewidth(3)
94+
95+
plt.savefig('axis_ex.png')
96+
plt.show()
97+
98+
99+
def make_curve():
100+
t = np.arange(0.0, 1.01, 0.01)
101+
s = np.sin(2 * 2 * np.pi * t)
102+
103+
plt.fill(t, s * np.exp(-5 * t), 'r')
104+
plt.grid(True)
105+
106+
# save as png(jpeg, pdf, etc)
107+
plt.savefig('curve.png')
108+
plt.show()
109+
110+
def make_curve_ex():
111+
x = np.linspace(0, 10, 1000)
112+
y = np.sin(x)
113+
z = np.cos(x**2)
114+
115+
plt.figure(figsize=(10, 6))
116+
# label : 给所绘制的曲线一个名字,此名字在图示(legend)中显示。
117+
# 只要在字符串前后添加"$"符号,matplotlib就会使用其内嵌的latex引擎绘制的数学公式。
118+
plt.plot(x, y, label="$sin(x)$", color="red", linewidth=2)
119+
plt.plot(x, z, "b--", label="$cos(x^2)$") # "b--"指定曲线的颜色和线型
120+
plt.xlabel("Time(s)")
121+
plt.ylabel("Volt")
122+
plt.title("PyPlot First Example")
123+
plt.ylim(-1.2, 1.2)
124+
plt.legend() # 显示图示
125+
126+
plt.savefig('curve_ex.png')
127+
plt.show()
128+
129+
def make_scatter():
130+
if False:
131+
fig = plt.figure()
132+
ax = fig.add_subplot(2,1,1) # two rows, one column, first plot
133+
t = ax.scatter(np.random.rand(20), np.random.rand(20))
134+
135+
plt.savefig('scatter_one.png')
136+
plt.show()
137+
else:
138+
# Use numpy to load the data contained in the file
139+
# 'fakedata.txt' into a 2-D array called data
140+
data = np.loadtxt('fakedata.txt')
141+
142+
# plot the first column as x, and second column as y
143+
plt.plot(data[:, 0], data[:, 1], 'ro')
144+
plt.xlabel('x')
145+
plt.ylabel('y')
146+
plt.xlim(0.0, 10.)
147+
148+
plt.savefig('scatter_two.png')
149+
plt.show()
150+
151+
def make_pie():
152+
# make a square figure and axes
153+
plt.figure(1, figsize=(6, 6))
154+
ax = plt.axes([0.1, 0.1, 0.8, 0.8])
155+
156+
labels = 'Frogs', 'Hogs', 'Dogs', 'Logs'
157+
fracs = [15, 30, 45, 10]
158+
159+
explode = (0, 0.05, 0, 0)
160+
plt.pie(fracs, explode=explode, labels=labels, autopct='%1.1f%%', shadow=True)
161+
plt.title('Raining Hogs and Dogs', bbox={'facecolor':'0.8', 'pad':10})
162+
plt.savefig('pie.png')
163+
plt.show()
164+
165+
def make_histograms():
166+
# make an array of random numbers with a gaussian distribution with
167+
# mean = 5.0
168+
# rms = 3.0
169+
# number of points = 1000
170+
data = np.random.normal(5.0, 3.0, 1000)
171+
172+
# make a histogram of the data array
173+
plt.hist(data)
174+
175+
# make plot labels
176+
plt.xlabel('data')
177+
178+
plt.savefig('histograms.png')
179+
plt.show()
180+
181+
def randrange(n, vmin, vmax):
182+
'''
183+
Helper function to make an array of random numbers having shape (n, )
184+
with each number distributed Uniform(vmin, vmax).
185+
'''
186+
return (vmax - vmin) * np.random.rand(n) + vmin
187+
188+
def make_scatter3d():
189+
fig = plt.figure()
190+
ax = fig.add_subplot(111, projection='3d')
191+
192+
n = 100
193+
194+
# For each set of style and range settings, plot n random points in the box
195+
# defined by x in [23, 32], y in [0, 100], z in [zlow, zhigh].
196+
for c, m, zlow, zhigh in [('r', 'o', -50, -25), ('b', '^', -30, -5)]:
197+
xs = randrange(n, 23, 32)
198+
ys = randrange(n, 0, 100)
199+
zs = randrange(n, zlow, zhigh)
200+
ax.scatter(xs, ys, zs, c=c, marker=m)
201+
202+
ax.set_xlabel('X Label')
203+
ax.set_ylabel('Y Label')
204+
ax.set_zlabel('Z Label')
205+
206+
plt.savefig('scatter3d.png')
207+
plt.show()
208+
209+
if __name__ == '__main__':
210+
#make_axis()
211+
#make_axis_ex()
212+
#make_curve()
213+
#make_curve_ex()
214+
#make_scatter()
215+
#make_pie()
216+
#make_histograms()
217+
make_scatter3d()

matplot/pie.png

43.1 KB
Loading

matplot/scatter3d.png

97.6 KB
Loading

0 commit comments

Comments
 (0)