Skip to content

Commit a59b53a

Browse files
tgwoodcockmarcharper
authored andcommitted
Added examples of custom axis scaling and ticks
Added examples/custom_axis_scaling.py which shows three examples: 1) simple example of custom axis scaling, 2) similar to 1 but showing custom axis ticks, 3) more detailed example plotting the full range of data on the left and a zoomed region on the right.
1 parent 4f5dab6 commit a59b53a

File tree

1 file changed

+147
-0
lines changed

1 file changed

+147
-0
lines changed

examples/custom_axis_scaling.py

Lines changed: 147 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,147 @@
1+
import ternary
2+
3+
## Simple example:
4+
## Boundary and Gridlines
5+
scale = 9
6+
figure, tax = ternary.figure(scale=scale)
7+
8+
tax.ax.axis("off")
9+
figure.set_facecolor('w')
10+
11+
# Draw Boundary and Gridlines
12+
tax.boundary(linewidth=1.0)
13+
tax.gridlines(color="black", multiple=1, linewidth=0.5,ls='-')
14+
15+
# Set Axis labels and Title
16+
fontsize = 16
17+
tax.left_axis_label("Logs", fontsize=fontsize, offset=0.13)
18+
tax.right_axis_label("Dogs", fontsize=fontsize, offset=0.12)
19+
tax.bottom_axis_label("Hogs", fontsize=fontsize, offset=0.06)
20+
21+
22+
# Set custom axis limits by passing a dict into set_limits.
23+
# The keys are b, l and r for the three axes and the vals are a list
24+
# of the min and max in data coords for that axis. max-min for each
25+
# axis must be the same as the scale i.e. 9 in this case.
26+
tax.set_axis_limits({'b':[67,76],'l':[24,33],'r':[0,9]})
27+
# get and set the custom ticks:
28+
tax.get_ticks_from_axis_limits()
29+
tax.set_custom_ticks(fsize=10,offset=0.02)
30+
31+
# data can be plotted by entering data coords (rather than simplex coords):
32+
points = [(70,3,27),(73,2,25),(68,6,26)]
33+
points_c = tax.convert_coordinates(points,axisorder='brl')
34+
tax.scatter(points_c,marker='o',s=25,c='r')
35+
36+
tax.ax.set_aspect('equal', adjustable='box')
37+
tax._redraw_labels()
38+
39+
40+
## Simple example with axis tick formatting:
41+
## Boundary and Gridlines
42+
scale = 9
43+
figure, tax = ternary.figure(scale=scale)
44+
45+
tax.ax.axis("off")
46+
figure.set_facecolor('w')
47+
48+
# Draw Boundary and Gridlines
49+
tax.boundary(linewidth=1.0)
50+
tax.gridlines(color="black", multiple=1, linewidth=0.5,ls='-')
51+
52+
# Set Axis labels and Title
53+
fontsize = 16
54+
tax.left_axis_label("Logs", fontsize=fontsize, offset=0.13)
55+
tax.right_axis_label("Dogs", fontsize=fontsize, offset=0.12)
56+
tax.bottom_axis_label("Hogs", fontsize=fontsize, offset=0.06)
57+
58+
59+
# Set custom axis limits by passing a dict into set_limits.
60+
# The keys are b, l and r for the three axes and the vals are a list
61+
# of the min and max in data coords for that axis. max-min for each
62+
# axis must be the same as the scale i.e. 9 in this case.
63+
tax.set_axis_limits({'b':[67,76],'l':[24,33],'r':[0,9]})
64+
# get and set the custom ticks:
65+
# custom tick formats:
66+
# tick_formats can either be a dict, like below or a single format string
67+
# e.g. "%.3e" (valid for all 3 axes) or None, in which case, ints are
68+
# plotted for all 3 axes.
69+
tick_formats = {'b' : "%.2f",'r' : "%d",'l' : "%.1f"}
70+
71+
tax.get_ticks_from_axis_limits()
72+
tax.set_custom_ticks(fsize=10,offset=0.02,tick_formats=tick_formats)
73+
74+
# data can be plotted by entering data coords (rather than simplex coords):
75+
points = [(70,3,27),(73,2,25),(68,6,26)]
76+
points_c = tax.convert_coordinates(points,axisorder='brl')
77+
tax.scatter(points_c,marker='o',s=25,c='r')
78+
79+
tax.ax.set_aspect('equal', adjustable='box')
80+
tax._redraw_labels()
81+
82+
83+
84+
## Zoom example:
85+
## Draw a plot with the full range on the left and a second plot which
86+
## shows a zoomed region of the left plot.
87+
fig = ternary.plt.figure(figsize=(11,6))
88+
ax1 = fig.add_subplot(2,1,1)
89+
ax2 = fig.add_subplot(2,1,2)
90+
91+
tax1 = ternary.TernaryAxesSubplot(ax=ax1,scale=100)
92+
tax1.boundary(linewidth=1.0)
93+
tax1.gridlines(color="black", multiple=10, linewidth=0.5,ls='-')
94+
tax1.ax.axis("equal")
95+
tax1.ax.axis("off")
96+
97+
tax2 = ternary.TernaryAxesSubplot(ax=ax2,scale=30)
98+
axes_colors = {'b' : 'r',
99+
'r' : 'r',
100+
'l' : 'r'
101+
}
102+
tax2.boundary(linewidth=1.0,axes_colors=axes_colors)
103+
tax2.gridlines(color="r", multiple=5, linewidth=0.5,ls='-')
104+
tax2.ax.axis("equal")
105+
tax2.ax.axis("off")
106+
107+
fontsize = 16
108+
tax1.set_title("Entire range")
109+
tax1.left_axis_label("Logs", fontsize=fontsize, offset=0.12)
110+
tax1.right_axis_label("Dogs", fontsize=fontsize, offset=0.12)
111+
tax1.bottom_axis_label("Hogs", fontsize=fontsize, offset=0.)
112+
tax2.set_title("Zoomed region",color='r')
113+
tax2.left_axis_label("Logs", fontsize=fontsize, offset=0.17,color='r')
114+
tax2.right_axis_label("Dogs", fontsize=fontsize, offset=0.17,color='r')
115+
tax2.bottom_axis_label("Hogs", fontsize=fontsize, offset=0.03,color='r')
116+
117+
tax1.ticks(multiple=10,offset=0.02)
118+
119+
tax2.set_axis_limits({'b':[60,75],'l':[15,30],'r':[10,25]})
120+
tax2.get_ticks_from_axis_limits(multiple=5)
121+
tick_formats = "%.1f"
122+
tax2.set_custom_ticks(fsize=10,offset=0.025,multiple=5,axes_colors=axes_colors,
123+
tick_formats=tick_formats)
124+
125+
# plot some data
126+
points = [(62,12,26),(63.5,13.5,23),(65,14,21),(61,15,24),(62,16,22),
127+
(67.5,14.5,18),(68.2,16.5,15.3),(62,22.5,15.5)]
128+
129+
# data coords == simplex coords:
130+
tax1.scatter(points,marker='^',s=25,c='b')
131+
# data coords != simplex coords:
132+
points_c = tax2.convert_coordinates(points,axisorder='brl')
133+
tax2.scatter(points_c,marker='^',s=25,c='b')
134+
135+
# draw the zoom region on the first plot
136+
tax1.line((60,10,30),(75,10,15),color='r',lw=2.0)
137+
tax1.line((60,10,30),(60,25,15),color='r',lw=2.0)
138+
tax1.line((75,10,15),(60,25,15),color='r',lw=2.0)
139+
140+
fig.set_facecolor("w")
141+
142+
tax1.ax.set_position([0.01,0.05,0.46,0.8])
143+
tax2.ax.set_position([0.50,0.05,0.46,0.8])
144+
145+
tax1.resize_drawing_canvas()
146+
tax2.resize_drawing_canvas()
147+
ternary.plt.show()

0 commit comments

Comments
 (0)