_images/toyplot.png

Tick Locators

When you create a figure in Toyplot, you begin by creating a canvas, add coordinate systems, and add data to the coordinate systems in the form of marks. The coordinate system axes map data from its domain to a range on the canvas, and generate ticks in domain-space with tick labels as an integral part of the process.

To generate tick locations and tick labels, individual coordinate system axes delegate to the tick locator classes in the toyplot.locator module. Each tick locator class is responsible for generating a collection of tick locations from the range of values in an axis domain, and there are several different classes available that implement different strategies for generating “good” tick locations. If you don’t specify any tick locators when creating axes, sensible defaults will be chosen for you. For example:

[1]:
import numpy
x = numpy.arange(20)
y = numpy.linspace(0, 1, len(x)) ** 2
[2]:
import toyplot
canvas, axes, mark = toyplot.plot(x, y, width=300)
051015200.00.51.0

Note that the X and Y axes in this plot have sensible ticks that use round numbers. In this case the algorithm for identifying “good” tick values is provided by Toyplot’s default toyplot.locator.Extended locator.

However, notice that the X axis has a tick at the value \(20\), even though the domain of the data values is \([0, 19]\). Toyplot always expands the visible domain to include every tick generated by a locator. If you need to change this behavior, there are several approaches: first, you can configure toyplot.locator.Extended to never generate ticks outside the data domain:

[3]:
canvas, axes, mark = toyplot.plot(x, y, width=300)
axes.x.ticks.locator = toyplot.locator.Extended(only_inside=True)
0612180.00.51.0

Notice that this can have the side effect of altering the number and spacing of ticks. Let’s say instead that we prefer to always have ticks that include the exact minimum and maximum data domain values, and evenly divide the rest of the domain. In this case, we can override the default choice of locator with the toyplot.locator.Uniform tick locator:

[4]:
canvas, axes, mark = toyplot.plot(x, y, width=300)
axes.x.ticks.locator = toyplot.locator.Uniform(count=5)
04.759.514.25190.00.51.0

A third alternative is to use explicit tick locators to explicitly specify the ticks to generate for an axis. Explicit tick locators are discussed in detail below.

In the meantime, we can also override the default formatting string used to generate the locator labels:

[5]:
canvas, axes, mark = toyplot.plot(x, y, width=300)
axes.x.ticks.locator = toyplot.locator.Uniform(count=5, format="{:.2f}")
0.004.759.5014.2519.000.00.51.0

Anytime you use log scale axes in a plot, Toyplot automatically uses the toyplot.locator.Log locator to provide ticks that are evenly-spaced in the logarithmic domain:

[6]:
canvas, axes, mark = toyplot.plot(x, y, xscale="log10", width=300)
010 010 110 20.00.51.0

If you don’t like the “superscript” notation that the Log locator produces, you could replace it with your own locator and custom format:

[7]:
canvas, axes, mark = toyplot.plot(x, y, xscale="log10", width=300)
axes.x.ticks.locator = toyplot.locator.Log(base=10, format="{base}^{exponent}")
010^010^110^20.00.51.0

Or even display raw tick values:

[8]:
canvas, axes, mark = toyplot.plot(x, y, xscale="log2", width=300)
axes.x.ticks.locator = toyplot.locator.Log(base=2, format="{:.0f}")
0124816320.00.51.0

Although you might not think of Table Coordinates as needing tick locators, when you use toyplot.matrix() or toyplot.canvas.Canvas.matrix() to visualize a matrix, it generates a table visualization that uses toyplot.locator.Integer locators to generate row and column labels:

[9]:
numpy.random.seed(1234)
canvas, table = toyplot.matrix(numpy.random.random((5, 5)), width=300)
0123400.1915190.6221090.4377280.7853590.77997610.2725930.2764640.8018720.9581390.87593320.3578170.5009950.6834630.7127020.37025130.5611960.5030830.0137680.7728270.88264140.3648860.6153960.0753810.3688240.933140

By default the Integer locator generates a tick/label for every integer in the range \([0, N)\) … as you visualize larger matrices, you’ll find that a label for every row and column becomes crowded, in which case you can override the default step parameter to space-out the labels:

