|
1 |
| -#!/usr/bin/env python |
2 |
| -# Demonstrating the improvements and options of the proposed new ScalarFormatter |
3 |
| -from pylab import * |
4 |
| -from matplotlib.ticker import OldScalarFormatter |
5 |
| - |
6 |
| -x = frange(0, 1, .01) |
7 |
| -f = figure(figsize=(6, 6)) |
8 |
| -f.text(0.5, 0.975, 'The old formatter', horizontalalignment='center', verticalalignment='top') |
9 |
| -subplot(221) |
10 |
| -plot(x*1e5 + 1e10, x*1e-10 + 1e-5) |
11 |
| -gca().xaxis.set_major_formatter(OldScalarFormatter()) |
12 |
| -gca().yaxis.set_major_formatter(OldScalarFormatter()) |
13 |
| -subplot(222) |
14 |
| -plot(x*1e5, x*1e-4) |
15 |
| -gca().xaxis.set_major_formatter(OldScalarFormatter()) |
16 |
| -gca().yaxis.set_major_formatter(OldScalarFormatter()) |
17 |
| -subplot(223) |
18 |
| -plot(-x*1e5 - 1e10, -x*1e-5 - 1e-10) |
19 |
| -gca().xaxis.set_major_formatter(OldScalarFormatter()) |
20 |
| -gca().yaxis.set_major_formatter(OldScalarFormatter()) |
21 |
| -subplot(224) |
22 |
| -plot(-x*1e5, -x*1e-4) |
23 |
| -gca().xaxis.set_major_formatter(OldScalarFormatter()) |
24 |
| -gca().yaxis.set_major_formatter(OldScalarFormatter()) |
25 |
| - |
26 |
| -x = frange(0, 1, .01) |
27 |
| -f = figure(figsize=(6, 6)) |
28 |
| -f.text(0.5, 0.975, 'The new formatter, default settings', horizontalalignment='center', |
29 |
| - verticalalignment='top') |
30 |
| -subplot(221) |
31 |
| -plot(x*1e5 + 1e10, x*1e-10 + 1e-5) |
32 |
| -gca().xaxis.set_major_formatter(ScalarFormatter()) |
33 |
| -gca().yaxis.set_major_formatter(ScalarFormatter()) |
34 |
| -subplot(222) |
35 |
| -plot(x*1e5, x*1e-4) |
36 |
| -gca().xaxis.set_major_formatter(ScalarFormatter()) |
37 |
| -gca().yaxis.set_major_formatter(ScalarFormatter()) |
38 |
| -subplot(223) |
39 |
| -plot(-x*1e5 - 1e10, -x*1e-5 - 1e-10) |
40 |
| -gca().xaxis.set_major_formatter(ScalarFormatter()) |
41 |
| -gca().yaxis.set_major_formatter(ScalarFormatter()) |
42 |
| -subplot(224) |
43 |
| -plot(-x*1e5, -x*1e-4) |
44 |
| -gca().xaxis.set_major_formatter(ScalarFormatter()) |
45 |
| -gca().yaxis.set_major_formatter(ScalarFormatter()) |
46 |
| - |
47 |
| -x = frange(0, 1, .01) |
48 |
| -f = figure(figsize=(6, 6)) |
49 |
| -f.text(0.5, 0.975, 'The new formatter, no numerical offset', horizontalalignment='center', |
50 |
| - verticalalignment='top') |
51 |
| -subplot(221) |
52 |
| -plot(x*1e5 + 1e10, x*1e-10 + 1e-5) |
53 |
| -gca().xaxis.set_major_formatter(ScalarFormatter(useOffset=False)) |
54 |
| -gca().yaxis.set_major_formatter(ScalarFormatter(useOffset=False)) |
55 |
| -subplot(222) |
56 |
| -plot(x*1e5, x*1e-4) |
57 |
| -gca().xaxis.set_major_formatter(ScalarFormatter(useOffset=False)) |
58 |
| -gca().yaxis.set_major_formatter(ScalarFormatter(useOffset=False)) |
59 |
| -subplot(223) |
60 |
| -plot(-x*1e5 - 1e10, -x*1e-5 - 1e-10) |
61 |
| -gca().xaxis.set_major_formatter(ScalarFormatter(useOffset=False)) |
62 |
| -gca().yaxis.set_major_formatter(ScalarFormatter(useOffset=False)) |
63 |
| -subplot(224) |
64 |
| -plot(-x*1e5, -x*1e-4) |
65 |
| -gca().xaxis.set_major_formatter(ScalarFormatter(useOffset=False)) |
66 |
| -gca().yaxis.set_major_formatter(ScalarFormatter(useOffset=False)) |
67 |
| - |
68 |
| -x = frange(0, 1, .01) |
69 |
| -f = figure(figsize=(6, 6)) |
70 |
| -f.text(0.5, 0.975, 'The new formatter, with mathtext', horizontalalignment='center', |
71 |
| - verticalalignment='top') |
72 |
| -subplot(221) |
73 |
| -plot(x*1e5 + 1e10, x*1e-10 + 1e-5) |
74 |
| -gca().xaxis.set_major_formatter(ScalarFormatter(useMathText=True)) |
75 |
| -gca().yaxis.set_major_formatter(ScalarFormatter(useMathText=True)) |
76 |
| -subplot(222) |
77 |
| -plot(x*1e5, x*1e-4) |
78 |
| -gca().xaxis.set_major_formatter(ScalarFormatter(useMathText=True)) |
79 |
| -gca().yaxis.set_major_formatter(ScalarFormatter(useMathText=True)) |
80 |
| -subplot(223) |
81 |
| -plot(-x*1e5 - 1e10, -x*1e-5 - 1e-10) |
82 |
| -gca().xaxis.set_major_formatter(ScalarFormatter(useMathText=True)) |
83 |
| -gca().yaxis.set_major_formatter(ScalarFormatter(useMathText=True)) |
84 |
| -subplot(224) |
85 |
| -plot(-x*1e5, -x*1e-4) |
86 |
| -gca().xaxis.set_major_formatter(ScalarFormatter(useMathText=True)) |
87 |
| -gca().yaxis.set_major_formatter(ScalarFormatter(useMathText=True)) |
88 |
| -show() |
| 1 | +import matplotlib.pyplot as plt |
| 2 | +import numpy as np |
| 3 | +from matplotlib.ticker import OldScalarFormatter, ScalarFormatter |
| 4 | + |
| 5 | +# Example 1 |
| 6 | +x = np.arange(0, 1, .01) |
| 7 | +fig, [[ax1, ax2], [ax3, ax4]] = plt.subplots(2, 2, figsize=(6, 6)) |
| 8 | +fig.text(0.5, 0.975, 'The old formatter', |
| 9 | + horizontalalignment='center', verticalalignment='top') |
| 10 | +ax1.plot(x * 1e5 + 1e10, x * 1e-10 + 1e-5) |
| 11 | +ax1.xaxis.set_major_formatter(OldScalarFormatter()) |
| 12 | +ax1.yaxis.set_major_formatter(OldScalarFormatter()) |
| 13 | + |
| 14 | +ax2.plot(x * 1e5, x * 1e-4) |
| 15 | +ax2.xaxis.set_major_formatter(OldScalarFormatter()) |
| 16 | +ax2.yaxis.set_major_formatter(OldScalarFormatter()) |
| 17 | + |
| 18 | +ax3.plot(-x * 1e5 - 1e10, -x * 1e-5 - 1e-10) |
| 19 | +ax3.xaxis.set_major_formatter(OldScalarFormatter()) |
| 20 | +ax3.yaxis.set_major_formatter(OldScalarFormatter()) |
| 21 | + |
| 22 | +ax4.plot(-x * 1e5, -x * 1e-4) |
| 23 | +ax4.xaxis.set_major_formatter(OldScalarFormatter()) |
| 24 | +ax4.yaxis.set_major_formatter(OldScalarFormatter()) |
| 25 | + |
| 26 | +# Example 2 |
| 27 | +x = np.arange(0, 1, .01) |
| 28 | +fig, [[ax1, ax2], [ax3, ax4]] = plt.subplots(2, 2, figsize=(6, 6)) |
| 29 | +fig.text(0.5, 0.975, 'The new formatter, default settings', |
| 30 | + horizontalalignment='center', |
| 31 | + verticalalignment='top') |
| 32 | + |
| 33 | +ax1.plot(x * 1e5 + 1e10, x * 1e-10 + 1e-5) |
| 34 | +ax1.xaxis.set_major_formatter(ScalarFormatter()) |
| 35 | +ax1.yaxis.set_major_formatter(ScalarFormatter()) |
| 36 | + |
| 37 | +ax2.plot(x * 1e5, x * 1e-4) |
| 38 | +ax2.xaxis.set_major_formatter(ScalarFormatter()) |
| 39 | +ax2.yaxis.set_major_formatter(ScalarFormatter()) |
| 40 | + |
| 41 | +ax3.plot(-x * 1e5 - 1e10, -x * 1e-5 - 1e-10) |
| 42 | +ax3.xaxis.set_major_formatter(ScalarFormatter()) |
| 43 | +ax3.yaxis.set_major_formatter(ScalarFormatter()) |
| 44 | + |
| 45 | +ax4.plot(-x * 1e5, -x * 1e-4) |
| 46 | +ax4.xaxis.set_major_formatter(ScalarFormatter()) |
| 47 | +ax4.yaxis.set_major_formatter(ScalarFormatter()) |
| 48 | + |
| 49 | +# Example 3 |
| 50 | +x = np.arange(0, 1, .01) |
| 51 | +fig, [[ax1, ax2], [ax3, ax4]] = plt.subplots(2, 2, figsize=(6, 6)) |
| 52 | +fig.text(0.5, 0.975, 'The new formatter, no numerical offset', |
| 53 | + horizontalalignment='center', |
| 54 | + verticalalignment='top') |
| 55 | + |
| 56 | +ax1.plot(x * 1e5 + 1e10, x * 1e-10 + 1e-5) |
| 57 | +ax1.xaxis.set_major_formatter(ScalarFormatter(useOffset=False)) |
| 58 | +ax1.yaxis.set_major_formatter(ScalarFormatter(useOffset=False)) |
| 59 | + |
| 60 | +ax2.plot(x * 1e5, x * 1e-4) |
| 61 | +ax2.xaxis.set_major_formatter(ScalarFormatter(useOffset=False)) |
| 62 | +ax2.yaxis.set_major_formatter(ScalarFormatter(useOffset=False)) |
| 63 | + |
| 64 | +ax3.plot(-x * 1e5 - 1e10, -x * 1e-5 - 1e-10) |
| 65 | +ax3.xaxis.set_major_formatter(ScalarFormatter(useOffset=False)) |
| 66 | +ax3.yaxis.set_major_formatter(ScalarFormatter(useOffset=False)) |
| 67 | + |
| 68 | +ax4.plot(-x * 1e5, -x * 1e-4) |
| 69 | +ax4.xaxis.set_major_formatter(ScalarFormatter(useOffset=False)) |
| 70 | +ax4.yaxis.set_major_formatter(ScalarFormatter(useOffset=False)) |
| 71 | + |
| 72 | +# Example 4 |
| 73 | +x = np.arange(0, 1, .01) |
| 74 | +fig, [[ax1, ax2], [ax3, ax4]] = plt.subplots(2, 2, figsize=(6, 6)) |
| 75 | +fig.text(0.5, 0.975, 'The new formatter, with mathtext', |
| 76 | + horizontalalignment='center', |
| 77 | + verticalalignment='top') |
| 78 | + |
| 79 | +ax1.plot(x * 1e5 + 1e10, x * 1e-10 + 1e-5) |
| 80 | +ax1.xaxis.set_major_formatter(ScalarFormatter(useMathText=True)) |
| 81 | +ax1.yaxis.set_major_formatter(ScalarFormatter(useMathText=True)) |
| 82 | + |
| 83 | +ax2.plot(x * 1e5, x * 1e-4) |
| 84 | +ax2.xaxis.set_major_formatter(ScalarFormatter(useMathText=True)) |
| 85 | +ax2.yaxis.set_major_formatter(ScalarFormatter(useMathText=True)) |
| 86 | + |
| 87 | +ax3.plot(-x * 1e5 - 1e10, -x * 1e-5 - 1e-10) |
| 88 | +ax3.xaxis.set_major_formatter(ScalarFormatter(useMathText=True)) |
| 89 | +ax3.yaxis.set_major_formatter(ScalarFormatter(useMathText=True)) |
| 90 | + |
| 91 | +ax4.plot(-x * 1e5, -x * 1e-4) |
| 92 | +ax4.xaxis.set_major_formatter(ScalarFormatter(useMathText=True)) |
| 93 | +ax4.yaxis.set_major_formatter(ScalarFormatter(useMathText=True)) |
| 94 | +plt.show() |
0 commit comments