| FazBrowse GitHub Viewer | Trending | | Home |
| Tools: [Download Repo ZIP] [Original HTTPS Page] |
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -0,0 +1,90 @@ | |||
| 1 | + """ | ||
| 2 | + Show the different tick formatters | ||
| 3 | + """ | ||
| 4 | + | ||
| 5 | + import numpy as np | ||
| 6 | + import matplotlib.pyplot as plt | ||
| 7 | + import matplotlib.ticker as ticker | ||
| 8 | + | ||
| 9 | + | ||
| 10 | + # Setup a plot such that only the bottom spine is shown | ||
| 11 | + def setup(ax): | ||
| 12 | + ax.spines['right'].set_color('none') | ||
| 13 | + ax.spines['left'].set_color('none') | ||
| 14 | + ax.yaxis.set_major_locator(ticker.NullLocator()) | ||
| 15 | + ax.spines['top'].set_color('none') | ||
| 16 | + ax.xaxis.set_ticks_position('bottom') | ||
| 17 | + ax.tick_params(which='major', width=1.00, length=5) | ||
| 18 | + ax.tick_params(which='minor', width=0.75, length=2.5, labelsize=10) | ||
| 19 | + ax.set_xlim(0, 5) | ||
| 20 | + ax.set_ylim(0, 1) | ||
| 21 | + ax.patch.set_alpha(0.0) | ||
| 22 | + | ||
| 23 | + | ||
| 24 | + plt.figure(figsize=(8, 6)) | ||
| 25 | + n = 6 | ||
| 26 | + | ||
| 27 | + # Null formatter | ||
| 28 | + ax = plt.subplot(n, 1, 1) | ||
| 29 | + setup(ax) | ||
| 30 | + ax.xaxis.set_major_locator(ticker.MultipleLocator(1.00)) | ||
| 31 | + ax.xaxis.set_minor_locator(ticker.MultipleLocator(0.25)) | ||
| 32 | + ax.xaxis.set_major_formatter(ticker.NullFormatter()) | ||
| 33 | + ax.xaxis.set_minor_formatter(ticker.NullFormatter()) | ||
| 34 | + ax.text(0.0, 0.5, "NullFormatter()", fontsize=16, transform=ax.transAxes) | ||
| 35 | + | ||
| 36 | + # Fixed formatter | ||
| 37 | + ax = plt.subplot(n, 1, 2) | ||
| 38 | + setup(ax) | ||
| 39 | + ax.xaxis.set_major_locator(ticker.MultipleLocator(1.0)) | ||
| 40 | + ax.xaxis.set_minor_locator(ticker.MultipleLocator(0.25)) | ||
| 41 | + majors = ["", "0", "1", "2", "3", "4", "5"] | ||
| 42 | + ax.xaxis.set_major_formatter(ticker.FixedFormatter(majors)) | ||
| 43 | + minors = [""] + ["%.2f" % (x-int(x)) if (x-int(x)) | ||
| 44 | + else "" for x in np.arange(0, 5, 0.25)] | ||
| 45 | + ax.xaxis.set_minor_formatter(ticker.FixedFormatter(minors)) | ||
| 46 | + ax.text(0.0, 0.5, "FixedFormatter(['', '0', '1', ...])", | ||
| 47 | + fontsize=15, transform=ax.transAxes) | ||
| 48 | + | ||
| 49 | + | ||
| 50 | + # Func formatter | ||
| 51 | + def major_formatter(x, pos): | ||
| 52 | + return "[%.2f]" % x | ||
| 53 | + | ||
| 54 | + | ||
| 55 | + ax = plt.subplot(n, 1, 3) | ||
| 56 | + setup(ax) | ||
| 57 | + ax.xaxis.set_major_locator(ticker.MultipleLocator(1.00)) | ||
| 58 | + ax.xaxis.set_minor_locator(ticker.MultipleLocator(0.25)) | ||
| 59 | + ax.xaxis.set_major_formatter(ticker.FuncFormatter(major_formatter)) | ||
| 60 | + ax.text(0.0, 0.5, 'FuncFormatter(lambda x, pos: "[%.2f]" % x)', | ||
| 61 | + fontsize=15, transform=ax.transAxes) | ||
| 62 | + | ||
| 63 | + | ||
| 64 | + # FormatStr formatter | ||
| 65 | + ax = plt.subplot(n, 1, 4) | ||
| 66 | + setup(ax) | ||
| 67 | + ax.xaxis.set_major_locator(ticker.MultipleLocator(1.00)) | ||
| 68 | + ax.xaxis.set_minor_locator(ticker.MultipleLocator(0.25)) | ||
| 69 | + ax.xaxis.set_major_formatter(ticker.FormatStrFormatter(">%d<")) | ||
| 70 | + ax.text(0.0, 0.5, "FormatStrFormatter('>%d<')", | ||
| 71 | + fontsize=15, transform=ax.transAxes) | ||
| 72 | + | ||
| 73 | + # Scalar formatter | ||
| 74 | + ax = plt.subplot(n, 1, 5) | ||
| 75 | + setup(ax) | ||
| 76 | + ax.xaxis.set_major_locator(ticker.AutoLocator()) | ||
| 77 | + ax.xaxis.set_minor_locator(ticker.AutoMinorLocator()) | ||
| 78 | + ax.xaxis.set_major_formatter(ticker.ScalarFormatter(useMathText=True)) | ||
| 79 | + ax.text(0.0, 0.5, "ScalarFormatter()", fontsize=15, transform=ax.transAxes) | ||
| 80 | + | ||
| 81 | + # StrMethod formatter | ||
| 82 | + ax = plt.subplot(n, 1, 6) | ||
| 83 | + setup(ax) | ||
| 84 | + ax.xaxis.set_major_locator(ticker.MultipleLocator(1.00)) | ||
| 85 | + ax.xaxis.set_minor_locator(ticker.MultipleLocator(0.25)) | ||
| 86 | + ax.xaxis.set_major_formatter(ticker.StrMethodFormatter("{x}")) | ||
| 87 | + ax.text(0.0, 0.5, "StrMethodFormatter('{x}')", | ||
| 88 | + fontsize=15, transform=ax.transAxes) | ||
| 89 | + | ||
| 90 | + plt.show() | ||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -0,0 +1,93 @@ | |||
| 1 | + """ | ||
| 2 | + Show the different tick locators | ||
| 3 | + """ | ||
| 4 | + | ||
| 5 | + import numpy as np | ||
| 6 | + import matplotlib.pyplot as plt | ||
| 7 | + import matplotlib.ticker as ticker | ||
| 8 | + | ||
| 9 | + | ||
| 10 | + # Setup a plot such that only the bottom spine is shown | ||
| 11 | + def setup(ax): | ||
| 12 | + ax.spines['right'].set_color('none') | ||
| 13 | + ax.spines['left'].set_color('none') | ||
| 14 | + ax.yaxis.set_major_locator(ticker.NullLocator()) | ||
| 15 | + ax.spines['top'].set_color('none') | ||
| 16 | + ax.xaxis.set_ticks_position('bottom') | ||
| 17 | + ax.tick_params(which='major', width=1.00) | ||
| 18 | + ax.tick_params(which='major', length=5) | ||
| 19 | + ax.tick_params(which='minor', width=0.75) | ||
| 20 | + ax.tick_params(which='minor', length=2.5) | ||
| 21 | + ax.set_xlim(0, 5) | ||
| 22 | + ax.set_ylim(0, 1) | ||
| 23 | + ax.patch.set_alpha(0.0) | ||
| 24 | + | ||
| 25 | + | ||
| 26 | + plt.figure(figsize=(8, 8)) | ||
| 27 | + n = 8 | ||
| 28 | + | ||
| 29 | + # Null Locator | ||
| 30 | + ax = plt.subplot(n, 1, 1) | ||
| 31 | + setup(ax) | ||
| 32 | + ax.xaxis.set_major_locator(ticker.NullLocator()) | ||
| 33 | + ax.xaxis.set_minor_locator(ticker.NullLocator()) | ||
| 34 | + ax.text(0.0, 0.5, "NullLocator()", fontsize=14, transform=ax.transAxes) | ||
| 35 | + | ||
| 36 | + # Multiple Locator | ||
| 37 | + ax = plt.subplot(n, 1, 2) | ||
| 38 | + setup(ax) | ||
| 39 | + ax.xaxis.set_major_locator(ticker.MultipleLocator(0.5)) | ||
| 40 | + ax.xaxis.set_minor_locator(ticker.MultipleLocator(0.1)) | ||
| 41 | + ax.text(0.0, 0.5, "MultipleLocator(0.5)", fontsize=14, | ||
| 42 | + transform=ax.transAxes) | ||
| 43 | + | ||
| 44 | + # Fixed Locator | ||
| 45 | + ax = plt.subplot(n, 1, 3) | ||
| 46 | + setup(ax) | ||
| 47 | + majors = [0, 1, 5] | ||
| 48 | + ax.xaxis.set_major_locator(ticker.FixedLocator(majors)) | ||
| 49 | + minors = np.linspace(0, 1, 11)[1:-1] | ||
| 50 | + ax.xaxis.set_minor_locator(ticker.FixedLocator(minors)) | ||
| 51 | + ax.text(0.0, 0.5, "FixedLocator([0, 1, 5])", fontsize=14, | ||
| 52 | + transform=ax.transAxes) | ||
| 53 | + | ||
| 54 | + # Linear Locator | ||
| 55 | + ax = plt.subplot(n, 1, 4) | ||
| 56 | + setup(ax) | ||
| 57 | + ax.xaxis.set_major_locator(ticker.LinearLocator(3)) | ||
| 58 | + ax.xaxis.set_minor_locator(ticker.LinearLocator(31)) | ||
| 59 | + ax.text(0.0, 0.5, "LinearLocator(numticks=3)", | ||
| 60 | + fontsize=14, transform=ax.transAxes) | ||
| 61 | + | ||
| 62 | + # Index Locator | ||
| 63 | + ax = plt.subplot(n, 1, 5) | ||
| 64 | + setup(ax) | ||
| 65 | + ax.plot(range(0, 5), [0]*5, color='White') | ||
| 66 | + ax.xaxis.set_major_locator(ticker.IndexLocator(base=.5, offset=.25)) | ||
| 67 | + ax.text(0.0, 0.5, "IndexLocator(base=0.5, offset=0.25)", | ||
| 68 | + fontsize=14, transform=ax.transAxes) | ||
| 69 | + | ||
| 70 | + # Auto Locator | ||
| 71 | + ax = plt.subplot(n, 1, 6) | ||
| 72 | + setup(ax) | ||
| 73 | + ax.xaxis.set_major_locator(ticker.AutoLocator()) | ||
| 74 | + ax.xaxis.set_minor_locator(ticker.AutoMinorLocator()) | ||
| 75 | + ax.text(0.0, 0.5, "AutoLocator()", fontsize=14, transform=ax.transAxes) | ||
| 76 | + | ||
| 77 | + # MaxN Locator | ||
| 78 | + ax = plt.subplot(n, 1, 7) | ||
| 79 | + setup(ax) | ||
| 80 | + ax.xaxis.set_major_locator(ticker.MaxNLocator(4)) | ||
| 81 | + ax.xaxis.set_minor_locator(ticker.MaxNLocator(40)) | ||
| 82 | + ax.text(0.0, 0.5, "MaxNLocator(n=4)", fontsize=14, transform=ax.transAxes) | ||
| 83 | + | ||
| 84 | + # Log Locator | ||
| 85 | + ax = plt.subplot(n, 1, 8) | ||
| 86 | + setup(ax) | ||
| 87 | + ax.set_xlim(10**3, 10**10) | ||
| 88 | + ax.set_xscale('log') | ||
| 89 | + ax.xaxis.set_major_locator(ticker.LogLocator(base=10.0, numticks=15)) | ||
| 90 | + ax.text(0.0, 0.5, "LogLocator(base=10, numticks=15)", | ||
| 91 | + fontsize=15, transform=ax.transAxes) | ||
| 92 | + | ||
| 93 | + plt.show() | ||
| Back | FazBrowse Home | New Git URL |
0 commit comments