-
Notifications
You must be signed in to change notification settings - Fork 155
Expand file tree
/
Copy pathcustom_axis_scaling.py
More file actions
153 lines (123 loc) · 5.64 KB
/
custom_axis_scaling.py
File metadata and controls
153 lines (123 loc) · 5.64 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
"""
This script gives two examples of setting custom axis data limits instead of
having the axis limits being from 0 to scale as is the default case.
In the first example we simply set some axis data limits, get and set the
ticks for the axes and then scatter some data. This example is then repeated
but with the additional feature of showing custom axis tick formatting.
The second example shows how to use custom axis scaling to achieve a zoom
effect. We draw the full plot on the left and then zoom into a specific
region and plot that on the right. The basic principle is the same as the
first example.
"""
import ternary
## Simple example:
figure, tax = ternary.figure(scale=9)
figure.set_size_inches((4.8,4.8))
figure.subplots_adjust(left=0.05, right=0.95, bottom=0.05, top=0.95)
tax.ax.axis("off")
# Draw Boundary and Gridlines
tax.boundary(linewidth=1.0)
tax.gridlines(color="black", multiple=1, linewidth=0.5, ls='-')
# Set Axis labels and Title
fontsize = 16
tax.left_axis_label("Logs", fontsize=fontsize, offset=0.13)
tax.right_axis_label("Dogs", fontsize=fontsize, offset=0.12)
tax.bottom_axis_label("Hogs", fontsize=fontsize, offset=0.06)
# Set custom axis DATA limits by passing a dict into set_axis_limits.
# The keys are b, l and r for the three axes and the values are a list
# of the min and max in data coords for that axis. max-min for each
# axis is the same as the scale i.e. 9 in this case.
tax.set_axis_limits({'b': [67, 76], 'l': [24, 33], 'r': [0, 9]})
# get and set the custom ticks:
tax.get_ticks_from_axis_limits()
tax.set_custom_ticks(fontsize=10, offset=0.02)
# data can be plotted by entering data coords (rather than simplex coords):
points = [(70, 3, 27), (73, 2, 25), (68, 6, 26)]
points_c = tax.convert_coordinates(points,axisorder='brl')
tax.scatter(points_c, marker='o', s=25, c='r')
tax.ax.set_aspect('equal', adjustable='box')
tax._redraw_labels()
figure.canvas.draw()
## Simple example with axis tick formatting:
figure, tax = ternary.figure(scale=9)
figure.set_size_inches((4.8,4.8))
figure.subplots_adjust(left=0.05, right=0.95, bottom=0.05, top=0.95)
tax.ax.axis("off")
# Draw Boundary and Gridlines
tax.boundary(linewidth=1.0)
tax.gridlines(color="black", multiple=1, linewidth=0.5, ls='-')
# Set Axis labels and Title
fontsize = 16
tax.left_axis_label("Logs", fontsize=fontsize, offset=0.13)
tax.right_axis_label("Dogs", fontsize=fontsize, offset=0.12)
tax.bottom_axis_label("Hogs", fontsize=fontsize, offset=0.06)
# Set custom axis DATA limits by passing a dict into set_axis_limits.
# The keys are b, l and r for the three axes and the values are a list
# of the min and max in data coords for that axis. max-min for each
# axis is the same as the scale i.e. 9 in this case.
tax.set_axis_limits({'b': [67, 76], 'l': [24, 33], 'r': [0, 9]})
# get and set the custom ticks:
# custom tick formats:
# tick_formats can either be a dict, like below or a single format string
# e.g. "%.3e" (valid for all 3 axes) or None, in which case, ints are
# plotted for all 3 axes.
tick_formats = {'b': "%.2f", 'r': "%d", 'l': "%.1f"}
tax.get_ticks_from_axis_limits()
tax.set_custom_ticks(fontsize=10, offset=0.02, tick_formats=tick_formats)
# data can be plotted by entering data coords (rather than simplex coords):
points = [(70, 3, 27), (73, 2, 25), (68, 6, 26)]
points_c = tax.convert_coordinates(points, axisorder='brl')
tax.scatter(points_c, marker='o', s=25, c='r')
tax.ax.set_aspect('equal', adjustable='box')
tax._redraw_labels()
figure.canvas.draw()
## Zoom example:
# Draw a plot with the full range on the left and a second plot which
# shows a zoomed region of the left plot.
fig = ternary.plt.figure(figsize=(11, 6))
ax1 = fig.add_subplot(2, 1, 1)
ax2 = fig.add_subplot(2, 1, 2)
tax1 = ternary.TernaryAxesSubplot(ax=ax1, scale=100)
tax1.boundary(linewidth=1.0)
tax1.gridlines(color="black", multiple=10, linewidth=0.5, ls='-')
tax1.ax.axis("equal")
tax1.ax.axis("off")
tax2 = ternary.TernaryAxesSubplot(ax=ax2,scale=30)
axes_colors = {'b': 'r', 'r': 'r', 'l': 'r'}
tax2.boundary(linewidth=1.0, axes_colors=axes_colors)
tax2.gridlines(color="r", multiple=5, linewidth=0.5, ls='-')
tax2.ax.axis("equal")
tax2.ax.axis("off")
fontsize = 16
tax1.set_title("Entire range")
tax1.left_axis_label("Logs", fontsize=fontsize, offset=0.12)
tax1.right_axis_label("Dogs", fontsize=fontsize, offset=0.12)
tax1.bottom_axis_label("Hogs", fontsize=fontsize, offset=0.)
tax2.set_title("Zoomed region",color='r')
tax2.left_axis_label("Logs", fontsize=fontsize, offset=0.17, color='r')
tax2.right_axis_label("Dogs", fontsize=fontsize, offset=0.17, color='r')
tax2.bottom_axis_label("Hogs", fontsize=fontsize, offset=0.03, color='r')
tax1.ticks(multiple=10,offset=0.02)
tax2.set_axis_limits({'b': [60, 75], 'l': [15, 30], 'r': [10, 25]})
tax2.get_ticks_from_axis_limits(multiple=5)
tick_formats = "%.1f"
tax2.set_custom_ticks(fontsize=10, offset=0.025, axes_colors=axes_colors,
tick_formats=tick_formats)
# plot some data
points = [(62, 12, 26), (63.5, 13.5, 23), (65, 14, 21), (61, 15, 24),
(62, 16, 22), (67.5, 14.5, 18), (68.2, 16.5, 15.3), (62, 22.5, 15.5)]
# data coords == simplex coords:
tax1.scatter(points, marker='^', s=25, c='b')
# data coords != simplex coords:
points_c = tax2.convert_coordinates(points, axisorder='brl')
tax2.scatter(points_c, marker='^', s=25, c='b')
# draw the zoom region on the first plot
tax1.line((60, 10, 30), (75, 10, 15), color='r', lw=2.0)
tax1.line((60, 10, 30), (60, 25, 15), color='r', lw=2.0)
tax1.line((75, 10, 15), (60, 25, 15), color='r', lw=2.0)
tax1.ax.set_position([0.01, 0.05, 0.46, 0.8])
tax2.ax.set_position([0.50, 0.05, 0.46, 0.8])
tax1.resize_drawing_canvas()
tax2.resize_drawing_canvas()
figure.canvas.draw()
ternary.plt.show()