-
Notifications
You must be signed in to change notification settings - Fork 1
/
matplotlib_din461.py
executable file
·106 lines (87 loc) · 3.65 KB
/
matplotlib_din461.py
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
# -*- coding: utf-8 -*-
#
"""
MIT License
Copyright (c) 2018 Christof Küstner
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.
"""
import matplotlib.pyplot as plt
import matplotlib.ticker as mpl_ticker
import numpy as np
def apply_din461(
ax, x_unit_name, y_unit_name, x_left_to_right=True, y_bottom_to_top=True
):
"""
Applies the DIN 461 for units and labels
@param ax: Axis to be motified
@type ax: matplotlib.axes
@param x_unit_name: Name of the unit in x direction
@type x_unit_name: unicode
@param y_unit_name: Name of the unit in y direction
@type y_unit_name: unicode
@param x_left_to_right: If True, arrow from left to right
@type x_left_to_right: bool
@param y_bottom_to_top: If True, arrow bottom to top
@type y_bottom_to_top: bool
"""
# updates can only be applied if plot has been plotted/updated
ax.figure.canvas.draw()
# add arrow to x axis label
label_text = ax.xaxis.get_label().get_text()
if x_left_to_right:
label_text += " $\longrightarrow$\t\t"
else:
label_text = "$\longleftarrow$ " + label_text
ax.set_xlabel(label_text, loc="right")
# add arrow to y axis label
label_text = ax.yaxis.get_label().get_text()
if y_bottom_to_top:
label_text += " $\longrightarrow$\t\t"
else:
label_text = "$\longleftarrow$ " + label_text
ax.set_ylabel(label_text, loc="top")
# change the x unit name
def x_tick_formatter(x, pos):
visible_labels = [t for t in ax.get_xticklabels() if t.get_visible()]
x_number_of_ticks = len(visible_labels)
if pos == x_number_of_ticks - 3: # empirically determined, bypasses prune
return x_unit_name
else:
return "{:g}".format(float("{:.2g}".format(x)))
# ax.xaxis.set_major_locator(mpl_ticker.MaxNLocator(prune="upper"))
ax.xaxis.set_major_formatter(mpl_ticker.FuncFormatter(x_tick_formatter))
# change the y unit name
def y_tick_formatter(x, pos):
visible_labels = [t for t in ax.get_yticklabels() if t.get_visible()]
y_number_of_ticks = len(visible_labels)
if pos == y_number_of_ticks - 3:
return y_unit_name
else:
return "{:g}".format(float("{:.2g}".format(x)))
# ax.yaxis.set_major_locator(mpl_ticker.MaxNLocator(prune="upper"))
ax.yaxis.set_major_formatter(mpl_ticker.FuncFormatter(y_tick_formatter))
if __name__ == "__main__":
# Minimal example (tested in Python 3.x)
t = np.arange(0.0, 1.0 + 0.01, 0.01)
s = np.cos(4 * np.pi * t) + 2
plt.plot(t, s)
plt.xlabel("Time $t$")
plt.ylabel("Voltage $U$")
ax = plt.gca()
apply_din461(ax, "s", "V")
plt.tight_layout()
plt.show()