-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathTutorial_plot.py
More file actions
175 lines (147 loc) · 4.22 KB
/
Tutorial_plot.py
File metadata and controls
175 lines (147 loc) · 4.22 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
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
# %% [markdown]
# # Tutorial - Plotting with matplotlib and seaborn
# # Matplotlib
# %%
# Set up
import matplotlib as mpl
import matplotlib.pyplot as plt
import seaborn as sns
import numpy as np
import pandas as pd
# %% Plot a line
x = np.arange(-10,10,1) # Create an array
y = x**2 # Square the array
plt.plot(x,y) # Plot a line plot
#plt.show()
# %% Plot multiple lines
x = np.arange(-10,10,1)
y1 = x
y2 = x**2
y3 = x**3
# Method 1
plt.plot(x,y1)
plt.plot(x,y2)
plt.plot(x,y3)
# Method 2
plt.plot(x,y1,x,y2,x,y3)
# %% Figure legend
plt.close('all')
plt.plot(x,y1,label=r'$x$')
plt.plot(x,y2,label=r'$x^2$')
plt.plot(x,y3,label=r'$x^3$')
plt.legend()
# %% Axis labels and title
plt.close('all')
plt.plot(x,y1,label=r'$x$')
plt.plot(x,y2,label=r'$x^2$')
plt.plot(x,y3,label=r'$x^3$')
plt.legend()
plt.xlabel('x_axis')
plt.ylabel('y_axis')
plt.title('title')
# %% Axis range
plt.close('all')
plt.plot(x,y1,label=r'$x$')
plt.plot(x,y2,label=r'$x^2$')
plt.plot(x,y3,label=r'$x^3$')
plt.legend()
plt.xlim([-5,5])
plt.ylim([-100,100])
# %% Color
plt.close('all')
plt.plot(x,y1,label=r'$x$',color='red')
plt.plot(x,y2,label=r'$x^2$', color='blue')
plt.plot(x,y3,label=r'$x^3$',color='#000000')
plt.legend()
# %% Markers and linestyle
# linestyles - https://matplotlib.org/3.1.0/gallery/lines_bars_and_markers/linestyles.html
# markers - https://matplotlib.org/3.3.1/api/markers_api.html
plt.close('all')
plt.plot(x,y1,label=r'$x$',marker='.')
plt.plot(x,y2,label=r'$x^2$',marker='*')
plt.plot(x,y3,label=r'$x^3$',linestyle='-.')
plt.legend()
# %% Save plots
plt.close('all')
plt.plot(x,y1,label=r'$x$',marker='.')
plt.plot(x,y2,label=r'$x^2$',marker='*')
plt.plot(x,y3,label=r'$x^3$',linestyle='-')
plt.legend()
plt.savefig('./tutorial_fig.png')
# %% Subplots
plt.close('all')
ax1 = plt.subplot(1,3,1) #nrows, ncols, index
ax2 = plt.subplot(1,3,2)
ax3 = plt.subplot(1,3,3)
ax1.plot(x,y1,label=r'$x$',marker='.')
ax2.plot(x,y2,label=r'$x^2$',marker='*')
ax3.plot(x,y3,label=r'$x^3$',linestyle='-')
plt.legend()
# %% Object oriented approach
# Create canvas
## Axes are contained within figures
fig = plt.figure() # Figure object
ax1 = fig.add_subplot(2,2,1) # Axes object
ax2 = fig.add_subplot(2,2,2)
ax3 = fig.add_subplot(2,2,3)
ax4 = fig.add_subplot(2,2,4)
fig.show()
# Add a line object
lin1, = ax1.plot(x,y1) # Line object
fig.show()
# Change object properties
lin1.set_color('red') # Change line color to red
ax1.set_xlabel('x_label') # Change xaxis label in axes 1
ax1.set_title('Subplot 1') # Set title for axes 1
fig.suptitle('Figure title') # Set title for figure
# Miscellaneous commands
#plt.show() # show plots
#plt.close('all') # close all plots
# %% Mpl Gallery
# https://matplotlib.org/gallery/index.html
# %% [markdown]
# # Seaborn
# It is an extension of matplotlib that uses the grammar of graphics to improve the ease of plotting.
# %% Seaborn
sns.set_theme()
plt.close('all')
plt.plot(x,y1,label=r'$x$',marker='.')
plt.plot(x,y2,label=r'$x^2$',marker='*')
plt.plot(x,y3,label=r'$x^3$',linestyle='-.')
plt.legend()
# %% Reading code - grouped boxplots
sns.set_theme(style="ticks", palette="pastel")
# Load the example tips dataset
tips = sns.load_dataset("tips")
# Draw a nested boxplot to show bills by day and time
sns.boxplot(x="day", y="total_bill",
hue="smoker", palette=["m", "g"],
data=tips)
sns.despine(offset=10, trim=True)
# %% Reading code - grouped_barplot
sns.set_theme(style="whitegrid")
penguins = sns.load_dataset("penguins")
# Draw a nested barplot by species and sex
g = sns.catplot(
data=penguins, kind="bar",
x="species", y="body_mass_g", hue="sex",
ci="sd", palette="dark", alpha=.6, height=6
)
g.despine(left=True)
g.set_axis_labels("", "Body mass (g)")
# %% [markdown]
# # Where to look for more information
# * https://seaborn.pydata.org/tutorial.html
# * https://matplotlib.org/3.3.1/tutorials/index.html#
# * https://matplotlib.org/tutorials/introductory/pyplot.html
#
# # Galleries
# * https://seaborn.pydata.org/examples/index.html
# * https://matplotlib.org/3.1.1/gallery/index.html
#
# # Other python plotting libraries
# 1. plotly
# 2. ggplot2
#
# # The grammar of graphics
# https://towardsdatascience.com/a-comprehensive-guide-to-the-grammar-of-graphics-for-effective-visualization-of-multi-dimensional-1f92b4ed4149