[10]:
canvas, table = toyplot.matrix(numpy.random.random((50, 50)), width=400, step=5)
05101520253035404500.6513780.3972030.7887300.3168360.5680990.8691270.4361730.8021480.1437670.7042610.7045810.2187920.9248680.4421410.9093160.0598090.1842870.0473550.6748810.5946250.5333100.0433240.5614330.3296680.5029670.1118940.6071940.5659450.0067640.6174420.9121230.7905240.9920810.9588020.7919640.2852510.6249170.4780940.1956750.3823170.0538740.4516480.9820050.1239430.1193810.7385230.5873040.4716330.1071270.2292190.8999650.4167540.5358520.0062090.3006420.4368930.6121490.9181980.6257370.7059980.1498340.7460630.8310070.6337260.4383100.1525730.5684100.5282240.9514290.4803590.5025600.5368780.8192020.0571160.6694220.7671170.7081150.7968670.5577610.9658370.1471570.0296470.5938930.1140660.9508100.3257070.1936190.4578120.9204030.8790690.2526160.3480090.1825890.9017960.7065280.7266580.9000880.7791640.5991550.2911250.1513950.3351750.6575520.0733430.0550060.3231950.5904820.8538990.2870620.1730670.1340210.9946540.1794980.3175470.5682910.0093490.9006490.9772410.5568950.0847740.3330020.7284290.1424350.5524690.2730430.9744950.6677870.2556530.1083110.7761810.7824780.7616040.9144030.6586230.5683680.2017560.6982960.9521950.8899630.9935670.8187040.5451220.4512540.8905570.9732650.5934110.3660740.3230950.8714230.2156340.7349450.3656190.8016030.7827360.7013550.6227770.4936830.8405380.7120970.4439090.0310350.3632400.7307220.4755670.3444170.6408800.1262050.1714650.7370860.1270290.3696500.6043340.1031040.8023740.9455530.9790390.8812320.6276820.9304870.7247900.7166780.0410790.4394820.2820700.3349960.0835270.7608490.5092720.6610470.6303140.3709270.4467400.4151080.4803890.9833240.3734240.0124070.9219030.8732760.3517470.6301330.3578270.2128200.2233190.4195640.0729100.6508390.7556860.9321010.3764040.2969270.3719440.8276990.9011400.4273040.0021890.0419650.1417870.5982370.1064610.2971590.0583920.6213250.0226800.8550550.3063370.7587830.5632750.0381640.5655170.6588730.1832660.7974110.6123670.5556530.6294920.6861800.2403830.7879280.8564770.7277960.6923450.4723510.8568730.6647430.3333760.5181450.3946890.0310500.57684050.8502520.9514890.6829790.0761630.4100780.0419010.1741870.5654020.1728910.5666100.5140040.8688710.7203470.9035910.2776150.2566650.6997030.9015690.9836920.6409130.3300070.6066750.8221600.6279650.1179230.2858780.9867470.4318010.5742340.5272420.1976530.1657080.5009900.9902040.3939920.7140130.1759040.0706960.1576230.1615580.2836370.5367060.7732390.8803440.6373910.8526020.5979550.6938470.5371770.8892300.0522660.7831840.1451730.0586980.0589950.0519710.5159860.4049960.9996500.1085700.3273840.9978300.4011140.8833360.5691390.9532410.8853870.7792860.0318080.9626190.5196350.2077910.8750980.2241510.1386590.7252390.9737890.5354820.4447980.0221110.6059880.9647970.9675910.9300290.1846800.6229620.4129630.3630610.0366030.8681490.6728280.0874720.8869660.7824650.3173040.8183140.5075550.0211930.4335220.4463130.2388200.8302460.7447640.5864790.4928680.4873560.2667410.6050110.7535440.2705840.5223030.0983290.7136370.8840410.5670540.9944820.1787400.0122000.4569980.9317520.8460250.4733300.9025550.2259960.3041540.7149940.7240910.0186760.2858130.5804860.9307870.3389970.1200830.5162730.6992070.2986410.8616100.9058070.7685830.2612320.9384560.9386420.7450450.9107350.2372250.4949670.8098780.9545660.6374830.9108500.6921370.0429430.8335870.3699490.9365570.4830530.1253320.9644540.0170260.6765710.1404400.1553130.6495580.9816540.6948070.7619740.4252090.1388930.2161860.7647000.0546060.4901230.1806900.6309250.5512420.5670810.8183450.9389870.1929010.7128440.6979400.2582960.9158080.5323580.5579640.3227720.3386310.3323020.9788180.2032150.6670260.5747840.0519750.5428380.2079490.0910900.8698560.0273700.9686250.3275000.4102780.1355440.1270660.4139840.6174440.1322670.9744810.1806350.7603500.4821620.7680790.3028980.0151750.4633690.7033370.0660410.6690110.3454790.5917210.2290530.2675680.9328270.8261450.1454430.6470040.8839510.7413610.5157110.1352520.0398840.8343220.1170580.5231020.8730580.0387080.6923910.4762680.1692640.0631720.0322240.2424310.1127070.4049900.0420730.9265650.9352630.8104270.1883430.5873180.903511100.9673420.0675390.3175860.6968630.6402650.6326230.8287510.7131100.3354800.1676950.7646900.3593150.0880050.7295680.8810500.0412640.1748680.4200970.3406070.0447690.1128910.4380410.0710730.8162450.8661160.1311200.1932190.4374930.9709250.7738610.1317760.9271800.8277970.3927320.1405710.0521360.0927290.8869900.1681510.3628870.6542820.6048220.3949930.3746050.0630730.4289590.7197210.7680310.1943680.8628710.0287220.9021290.2365460.1285840.3178760.8487610.8322980.3456510.2399710.5357880.2714670.0344310.6270860.5202000.0419560.7574730.9883500.8014010.3439810.3353910.3515710.4429460.5794930.6175610.1771170.8649820.7102710.1706320.8550830.0198570.6416840.1623340.2575500.3426060.0061440.4385510.7970440.6756650.5450230.5642270.0139840.6089770.9885660.2241070.5872180.8350580.7924980.3292280.3572030.9601130.2237960.2115370.1539150.4958960.2612950.5556860.0729160.3785170.4610890.7785400.7204430.2312040.0245440.2573650.4101160.0083990.4715490.8524220.9231800.2314360.9909550.0059980.3352510.7892580.0985120.4423320.3806690.8325980.7119620.3550010.0246360.6935130.4212120.8583490.5750740.9400620.8802560.4581450.0173100.0686550.3206080.5923020.6100520.5124280.1447560.8768090.7382350.3733200.8286860.0902400.0640030.5058970.9334270.1489620.5694790.0597490.1692780.0690280.9060220.4474100.1063850.6652600.5022730.4092060.7279930.1720900.3844610.2670010.3082210.1773030.4825580.6297170.0595530.1968870.2792080.7078210.3274290.4252940.7047680.1590770.4394040.6293300.9250580.3569880.8191330.6401430.7150520.7384230.9710000.2460490.0425820.2477970.0450170.7054620.8347070.9341360.4707930.5123600.2629800.0988970.7988460.1428030.8056660.8795260.4999200.9142520.4124130.2003740.6435710.3998650.6142310.4373110.5931040.9514400.5531200.9037350.1385050.0015440.6128340.5417030.7806310.8214940.9534100.6099560.2900820.8268080.3760170.6091770.6200220.4415000.1987720.4439540.9247670.6714340.0815230.5726590.1206620.9999640.9973800.6947120.3738800.4015110.9250990.2448590.6640130.4812210.0349440.1108950.8953690.892101150.9151610.3074020.2984200.6592520.5951380.9843690.6156220.1547160.8314690.8156970.0920420.8877900.0164390.8639300.8987710.8503670.6528690.3287550.5453790.2379350.8717850.1767340.3706360.7595850.3061840.6161390.2192910.8725240.6478650.4730030.2341900.8338220.0985160.2050270.2347310.5969740.6284680.9826510.1763170.7999770.1552640.3609920.9550750.1301580.2728470.8091180.9656570.0924220.5834240.4054260.8993240.0847830.1811360.0506650.3012790.1901840.0086050.1275230.1128650.7304140.2393750.9015700.3285140.8740430.2365300.3784550.5422910.5157940.4138020.3888670.3403310.4022350.4571100.3829910.6580900.8812140.5726730.4280400.2583780.8652690.1732780.5799530.7273770.7304020.4039960.1179270.7635950.5336610.7220350.5976320.4228660.9334860.9939710.5478390.7302350.0279840.8635910.9986310.3253100.8502610.6114920.4411130.5272050.7798210.2513500.4628920.8471870.6780240.3678620.1379830.4028090.9312970.6731630.4192300.5686670.0417110.9956340.6190040.7794660.9313390.3416190.8112990.8727100.6659880.5887870.8933520.4485840.2443840.8141730.6971490.4096050.0057150.9540140.6052530.0065460.4984930.5986350.3001470.4617570.4956000.5856590.8696380.8322270.7785630.5484330.6134520.7342700.5663690.7752100.9865460.7892060.6143360.1582850.4763860.2836490.1538120.7772200.0589630.0791230.6642380.5233450.4464070.3068300.0172850.2935270.2677940.0367260.8040400.3338000.8918870.8103530.9641010.9577140.5911660.3115030.9184210.0506500.2257430.3627060.4122560.9446210.5793830.1867600.5964850.9337560.1341380.9822840.1538760.8207280.6775360.4040890.2172870.7735850.8476960.5297000.4096000.0183080.1486600.8043350.4197780.3392180.2971460.7179860.9978070.5519020.8898960.1684680.8835640.7027760.4221090.8633150.2350430.5637510.0265600.3800510.3618350.0912440.0452540.3424520.3554140.9958380.9442050.1311440.5183550.4772450.4011060.9306140.5153360.8095820.8817720.7626680.4644210.0739200.4704990.2005720.2085670.1775400.3542410.9117530.4335210.1782960.7521620.0504710.0826690.0385830.4652790.6038290.1817890.5190300.710153200.9249800.5291320.9711860.5472530.0064380.4727770.5220880.1522090.6542340.1304440.4541490.9823610.1325140.0602790.0678720.0961460.8193420.6296070.7836310.0771350.9719980.8828340.6304330.9290730.3981270.4889260.6858030.2815970.6767000.4957320.7911420.2188340.7461960.4541110.4082620.9226430.1507210.1309830.3818900.5419870.9443700.3232590.0974420.9473870.8570350.0260370.1178940.8293760.1384740.5694710.4777750.3066860.0566070.9368350.9040040.7222350.6540780.3922100.9736750.7110080.4930480.3264410.1158530.9720610.2744280.1874500.2148950.7608540.9448890.5015510.6620840.3474040.1285700.6812960.3912830.0262040.5006000.5281270.2232680.0016900.4465630.5053260.0810190.8227400.2401950.0096900.2250420.0841900.8259650.7147670.2445560.0638370.4267540.7060420.7181540.1934340.7400480.9567300.5158940.4300450.0230370.5073260.6865620.1381750.6724230.0393110.4317770.8542760.4223280.0884490.0566950.1032730.4853160.6746570.0908950.8322120.7764640.0611630.3224180.3771400.5022650.4499650.4673620.1047140.3737510.0217720.4483380.8931150.7758810.2491610.7315450.3151380.4961300.0504260.5825200.2825940.2933360.1260540.8931810.4512730.6508710.8093050.2829630.2733820.3857370.4216080.9321650.8676330.4451740.0291890.1071980.1962590.2382360.8245100.8964860.0278870.5733010.8056430.4205120.5252670.2861430.6586460.0903000.8222650.8074140.1916170.6383030.8324650.4698270.5803530.1089030.4624190.8863520.4649830.4662150.1960050.0387290.6158620.5646310.1082310.7416330.1834820.3486410.4804810.6439260.6216260.7611930.2866510.4923850.6819100.5099700.7682110.2999360.5468810.2446450.4435970.8100140.1038670.2299850.7917640.5665410.8274170.9749340.9652240.9497030.5517870.0925360.7404310.5642970.7458580.4553900.1556630.9959530.8896910.8037490.4114760.2131570.2944400.6229310.2482170.7996810.0188820.4803510.7340150.6835680.4212830.6932030.3671980.2324410.7850510.7267050.7064570.0317420.2394390.3710580.5818790.0879890.2321290.8125160.7527400.8047770.2944430.7806190.2247860.2898490.8489820.7441660.3322530.5673340.699648250.0919350.0538020.8627960.6299700.8696530.0876910.8659040.0412480.9581740.2273680.7518620.4007490.4030750.2532870.5068380.1845600.0570840.3669270.7198370.4741750.9226200.1701220.7183820.8697980.1293100.9740560.7542160.9166770.0377030.2858490.8229080.2747340.0517240.1479430.0164930.7824170.3491220.7496780.0549530.0781980.7893410.3704380.1109620.4052160.6512990.4362880.2024750.4438900.7421480.0960150.5117210.8436160.9935440.6307560.2854730.1260220.0133560.3381560.4843200.5461910.0360390.0612240.0231610.5073890.2665910.4453060.9736090.7977760.2841260.5557860.1975880.7906010.0876200.8379250.2412630.4342620.4278620.1021170.9088140.1490110.9075680.0104540.1735340.8391390.3102550.8489860.9113440.8999010.8672290.8377620.2536430.8430080.6176280.9841770.6105400.6516980.6544650.5523270.1857300.9809760.0959370.7482630.5246180.2907600.2281910.5037390.9319570.5741300.9897450.4297730.4163560.6278770.9073680.0694230.5504030.1546060.7185610.1354070.0045460.5375160.0165930.5355150.0456920.1539790.8430100.7979290.3035940.3984930.4492940.0205350.6175540.8045550.2341720.4001280.4933140.5018810.3334880.1054200.6408080.1514690.1951770.0573910.9147450.8306700.1590040.8677070.5072980.1283010.1131180.2227980.8898370.4509220.6972100.3902900.6226240.7164480.5008590.7600810.8326040.5599440.5438530.5879720.0251540.9891020.0707740.8176150.1370360.1530200.3592420.2534620.2187640.5633830.4800620.0930470.5139070.1974180.4802700.2770200.0104840.8877810.3738980.4162170.8775830.7607710.5481120.9023410.9207010.4654410.5656470.0285580.1403310.3999980.8270980.4553910.6935830.6379800.6558830.7693190.0002270.3006950.6954710.0908870.5236480.8945950.9637060.7056880.7640290.3446080.2518500.6672090.1493050.4709170.0222920.7597110.2056570.8405900.7748540.1761640.8845590.0930640.2021880.3724050.1736560.3452340.5677380.1569520.7757400.0336620.0109590.3201230.5267270.6297270.8815140.6411390.0226880.6951900.8846520.7513940.2630780.5075740.1241910.6087260.6933130.0777860.4040810.5134870.8070660.0989660.4725320.567342300.5359840.1842630.5867830.7859110.9980690.0527300.0426790.5289340.6033790.0822530.7098160.1161380.1973020.7792850.4558790.3088500.3676760.7633080.1804470.1377430.5008610.0149030.3472940.3377370.5046310.4923790.3502960.2023900.1599440.5610170.4461760.8552990.9111810.8765650.9654290.2023110.7263760.5713180.5839270.4567220.5821110.0677480.0059510.4680120.7902280.9229490.7202410.1259310.7190920.6154940.3867180.7852140.1013440.4029380.7423970.0124340.9281830.4602230.0696250.9099310.1233040.8947820.2050600.9319890.4979290.3600040.1608710.7263760.8352750.3188790.9780650.4727110.5661440.1466840.0997440.0865170.7530140.7422890.3155060.8453140.5345470.8325700.1860340.2759530.5830580.9299510.0572950.8182970.1646230.4397060.3665210.0657790.7878090.5123880.1768390.5208310.5910090.4445390.1430080.9612640.4177700.1300250.6509930.5879050.2547380.2874330.8052790.0562080.2781920.9367110.8384070.9081740.5946170.7735010.3441000.3838670.3779410.4259640.7068980.2441710.1777810.2164650.7243900.6314870.7945350.7857190.1449770.0874880.2567570.6401170.5697080.8893430.9181420.1523850.1189850.0100540.2798390.1229220.4576360.1884010.0076110.8963380.9975530.8060040.0301680.5353230.6100010.8514240.4099580.2613970.7547910.7910760.7872900.9477820.4002080.6582160.5256360.4181430.4075350.3712690.8438940.4708260.5849000.1657180.5471580.5388190.5076300.6430810.5446840.3182540.7881910.5644250.4155790.3279310.9206480.9304690.0106920.0930820.1340890.6273370.2616920.5825430.5549020.2003410.6570800.9432070.3567680.0318010.4735270.0674580.4992540.6921440.0761180.2298300.4973680.5427910.4949350.8012830.3079590.1140200.5602810.7691090.4351870.0835340.5985940.8460640.1832560.9129690.1036520.1268240.0040740.7845890.5604780.6605200.5218220.7957460.2516440.5304820.0563040.1373330.1419880.6637680.1078400.7570350.4542550.6941280.3567270.7680180.8173280.9267440.2070380.6009570.0344780.2929900.0423010.0972680.3009170.0602670.5831780.7244170.7507680.6980990.1133590.2869590.2621450.0662280.0021770.2693440.0338110.467862350.5408270.2047750.2287980.6533310.0196220.2994160.4804160.5212740.9589980.7606800.8034260.3295610.3767790.6870570.7072530.1108460.2677530.0305330.4009940.0061220.6688720.8688990.3758230.2811100.1013510.8057160.4842740.8766750.1522220.2197160.7332710.0655210.8155110.8597730.0316110.0668030.4730450.2056180.2254440.4343790.2329100.8284090.8914420.8143070.9613000.6638470.0459570.8755540.9429500.9691060.7493010.9327360.0884120.8343470.7313810.9614460.1146670.2634810.1974280.1455890.2695520.0839050.1957440.9475190.9697370.0038970.1842930.9279660.6865000.8147940.9900790.0806900.5735120.8665180.2464050.5933350.9758620.1938110.2929370.4964200.6537520.6066130.7078740.3142010.4458540.3010900.4372840.0507750.9535110.1788930.0767530.9367870.8358250.9726190.8231750.0331040.5694970.1475150.5800540.0834910.4552870.8591570.3255760.3375950.9181370.6372030.1659510.8741230.0955670.5901230.6327500.3326550.1611800.6407750.3877400.8165280.2293200.6651160.5756070.0793720.0423220.8570220.1475420.1030780.1063490.2932520.5301170.8663260.8687840.3565030.0981270.4382270.9474860.0415950.3108480.7822870.7955100.8525810.5438910.5906290.1391080.0347240.1552600.7569190.3687820.2828120.2417990.8713520.3670940.8880360.9498790.8727590.3720450.6458570.4326910.7306000.6989450.4302680.4038720.6011060.6488860.1140800.7053510.3597290.4229260.3135420.5683060.0335650.9768400.9705890.2822730.6578210.7770860.9454950.6681080.4505200.8402530.8225210.9301830.7020580.8006250.9589450.3858840.7325100.1285250.2053340.8831820.6958240.7109030.3697910.5782740.0838090.5941830.1769730.4208670.2398740.1275280.8956020.9100760.6288790.1575610.7305900.9890960.7797480.9795350.8049480.4197330.4650540.7563950.8101500.1413430.0811620.3575510.9687920.6917280.0226320.2128220.7262470.6732180.0383960.3586870.7511410.8656720.9542170.8795650.8659620.7120570.6637710.3847950.1793040.7445320.7670240.1565470.0023020.5216880.7585710.4411690.8885580.5293150.8849920.4526770.6839130.0612000.7405370.6725970.0103960.6732220.7899250.8307750.994382400.5670550.8615620.0911710.1689130.3306070.9547290.1036980.4972330.9890880.6155340.5569420.2374310.4683760.4688480.5631280.4116600.1775370.1523290.7496350.4888560.7292770.4249680.7256260.5880080.1370710.5647470.2944750.6339950.6432350.5525020.3900210.2671220.8021660.8996360.1015880.4537800.3940230.6716430.5537940.8429090.0059650.6459080.1148570.7563750.2280410.2185380.4113400.0684950.4687520.3544380.0827020.1358290.9127070.8719280.7836640.7148360.6703390.6341910.3906740.7666540.2217100.7067570.3045540.7837220.6059980.8717480.0859380.5456520.9155450.6126610.5593320.3289050.8326040.2451670.2880220.6026080.3893000.4123110.5328530.8759270.7643140.8517180.8399690.5470910.5447420.0088150.6197030.9123700.2125960.3221510.2616460.8933620.8224510.2616510.2068840.5263700.0174290.4173860.0890230.9872800.9494890.7521570.6402640.2083780.1678660.1437200.4528460.0044710.3147810.6104550.9938930.5215800.1026770.0523930.3285840.5597620.8377540.0561140.8949200.8958000.4522580.9049280.1531900.7364610.4747040.9741070.5256820.8581110.8188300.8385570.1883830.9248690.7063190.0587880.8836310.3377290.9958010.4620740.8109190.7034230.9606800.7149310.4952140.9747450.7269470.4968950.3306210.3692280.0768640.7676580.9272810.4226650.5208930.0089540.4303270.1530620.4813890.5022240.4911420.7930160.0495010.7163410.5512660.3444680.9414140.7533130.8012080.6270310.5482850.7176080.5288460.5193110.2389710.7295450.9960750.6718740.5713390.3570990.0642730.7078200.2347930.7852740.3599550.0713220.7852050.3885020.3567670.5160930.8325680.3016450.5413660.3230270.1248230.1693840.0805940.4630170.8586330.1043550.8529880.5971230.9155840.1777160.9821030.6543840.1420180.0867050.4045650.4265880.8277470.6505770.0188830.9046820.5304500.0799200.6246700.4659730.3169800.2766350.9482450.7167800.2582650.3489110.4707680.0381670.2990570.3717660.6216700.0269730.9195480.2388660.1374100.9374220.7562190.9528880.8596590.8637230.4054970.6965080.1234940.9826200.2934090.0563640.9345410.6487800.3455820.5433100.8774910.8705950.9765890.981365450.4358960.0843000.3771240.9452980.5208450.7856900.8615670.4293750.6854400.2647640.1950040.8205170.5202840.3934950.0410820.1209450.6974980.3263770.3058870.8746240.8647120.7347660.2771360.3575380.2773510.6409940.1218150.3721800.4505320.3905610.8156840.9736440.9206140.0989800.1184540.9804590.1444180.9032130.7459150.3484590.0093440.6612470.7761380.9266180.1094160.0698860.8878430.7059020.2158220.7225140.9790470.0602940.7149180.1789700.8620100.6292780.3150530.2339320.6829520.6343710.5939900.6532780.1591690.8892910.4298080.9236760.4592040.6295890.1218090.2306980.0204910.0829250.1620640.3472800.3835000.7166140.4807500.8727870.5080410.9830350.4262150.5220110.9467440.5148180.3752330.5571680.4552690.9445960.7470600.5927560.9345950.8579460.6244520.7671870.7984260.7964310.0905900.0256600.3540530.2828020.0557170.0678240.1016800.6166960.2020650.8798320.9970080.7837190.7251940.2872130.7171010.6507330.1348640.0648110.3051910.6940900.5887570.7173070.8739500.8053250.0988740.3069140.3087380.6401910.3340830.2152300.0184010.8629860.1112050.2520690.9924700.8278760.2450560.0535510.2132060.6424790.7661730.7510220.0559330.6186490.3463200.1818440.4190660.2787520.0492160.8337840.8138030.3469550.7517570.0191380.7184950.1409490.6555430.4352200.7680170.2094110.3377960.5065790.2912000.6716360.0253660.3238170.0928970.1842270.8293700.0761550.5215680.9840660.4242390.6358260.4355680.5528290.7848890.1625030.5828400.5704190.1550160.2488840.1980530.2867890.5777200.5757230.7721010.4098070.6669050.8683160.3295130.0535480.7940440.6433820.4460250.0134210.3848980.0517980.5305310.2095150.2737660.2119400.3267140.0000430.2668030.9501260.4178660.5449100.6108010.0502810.1216250.5062970.5974270.2741210.0291520.8806730.9116680.0904840.5993090.6881650.0576130.0846100.3333170.5905750.9286700.1054240.3893760.7546460.7898280.0101460.5595520.4810310.3036890.9907860.6153700.8357260.4597650.6324200.8442620.6710210.1468770.1802280.9110610.6446610.9124710.5534640.7680970.0438430.3278120.9216820.5819010.7211120.6171710.759219

