| 1272 | - ``tick_locator`` - A matplotlib locator for the major ticks, or |
| 1273 | a number. There are several options. For more information about |
| 1274 | locators, type ``from matplotlib import ticker`` and then |
| 1275 | ``ticker?``. |
| 1276 | |
| 1277 | - If this is a locator object, then it is the locator for |
| 1278 | the horizontal axis. A value of None means use the default |
| 1279 | locator. |
| 1280 | |
| 1281 | - If it is a list of two locators, then the first is for the |
| 1282 | horizontal axis and one for the vertical axis. A value of |
| 1283 | None means use the default locator (so a value of |
| 1284 | [None, my_locator] uses my_locator for the vertical axis and |
| 1285 | the default for the horizontal axis). |
| 1286 | |
| 1287 | - If in either case above one of the entries is a number `m` |
| 1288 | (something which can be coerced to a float), it will be |
| 1289 | replaced by a MultipleLocator which places major ticks at |
| 1290 | integer multiples of `m`. See examples. |
| 1291 | |
| 1292 | - If in either case above one of the entries is a list of |
| 1293 | numbers, it will be replaced by a FixedLocator which places |
| 1294 | ticks at the locations specified. See examples. |
| 1295 | |
| 1296 | - ``tick_formatter`` - A matplotlib formatter for the major |
| 1297 | ticks. There are several options. For more information about |
| 1298 | formatters, type ``from matplotlib import ticker`` and then |
| 1299 | ``ticker?``. |
| 1300 | |
| 1301 | - If this is a formatter object, then it is the formatter for |
| 1302 | the horizontal axis. A value of None means use the default |
| 1303 | locator. |
| 1304 | |
| 1305 | - If it is a list of two formatters, then the first is for the |
| 1306 | horizontal axis and one for the vertical axis. A value of |
| 1307 | None means use the default formatter (so a value of |
| 1308 | [None, my_formatter] uses my_formatter for the vertical axis |
| 1309 | and the default for the horizontal axis). |
| 1310 | |
| 1311 | - If in either case above one of the entries is a symbolic |
| 1312 | constant such as `\pi`, `e`, or `sqrt(2)`, ticks will be |
| 1313 | formatted nicely at rational multiples of this constant. |
| 1314 | This should only be used with a ``tick_locator`` option using |
| 1315 | nice rational multiples of that constant! |
| 1316 | |
| 1317 | - If in either case above one of the entries is the string |
| 1318 | ``"latex"``, then the formatting will be nice typesetting |
| 1319 | of the ticks. This is intended to be used when the tick |
| 1320 | locator for a given axis is a list including some symbolic |
| 1321 | elements. See examples. |
| 1322 | |
| 1478 | Via matplotlib, Sage allows setting of custom ticks. See |
| 1479 | documentation for :meth:`show` for more details. |
| 1480 | |
| 1481 | :: |
| 1482 | |
| 1483 | sage: plot(sin(pi*x), (x, -8, 8)) # Labels not so helpful |
| 1484 | sage: plot(sin(pi*x), (x, -8, 8), tick_locator=2) # Multiples of 2 |
| 1485 | sage: plot(sin(pi*x), (x, -8, 8), tick_locator=[[-7,-3,0,3,7],[-1/2,0,1/2]]) # Your choices |
| 1486 | sage: plot(1.5/(1+e^(-x)), (x, -10, 10)) # doesn't quite show value of inflection point |
| 1487 | sage: plot(1.5/(1+e^(-x)), (x, -10, 10), tick_locator=[None, 1.5/4]) # It's right at f(x)=0.75! |
| 1488 | |
| 1489 | But be careful to leave enough room for at least two major ticks, so that |
| 1490 | the user can tell what the scale is. |
| 1491 | |
| 1492 | :: |
| 1493 | |
| 1494 | sage: plot(x^2,(x,1,8),tick_locator=6) |
| 1495 | Traceback (most recent call last): |
| 1496 | ... |
| 1497 | ValueError: Expand the range of the independent variable to allow two multiples of your `tick_locator`. |
| 1498 | |
| 1499 | We can also do custom formatting if you need it. See documentation |
| 1500 | for :meth:`show` for more details. |
| 1501 | |
| 1502 | :: |
| 1503 | |
| 1504 | sage: plot(sin(x),(x,0,2*pi),tick_locator=pi/3,tick_formatter=pi) |
| 1505 | sage: plot(2*x+1,(x,0,5),tick_locator=[[0,1,e,pi,sqrt(20)],2],tick_formatter="latex") |
| 1506 | |
1670 | | from matplotlib.ticker import OldScalarFormatter, MaxNLocator |
1671 | | subplot.xaxis.set_major_locator(MaxNLocator(nbins=9)) |
1672 | | subplot.yaxis.set_major_locator(MaxNLocator(nbins=9)) |
1673 | | subplot.xaxis.set_major_formatter(OldScalarFormatter()) |
1674 | | subplot.yaxis.set_major_formatter(OldScalarFormatter()) |
| 1760 | |
| 1761 | from matplotlib.ticker import OldScalarFormatter, MaxNLocator, MultipleLocator, FixedLocator, Locator |
| 1762 | x_locator, y_locator = tick_locator |
| 1763 | if x_locator is None: |
| 1764 | x_locator = MaxNLocator(nbins=9) |
| 1765 | elif isinstance(x_locator,Locator): |
| 1766 | pass |
| 1767 | elif isinstance(x_locator,list): |
| 1768 | x_locator = FixedLocator(x_locator) |
| 1769 | else: # x_locator is a number which can be made a float |
| 1770 | from sage.functions.other import ceil, floor |
| 1771 | if floor(xmax/x_locator)-ceil(xmin/x_locator)>1: |
| 1772 | x_locator=MultipleLocator(float(x_locator)) |
| 1773 | else: # not enough room for two major ticks |
| 1774 | raise ValueError('Expand the range of the independent variable to allow two multiples of your `tick_locator`.') |
| 1775 | if y_locator is None: |
| 1776 | y_locator = MaxNLocator(nbins=9) |
| 1777 | elif isinstance(y_locator,Locator): |
| 1778 | pass |
| 1779 | elif isinstance(y_locator,Locator): |
| 1780 | y_locator = FixedLocator(y_locator) |
| 1781 | else: # y_locator is a number which can be made a float |
| 1782 | from sage.functions.other import ceil, floor |
| 1783 | if floor(ymax/y_locator)-ceil(ymin/y_locator)>1: |
| 1784 | y_locator=MultipleLocator(float(y_locator)) |
| 1785 | else: # not enough room for two major ticks |
| 1786 | raise ValueError('Expand the range of the dependent variable to allow two multiples of your `tick_locator`.') |
| 1787 | |
| 1788 | x_formatter, y_formatter = tick_formatter |
| 1789 | from matplotlib.ticker import FuncFormatter |
| 1790 | from sage.misc.latex import latex |
| 1791 | from sage.symbolic.ring import SR |
| 1792 | if x_formatter is None: |
| 1793 | x_formatter = OldScalarFormatter() |
| 1794 | elif x_formatter in SR: |
| 1795 | from misc import multiple_of_constant |
| 1796 | x_const = x_formatter |
| 1797 | x_formatter = FuncFormatter(lambda n,pos: multiple_of_constant(n,pos,x_const)) |
| 1798 | elif x_formatter == "latex": |
| 1799 | x_formatter = FuncFormatter(lambda n,pos: '$%s$'%latex(n)) |
| 1800 | if y_formatter is None: |
| 1801 | y_formatter = OldScalarFormatter() |
| 1802 | elif y_formatter in SR: |
| 1803 | from misc import multiple_of_constant |
| 1804 | y_const = y_formatter |
| 1805 | y_formatter = FuncFormatter(lambda n,pos: multiple_of_constant(n,pos,y_const)) |
| 1806 | elif y_formatter == "latex": |
| 1807 | y_formatter = FuncFormatter(lambda n,pos: '$%s$'%latex(n)) |
| 1808 | |
| 1809 | subplot.xaxis.set_major_locator(x_locator) |
| 1810 | subplot.yaxis.set_major_locator(y_locator) |
| 1811 | subplot.xaxis.set_major_formatter(x_formatter) |
| 1812 | subplot.yaxis.set_major_formatter(y_formatter) |
1731 | | from matplotlib.ticker import OldScalarFormatter, MaxNLocator |
1732 | | subplot.xaxis.set_major_locator(MaxNLocator(nbins=10,steps=[1,2,5,10])) |
1733 | | subplot.yaxis.set_major_locator(MaxNLocator(nbins=10,steps=[1,2,5,10])) |
1734 | | subplot.xaxis.set_major_formatter(OldScalarFormatter()) |
1735 | | subplot.yaxis.set_major_formatter(OldScalarFormatter()) |
| 1869 | |
| 1870 | from matplotlib.ticker import OldScalarFormatter, MaxNLocator, MultipleLocator, FixedLocator, Locator |
| 1871 | x_locator, y_locator = tick_locator |
| 1872 | if x_locator is None: |
| 1873 | x_locator = MaxNLocator(nbins=9, steps=[1,2,5,10]) |
| 1874 | elif isinstance(x_locator,Locator): |
| 1875 | pass |
| 1876 | elif isinstance(x_locator,list): |
| 1877 | x_locator = FixedLocator(x_locator) |
| 1878 | else: # x_locator is a number which can be made a float |
| 1879 | from sage.functions.other import ceil, floor |
| 1880 | if floor(xmax/x_locator)-ceil(xmin/x_locator)>1: |
| 1881 | x_locator=MultipleLocator(float(x_locator)) |
| 1882 | else: # not enough room for two major ticks |
| 1883 | raise ValueError('Expand the range of the independent variable to allow two multiples of your `tick_locator`.') |
| 1884 | if y_locator is None: |
| 1885 | y_locator = MaxNLocator(nbins=9, steps=[1,2,5,10]) |
| 1886 | elif isinstance(y_locator,Locator): |
| 1887 | pass |
| 1888 | elif isinstance(y_locator,list): |
| 1889 | y_locator = FixedLocator(y_locator) |
| 1890 | else: # y_locator is a number which can be made a float |
| 1891 | from sage.functions.other import ceil, floor |
| 1892 | if floor(ymax/y_locator)-ceil(ymin/y_locator)>1: |
| 1893 | y_locator=MultipleLocator(float(y_locator)) |
| 1894 | else: # not enough room for two major ticks |
| 1895 | raise ValueError('Expand the range of the dependent variable to allow two multiples of your `tick_locator`.') |
| 1897 | x_formatter, y_formatter = tick_formatter |
| 1898 | from matplotlib.ticker import FuncFormatter |
| 1899 | from sage.misc.latex import latex |
| 1900 | from sage.symbolic.ring import SR |
| 1901 | if x_formatter is None: |
| 1902 | x_formatter = OldScalarFormatter() |
| 1903 | elif x_formatter in SR: |
| 1904 | from misc import multiple_of_constant |
| 1905 | x_const = x_formatter |
| 1906 | x_formatter = FuncFormatter(lambda n,pos: multiple_of_constant(n,pos,x_const)) |
| 1907 | elif x_formatter == "latex": |
| 1908 | x_formatter = FuncFormatter(lambda n,pos: '$%s$'%latex(n)) |
| 1909 | if y_formatter is None: |
| 1910 | y_formatter = OldScalarFormatter() |
| 1911 | elif y_formatter in SR: |
| 1912 | from misc import multiple_of_constant |
| 1913 | y_const = y_formatter |
| 1914 | y_formatter = FuncFormatter(lambda n,pos: multiple_of_constant(n,pos,y_const)) |
| 1915 | elif y_formatter == "latex": |
| 1916 | y_formatter = FuncFormatter(lambda n,pos: '$%s$'%latex(n)) |
| 1917 | |
| 1918 | subplot.xaxis.set_major_locator(x_locator) |
| 1919 | subplot.yaxis.set_major_locator(y_locator) |
| 1920 | subplot.xaxis.set_major_formatter(x_formatter) |
| 1921 | subplot.yaxis.set_major_formatter(y_formatter) |