-
Notifications
You must be signed in to change notification settings - Fork 4
Expand file tree
/
Copy pathplotting.py
More file actions
executable file
·139 lines (106 loc) · 4.56 KB
/
plotting.py
File metadata and controls
executable file
·139 lines (106 loc) · 4.56 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
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
#!/usr/bin/env python3
""" plotting.py at https://github.com/wilsonmar/python-samples/blob/main/plotting.py
This is starter sample code to create common visualizations using matplotlib.
STATUS: Working on macOS M2 14.5 (23F79) using Python 3.12.7.
"v003 + density histogram :plotting.py"
Before running this program:
brew install miniconda
conda create -n py312
conda install -c conda-forge python=3.12 matplotlib numpy scienceplots
conda activate py312
chmod +x plotting.py
./plotting.py
See https://matplotlib.org/stable/users/explain/quick_start.html
TODO: Matplotlib advanced features:
* Twin axes
* Logarithmic scales
* Customizable colormaps
* 2D Contour Plots
* 3D plotting
* Animations https://www.youtube.com/watch?v=bNbN9yoEOdU
TODO: Add nanosecond resolution timer like perf-ns.py
TODO: Use https://seaborn.pydata.org/installing.html
See https://www.youtube.com/watch?v=ooqXQ37XHMM for Seaborn
"""
import matplotlib.pyplot as plt
# FIXME: print(matplotlib.__version__)
import numpy as np
import scienceplots # https://github.com/garrettj403/SciencePlots
# Globals:
SAVE_PNG = False # FIXME: png files created with blanks.
# TODO: Define the list of charts to generate in the order specified.
# STAGE: Define sample data:
#x = [1, 2, 3, 4]
#y = [1, 4, 9, 16]
x = np.array([1, 2, 3, 4])
y = np.array([1, 4, 9,16])
# STAGE: Define enviornment:
plt.style.use(['science','ieee','no-latex','grid']) # https://github.com/garrettj403/SciencePlots/wiki/Gallery
plt.rcParams.update({'figure.dpi': '100'}) # dots per inch for IEEE publications
# STAGE: Create a density histogram: https://www.youtube.com/watch?v=cTJBJH8hacc&t=9m10s
# https://github.com/lukepolson/youtube_channel/blob/main/Python%20Tutorial%20Series/matplotlib_essentials.ipynb
# DEFINITION: A density plot normalizes the area of the histogram sums to 1.
res = np.random.randn(1000)*0.2 + 0.4
# TUTORIAL: https://www.youtube.com/watch?v=cTJBJH8hacc&t=12m
plt.suptitle('Sample Density Histogram')
plt.ylabel('Frequency')
plt.xlabel('X-axis')
#plt.figure(figsize=(8,3))
plt.hist(res, bins=5, density=True, histtype='step')
plt.show()
if SAVE_PNG:
plt.savefig('my_histogram.png')
exit()
# STAGE: Create a scatter dots plot:
# From https://matplotlib.org/stable/users/explain/quick_start.html#types-of-inputs-to-plotting-functions
np.random.seed(19680801) # seed the random number generator.
data = {'a': np.arange(50),
'c': np.random.randint(0, 50, 50),
'd': np.random.randn(50)}
data['b'] = data['a'] + 10 * np.random.randn(50)
data['d'] = np.abs(data['d']) * 100
fig, ax = plt.subplots(figsize=(5, 2.7), layout='constrained')
ax.set_title('Sample Scatter Dots Plot')
ax.scatter('a', 'b', c='c', s='d', data=data)
ax.set_xlabel('entry a')
ax.set_ylabel('entry b')
# See https://matplotlib.org/stable/users/explain/text/annotations.html#basic-annotation
ax.annotate('(0,0)\nzero', xy=(0, 0), xytext=(10, 40),
arrowprops=dict(facecolor='grey', shrink=0.05))
plt.show()
if SAVE_PNG:
plt.savefig('my_scatterdots.png')
# STAGE: Create a scatter plot: https://www.youtube.com/watch?v=cTJBJH8hacc
plt.suptitle('Sample Scatter Plot')
plt.xlabel('X-axis')
plt.ylabel('Y-axis')
plt.scatter(x, y)
plt.show()
if SAVE_PNG:
plt.savefig('my_scatterplot.png')
# TODO: Add regression line.
# STAGE: Create a line chart:
fig, ax = plt.subplots()
ax.set_title('Sample Line Chart')
plt.xlabel('X-axis')
plt.ylabel('Y-axis')
ax.plot(x, y, color='red', linestyle='--', marker='o')
plt.show()
if SAVE_PNG:
plt.savefig('my_line_chart.png')
# STAGE: Create a bar chart:
plt.suptitle('Sample Bar Chart')
plt.xlabel('X-axis')
plt.ylabel('Y-axis')
plt.bar(x, y)
plt.show()
if SAVE_PNG:
plt.savefig('my_bar_chart.png')
# TODO: Stacked Barchart
# TODO: Close pop-up window programmatically vs. manually with control+W.
# A figure with one Axes on the left, and two on the right:
#fig, axs = plt.subplot_mosaic([['left', 'right_top'],
# ['left', 'right_bottom']])
# TODO: Add ChatGPT generation of code.
# https://apps.cognitiveclass.ai/learning/course/course-v1:IBMSkillsNetwork+GPXX0TJREN+v1/block-v1:IBMSkillsNetwork+GPXX0TJREN+v1+type@sequential+block@guided_project/block-v1:IBMSkillsNetwork+GPXX0TJREN+v1+type@vertical+block@guided_project_lets_do_it
# https://cf-courses-data.static.labs.skills.network/jupyterlite/2.5.5/lab/index.html?mode=learn&env_type=jupyterlite¬ebook_url=https%3A%2F%2Fcf-courses-data.static.labs.skills.network%2FIBMSkillsNetwork-GPXX0TJREN%2Flabs%2FPlottingByChatGPT.jupyterlite.ipynb&file_path=%2Flabs%2FIBMSkillsNetwork-GPXX0TJREN%2Flabs%2FPlottingByChatGPT.jupyterlite.ipynb