Explicit Locators

For the ultimate flexibility in positioning tick locations and labels, you can use the toyplot.locator.Explicit locator. With it, you can specify an explicit set of labels, and a set of \([0, N)\) integer locations will be created to match. This is particularly useful if you are working with categorical data:

[11]:
fruits = ["Apples", "Oranges", "Kiwi", "Miracle Fruit", "Durian"]
counts = [452, 347, 67, 21, 5]

canvas, axes, mark = toyplot.bars(counts, width=400, height=300)
axes.x.ticks.locator = toyplot.locator.Explicit(labels=fruits)
ApplesOrangesKiwiMiracle FruitDurian0100200300400

Note that in the above example the implicit \([0, N)\) tick locations match the implicit \([0, N)\) X coordinates that are generated for each bar when you don’t supply any X coordinates of your own. This is by design!

You can also use Explicit locators with a list of tick locations, and a set of tick labels will be generated using a format string. For example:

[12]:
x = numpy.linspace(0, 2 * numpy.pi)
y = numpy.sin(x)
locations=[0, numpy.pi/2, numpy.pi, 3*numpy.pi/2, 2*numpy.pi]

canvas, axes, mark = toyplot.plot(x, y, width=500, height=300)
axes.x.ticks.locator = toyplot.locator.Explicit(locations=locations, format="{:.2f}")
0.001.573.144.716.28-1.0-0.50.00.51.0

