-
Notifications
You must be signed in to change notification settings - Fork 40
Expand file tree
/
Copy pathplot_line.py
More file actions
90 lines (68 loc) · 2.38 KB
/
plot_line.py
File metadata and controls
90 lines (68 loc) · 2.38 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
"""
Example for a Matplotlib plot with the following features:
- Logarithmic axis.
- Custom axis ticks.
"""
__author__ = "Thomas Guillod"
__copyright__ = "Thomas Guillod - Dartmouth College"
__license__ = "Mozilla Public License Version 2.0"
import os
import numpy as np
import matplotlib.pyplot as plt
import utils_mpl
# ###########################################################
# Plot global parameters and define dummy data
# ###########################################################
# set global parameters
utils_mpl.set_global()
# create the output folder
os.makedirs("render", exist_ok=True)
# define dummy data
x = np.linspace(0, 2, 11)
y_1 = 1e4 + 0.5e6 * x**2
y_2 = 1e4 + 0.5e6 * x**4
# get the axis ticks
xticks = np.linspace(0.0, 2.0, 4)
yticks = np.power(10.0, np.linspace(4, 7, 4))
# ###########################################################
# Definition of the line plot
# ###########################################################
# create a figure with a determined size
(fig, ax) = utils_mpl.get_fig(size=(3.5, 3.0), dpi=200)
# add reference curves
ax.plot([-1, +3], [1e6, 1e6], "-k", lw=1.0)
ax.plot([-1, +3], [1e5, 1e5], "-k", lw=1.0)
ax.axhspan(1e5, 1e6, facecolor="orange", alpha=0.2)
# add the data curves
ax.plot(x, y_1, "-or", ms=3.0, lw=1.5, label="label 1")
ax.plot(x, y_2, "-ob", ms=3.0, lw=1.5, label="label 2")
# get the axes transformations
ax.set_xscale("linear")
ax.set_yscale("log")
# custom format for y-axis
def custom_format(x, _):
out = f"10^{abs(np.log10(x)):.0f}"
if np.any(np.isclose(x, [1e5, 1e6])):
return f"bnd / ${out}$"
else:
return f"out / ${out}$"
# set the x-axis limit and format
utils_mpl.set_x_axis(ax, bnd=xticks, margin=0.05, log=False)
utils_mpl.set_format(ax.xaxis, ticks=xticks, fmt="${x:.2f}$")
# set the y-axis limit and format
utils_mpl.set_y_axis(ax, bnd=yticks, margin=0.05, log=True)
utils_mpl.set_format(ax.yaxis, ticks=yticks, fmt=custom_format)
# set the legend and labels
ax.legend(loc="upper left")
ax.set_xlabel("x-axis (unit)")
ax.set_ylabel("y-axis (unit)")
ax.set_title("Plot Title")
# set the grid
utils_mpl.set_grid(fig, ax, major=True, minor=True)
# save the plot for Inkscape
utils_mpl.save_svg(fig, "render/line.svg")
# ###########################################################
# Show the plots
# ###########################################################
# show the plot for inspection
plt.show()