forked from WilliamQLiu/python-examples
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsimple_plot.py
More file actions
117 lines (86 loc) · 3.98 KB
/
simple_plot.py
File metadata and controls
117 lines (86 loc) · 3.98 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
""" Matplotlib is a python 2D plotting library that mimics MATLAB and works
well with NumPy.
http://matplotlib.org/api/pyplot_summary.html
API: http://matplotlib.org/api/pyplot_api.html#module-matplotlib.pyplot
Tutorial: http://matplotlib.org/users/pyplot_tutorial.html
Note: Differences from Pyplot Vs Pylab
These are just different ways of doing the same thing;
pyplot is the python plotting library on matplotlib and so is pylab,
but pylab also imports numpy automatically. By default, just use
pyplot and import numpy because its cleaner to not pollute namespace.
"""
import matplotlib as mpl # Only need for configuring default settings
import matplotlib.pyplot as plt
import matplotlib.patches as mpatches # for legend
import numpy as np
### Default Settings for Matplotlib
print mpl.matplotlib_fname() # Find configuration file 'matplotlibrc'
### Settings for Pyplot
#plt.switch_backend('agg')
#plt.ion() #Turn on interactive
#plt.ioff() # Non-interactive
def simple_plot():
""" Do a simple plot """
### Setup Different Types of Data
a = np.linspace(0, 50, 10, endpoint=False) # start, stop, num=50 (# of samples), endpoint (stop at last sample?)
print a # [0, 5, 10, 15, 20, 25, 30, 35, 40, ]
b = np.arange(0, 20, 2) # start, stop, step; returns evenly spaced values given the interval/step
print b # [0, 2, 4, 6, 8, 10, 12, 14, 16, 18]
c = np.array([10, 20, 30, 40, 50, 60, 70, 80, 90, 100])
print c
### Set X and Y
x, y = a, b
### Format Text
plt.figure(figsize=(8,6), dpi=80) # Create 8*6 inch figure
plt.title("Test") # Create a title
plt.grid() # Display grid
plt.xlabel("X axis") # Create text for X-axis
plt.ylabel("Y axis") # Create text for Y-axis
plt.axes()
### Configure X
plt.xticks(np.arange(min(x), max(x)+1, 1.0)) # How often there are tick labels on x-axis
#plt.set_xticklabels() # Set X tick labels (e.g. if you want 3.142 to be pi symbol instead)
plt.xlim(0, 9) # Set how far we can see x-axis
### Configure Y
plt.yticks(np.arange(min(y), max(y)+1, 2.0)) # How often there are tick labels on y-axis
#plt.set_yticklabels() # Set Y tick lables (e.g. if you want 3.142 to be pi symbol instead)
plt.ylim(0, ) # Set how far we can see y-axis
### Legend
plt.legend(loc='upper left')
#plt.legend(bbox_to_anchor=(1,1), loc=2)
### Plot
plt.plot(x, y, color='blue', linewidth=2.5, linestyle='-', label='first')
plt.plot(x, c, color='red', linewidth=2.5, linestyle='--', label='second')
### TODO: Scatterplots, Bar Plots, Pie charts
#plt.scatter(x, y, alpha=0.5) # Create Scatterplot
### TODO: Annotate specific points - point out neat stuff
#plt.annotate('Neat text here', xy=(0, 1), xycoords='data',
# xytext=(-50, 30), textcoords='offset points', arrowprops=dict(arrowstyle="->"))
### TODO: Tick Locators - control the position of the ticks
plt.savefig('my_picture.png', dpi=72) # Save image
plt.show() # Display on screen
def simple_subplot():
""" How to generate subplots. Subplots can be arranged like this:
https://scipy-lectures.github.io/intro/matplotlib/matplotlib.html#subplots
If you want the placement of a smaller plot into a larger plot at any
location, then use 'Axes'
"""
### Make some data
x1 = np.linspace(0.0, 5.0)
x2 = np.linspace(0.0, 2.0)
y1 = np.cos(2 * np.pi * x1) * np.exp(-x1)
y2 = np.cos(2 * np.pi * x2)
### First plot (top)
plt.subplot(2, 1, 1) # (grid rows, grid columns, grid pos for new axes)
plt.plot(x1, y1, 'ko-')
plt.title('A tale of 2 subplots')
plt.ylabel('Damped oscillation')
### Second plot (bottom)
plt.subplot(2, 1, 2) # (grid rows, grid columns, grid pos for new axes)
plt.plot(x2, y2, 'r.-')
plt.xlabel('time (s)')
plt.ylabel('Undamped')
plt.show() # Display plot
if __name__ == '__main__':
simple_plot() # Do a basic plot
simple_subplot() # Have multiple plots