Finally, you can supply both locations and labels to an Explicit locator:

[13]:
labels = ["0", u"\u03c0 / 2", u"\u03c0", u"3\u03c0 / 2", u"2\u03c0"]

canvas, axes, mark = toyplot.plot(x, y, width=500, height=300)
axes.x.ticks.locator = toyplot.locator.Explicit(locations=locations, labels=labels)
0π / 2π3π / 2-1.0-0.50.00.51.0

Timestamp Locators

Toyplot includes the toyplot.locator.Timestamp locator which can be used to provide human-consumable date-time labels when your data’s domain is timestamps (seconds since the Unix epoch, i.e. midnight, January 1st, 1970, UTC). As an example, let’s load some real-world data containing date-time information:

[14]:
import toyplot.data
data = toyplot.data.commute()
data[:6]
[14]:
DatetimeNameValueUnits
2014-05-01T14:08:53.587607ZStatus Since DTC Cleared0011110100110
2014-05-01T14:08:53.656972ZFuel System Status0000
2014-05-01T14:08:53.726403ZCalculated Load Value0.0%
2014-05-01T14:08:53.734393ZCoolant Temperature13C
2014-05-01T14:08:53.804349ZShort Term Fuel Trim0.0%
2014-05-01T14:08:53.873862ZLong Term Fuel Trim-3.125%

Our first step will be to convert the string datetimes from the file into true numeric timestamps. Note that for this example we’re using Arrow, a library that improves upon the builtin datetime functionality in Python. Note that Arrow is used in the timestamp locator implementation, so it must already be installed for the following examples to work:

[15]:
import arrow
timestamps = numpy.array([arrow.get(datetime).timestamp() for datetime in data["Datetime"]])

Now, we can plot the data using the timestamps as our independent variable:

[16]:
observations = numpy.logical_and(data["Name"] == "Vehicle Speed", data["Value"] != "NODATA")
x = timestamps[observations]
y = data["Value"][observations]
[17]:
canvas, axes, mark = toyplot.plot(
    x, y, label="Vehicle Speed", xlabel="Time", ylabel="km/h",
    width=600, height=300)
1398953000139895400013989550001398956000Time04080120km/hVehicle Speed

As you would expect, the timestamps make very unfriendly tick labels. Let’s use the timestamp locator instead:

[18]:
canvas, axes, mark = toyplot.plot(
    x, y, label="Vehicle Speed", xlabel="Time", ylabel="km/h",
    width=600, height=300)
axes.x.ticks.show = True
axes.x.ticks.locator = toyplot.locator.Timestamp()
5/1 14:105/1 14:155/1 14:205/1 14:255/1 14:305/1 14:355/1 14:405/1 14:455/1 14:50Time04080120km/hVehicle Speed

By default, the timestamp locator chooses a “good” time interval based on the data domain - for this data, it chose to create ticks at five minute intervals. In addition, the locator also chooses a default format based on the interval - in this case, month/day hour:minute. This is a significant improvement over raw timestamps, but before we continue, we need to address timezone issues.

As alluded to above, in Toyplot all timestamps must always be UTC timestamps, without exception … that is to say that the numeric values to be displayed by the timestamp locator must be the number of seconds since Midnight, January 1st, 1970, UTC time. Our current example data already contained UTC datetimes (note the “Z” timezone in the data file), so no special conversion was necessary. If for some reason you’re working with datetimes that aren’t UTC, you’ll need to take their timezone into consideration when converting them to timestamps for display by Toyplot … and as an aside: you need to run, not walk to anyone collecting data using local timestamps, and make them stop!

Since all Toyplot timestamps are UTC, the times displayed by the timestamp locator are also UTC by default. If you prefer to display times using a specific timezone, you can specify it when creating the locator.

An Important Note on Timezone Names

The arrow library (and thus Toyplot) relies on the underlying operating system for its timezone information. The following examples use “US/Mountain” to identify a timezone, and should work on most posix-like systems including Linux and OSX. Unfortunately, Windows operating systems use a different naming scheme; if you are using Windows, you will need to substitute a different name, such as “US Mountain Standard Time”.

[19]:
canvas, axes, mark = toyplot.plot(
    x, y, label="Vehicle Speed", xlabel="Time (US/Mountain)", ylabel="km/h",
    width=600, height=300)
axes.x.ticks.show = True
axes.x.ticks.locator = toyplot.locator.Timestamp(timezone="US/Mountain")
5/1 08:105/1 08:155/1 08:205/1 08:255/1 08:305/1 08:355/1 08:405/1 08:455/1 08:50Time (US/Mountain)04080120km/hVehicle Speed

Note that the hour in this data changed from 14:00 to 8:00, which makes more sense, since this data came from a morning commute. You could also specify a timezone of “utc” (the default) to explicitly document in the code that no timezone conversion is taking place. Finally, you may use “local” to automatically display the data using whichever timezone is local to the host running the code … but be careful with this option, since it means that the the content of a figure could change based on where your laptop was when you generated it!

With timezones out of the way, let’s focus on the labels. The current set is a little crowded, so let’s look at different ways to clean things up. First, we can use the count attribute to request that the locator choose an interval that produces a specific number of ticks:

[20]:
canvas, axes, mark = toyplot.plot(
    x, y, label="Vehicle Speed", xlabel="Time (US/Mountain)", ylabel="km/h",
    width=600, height=300)
axes.x.ticks.show = True
axes.x.ticks.locator = toyplot.locator.Timestamp(timezone="US/Mountain", count=5)
5/1 08:105/1 08:205/1 08:305/1 08:405/1 08:50Time (US/Mountain)04080120km/hVehicle Speed

Note that in most cases the locator won’t produce the exact number of ticks requested (we got lucky in this case), it will choose an interval that produces the closest match.

Alternatively, we might choose to accept the default tick count and alter the tick format, to save space:

[21]:
canvas, axes, mark = toyplot.plot(
    x, y, label="Vehicle Speed", xlabel="Time (US/Mountain)", ylabel="km/h",
    width=600, height=300)
axes.x.ticks.show = True
axes.x.ticks.locator = toyplot.locator.Timestamp(
    timezone="US/Mountain", format="{0:HH}:{0:mm}")
08:1008:1508:2008:2508:3008:3508:4008:4508:50Time (US/Mountain)04080120km/hVehicle Speed

The individual tick values are passed to the format function as arrow.arrow.Arrow objects, so you can use any of the arrow attributes and formatting tokens in the format string, as we’ve done here.

Or, we might force a specific interval if it had special meaning for our domain, such as the number of seven-minute workouts we could have completed during our commute:

[22]:
canvas, axes, mark = toyplot.plot(
    x, y, label="Vehicle Speed", xlabel="Time (US/Mountain)", ylabel="km/h",
    width=600, height=300)
axes.x.ticks.show = True
axes.x.ticks.locator = toyplot.locator.Timestamp(
    timezone="US/Mountain", interval=(7, "minutes"))
5/1 08:105/1 08:175/1 08:245/1 08:315/1 08:385/1 08:45Time (US/Mountain)04080120km/hVehicle Speed

Toyplot supports common intervals in both singular and plural forms from seconds to millennia.

Finally, we might go in the opposite direction and show more detail in the timestamps. This is often a case where you’ll want to adjust the orientation of the labels so they don’t overlap (note in the following example that we had to make the canvas larger and position the axes manually on the canvas to make room for the angled labels - and we manually placed the x axis label to avoid overlap):

[23]:
canvas = toyplot.Canvas(width=600, height=350)
axes = canvas.cartesian(bounds=(80, -80, 50, -120), label="Vehicle Speed", ylabel="km/h")
axes.plot(x, y)
axes.x.ticks.show = True
axes.x.ticks.locator = toyplot.locator.Timestamp(
    timezone="US/Mountain", format="{0:MMMM} {0:d}, {0:YYYY} {0:h}:{0:mm} {0:a}")
axes.x.ticks.labels.angle = 30
canvas.text(300, 320, "Time (US/Mountain)", style={"font-weight":"bold"});
May 4, 2014 8:10 amMay 4, 2014 8:15 amMay 4, 2014 8:20 amMay 4, 2014 8:25 amMay 4, 2014 8:30 amMay 4, 2014 8:35 amMay 4, 2014 8:40 amMay 4, 2014 8:45 amMay 4, 2014 8:50 am04080120km/hVehicle SpeedTime (US/Mountain)