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:
import numpy
x = numpy . arange ( 20 )
y = numpy . linspace ( 0 , 1 , len ( x )) ** 2
import toyplot
canvas , axes , mark = toyplot . plot ( x , y , width = 300 )
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:
canvas , axes , mark = toyplot . plot ( x , y , width = 300 )
axes . x . ticks . locator = toyplot . locator . Extended ( only_inside = True )
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:
canvas , axes , mark = toyplot . plot ( x , y , width = 300 )
axes . x . ticks . locator = toyplot . locator . Uniform ( count = 5 )
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:
canvas , axes , mark = toyplot . plot ( x , y , width = 300 )
axes . x . ticks . locator = toyplot . locator . Uniform ( count = 5 , format = " {:.2f} " )
0.00 4.75 9.50 14.25 19.00 0.0 0.5 1.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:
canvas , axes , mark = toyplot . plot ( x , y , xscale = "log10" , width = 300 )
If you don’t like the “superscript” notation that the Log locator
produces, you could replace it with your own locator and custom format:
canvas , axes , mark = toyplot . plot ( x , y , xscale = "log10" , width = 300 )
axes . x . ticks . locator = toyplot . locator . Log ( base = 10 , format = " {base} ^ {exponent} " )
Or even display raw tick values:
canvas , axes , mark = toyplot . plot ( x , y , xscale = "log2" , width = 300 )
axes . x . ticks . locator = toyplot . locator . Log ( base = 2 , format = " {:.0f} " )
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:
numpy . random . seed ( 1234 )
canvas , table = toyplot . matrix ( numpy . random . random (( 5 , 5 )), width = 300 )
0 1 2 3 4 0 0.191519 0.622109 0.437728 0.785359 0.779976 1 0.272593 0.276464 0.801872 0.958139 0.875933 2 0.357817 0.500995 0.683463 0.712702 0.370251 3 0.561196 0.503083 0.013768 0.772827 0.882641 4 0.364886 0.615396 0.075381 0.368824 0.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:
canvas , table = toyplot . matrix ( numpy . random . random (( 50 , 50 )), width = 400 , step = 5 )
0 5 10 15 20 25 30 35 40 45 0 0.651378 0.397203 0.788730 0.316836 0.568099 0.869127 0.436173 0.802148 0.143767 0.704261 0.704581 0.218792 0.924868 0.442141 0.909316 0.059809 0.184287 0.047355 0.674881 0.594625 0.533310 0.043324 0.561433 0.329668 0.502967 0.111894 0.607194 0.565945 0.006764 0.617442 0.912123 0.790524 0.992081 0.958802 0.791964 0.285251 0.624917 0.478094 0.195675 0.382317 0.053874 0.451648 0.982005 0.123943 0.119381 0.738523 0.587304 0.471633 0.107127 0.229219 0.899965 0.416754 0.535852 0.006209 0.300642 0.436893 0.612149 0.918198 0.625737 0.705998 0.149834 0.746063 0.831007 0.633726 0.438310 0.152573 0.568410 0.528224 0.951429 0.480359 0.502560 0.536878 0.819202 0.057116 0.669422 0.767117 0.708115 0.796867 0.557761 0.965837 0.147157 0.029647 0.593893 0.114066 0.950810 0.325707 0.193619 0.457812 0.920403 0.879069 0.252616 0.348009 0.182589 0.901796 0.706528 0.726658 0.900088 0.779164 0.599155 0.291125 0.151395 0.335175 0.657552 0.073343 0.055006 0.323195 0.590482 0.853899 0.287062 0.173067 0.134021 0.994654 0.179498 0.317547 0.568291 0.009349 0.900649 0.977241 0.556895 0.084774 0.333002 0.728429 0.142435 0.552469 0.273043 0.974495 0.667787 0.255653 0.108311 0.776181 0.782478 0.761604 0.914403 0.658623 0.568368 0.201756 0.698296 0.952195 0.889963 0.993567 0.818704 0.545122 0.451254 0.890557 0.973265 0.593411 0.366074 0.323095 0.871423 0.215634 0.734945 0.365619 0.801603 0.782736 0.701355 0.622777 0.493683 0.840538 0.712097 0.443909 0.031035 0.363240 0.730722 0.475567 0.344417 0.640880 0.126205 0.171465 0.737086 0.127029 0.369650 0.604334 0.103104 0.802374 0.945553 0.979039 0.881232 0.627682 0.930487 0.724790 0.716678 0.041079 0.439482 0.282070 0.334996 0.083527 0.760849 0.509272 0.661047 0.630314 0.370927 0.446740 0.415108 0.480389 0.983324 0.373424 0.012407 0.921903 0.873276 0.351747 0.630133 0.357827 0.212820 0.223319 0.419564 0.072910 0.650839 0.755686 0.932101 0.376404 0.296927 0.371944 0.827699 0.901140 0.427304 0.002189 0.041965 0.141787 0.598237 0.106461 0.297159 0.058392 0.621325 0.022680 0.855055 0.306337 0.758783 0.563275 0.038164 0.565517 0.658873 0.183266 0.797411 0.612367 0.555653 0.629492 0.686180 0.240383 0.787928 0.856477 0.727796 0.692345 0.472351 0.856873 0.664743 0.333376 0.518145 0.394689 0.031050 0.576840 5 0.850252 0.951489 0.682979 0.076163 0.410078 0.041901 0.174187 0.565402 0.172891 0.566610 0.514004 0.868871 0.720347 0.903591 0.277615 0.256665 0.699703 0.901569 0.983692 0.640913 0.330007 0.606675 0.822160 0.627965 0.117923 0.285878 0.986747 0.431801 0.574234 0.527242 0.197653 0.165708 0.500990 0.990204 0.393992 0.714013 0.175904 0.070696 0.157623 0.161558 0.283637 0.536706 0.773239 0.880344 0.637391 0.852602 0.597955 0.693847 0.537177 0.889230 0.052266 0.783184 0.145173 0.058698 0.058995 0.051971 0.515986 0.404996 0.999650 0.108570 0.327384 0.997830 0.401114 0.883336 0.569139 0.953241 0.885387 0.779286 0.031808 0.962619 0.519635 0.207791 0.875098 0.224151 0.138659 0.725239 0.973789 0.535482 0.444798 0.022111 0.605988 0.964797 0.967591 0.930029 0.184680 0.622962 0.412963 0.363061 0.036603 0.868149 0.672828 0.087472 0.886966 0.782465 0.317304 0.818314 0.507555 0.021193 0.433522 0.446313 0.238820 0.830246 0.744764 0.586479 0.492868 0.487356 0.266741 0.605011 0.753544 0.270584 0.522303 0.098329 0.713637 0.884041 0.567054 0.994482 0.178740 0.012200 0.456998 0.931752 0.846025 0.473330 0.902555 0.225996 0.304154 0.714994 0.724091 0.018676 0.285813 0.580486 0.930787 0.338997 0.120083 0.516273 0.699207 0.298641 0.861610 0.905807 0.768583 0.261232 0.938456 0.938642 0.745045 0.910735 0.237225 0.494967 0.809878 0.954566 0.637483 0.910850 0.692137 0.042943 0.833587 0.369949 0.936557 0.483053 0.125332 0.964454 0.017026 0.676571 0.140440 0.155313 0.649558 0.981654 0.694807 0.761974 0.425209 0.138893 0.216186 0.764700 0.054606 0.490123 0.180690 0.630925 0.551242 0.567081 0.818345 0.938987 0.192901 0.712844 0.697940 0.258296 0.915808 0.532358 0.557964 0.322772 0.338631 0.332302 0.978818 0.203215 0.667026 0.574784 0.051975 0.542838 0.207949 0.091090 0.869856 0.027370 0.968625 0.327500 0.410278 0.135544 0.127066 0.413984 0.617444 0.132267 0.974481 0.180635 0.760350 0.482162 0.768079 0.302898 0.015175 0.463369 0.703337 0.066041 0.669011 0.345479 0.591721 0.229053 0.267568 0.932827 0.826145 0.145443 0.647004 0.883951 0.741361 0.515711 0.135252 0.039884 0.834322 0.117058 0.523102 0.873058 0.038708 0.692391 0.476268 0.169264 0.063172 0.032224 0.242431 0.112707 0.404990 0.042073 0.926565 0.935263 0.810427 0.188343 0.587318 0.903511 10 0.967342 0.067539 0.317586 0.696863 0.640265 0.632623 0.828751 0.713110 0.335480 0.167695 0.764690 0.359315 0.088005 0.729568 0.881050 0.041264 0.174868 0.420097 0.340607 0.044769 0.112891 0.438041 0.071073 0.816245 0.866116 0.131120 0.193219 0.437493 0.970925 0.773861 0.131776 0.927180 0.827797 0.392732 0.140571 0.052136 0.092729 0.886990 0.168151 0.362887 0.654282 0.604822 0.394993 0.374605 0.063073 0.428959 0.719721 0.768031 0.194368 0.862871 0.028722 0.902129 0.236546 0.128584 0.317876 0.848761 0.832298 0.345651 0.239971 0.535788 0.271467 0.034431 0.627086 0.520200 0.041956 0.757473 0.988350 0.801401 0.343981 0.335391 0.351571 0.442946 0.579493 0.617561 0.177117 0.864982 0.710271 0.170632 0.855083 0.019857 0.641684 0.162334 0.257550 0.342606 0.006144 0.438551 0.797044 0.675665 0.545023 0.564227 0.013984 0.608977 0.988566 0.224107 0.587218 0.835058 0.792498 0.329228 0.357203 0.960113 0.223796 0.211537 0.153915 0.495896 0.261295 0.555686 0.072916 0.378517 0.461089 0.778540 0.720443 0.231204 0.024544 0.257365 0.410116 0.008399 0.471549 0.852422 0.923180 0.231436 0.990955 0.005998 0.335251 0.789258 0.098512 0.442332 0.380669 0.832598 0.711962 0.355001 0.024636 0.693513 0.421212 0.858349 0.575074 0.940062 0.880256 0.458145 0.017310 0.068655 0.320608 0.592302 0.610052 0.512428 0.144756 0.876809 0.738235 0.373320 0.828686 0.090240 0.064003 0.505897 0.933427 0.148962 0.569479 0.059749 0.169278 0.069028 0.906022 0.447410 0.106385 0.665260 0.502273 0.409206 0.727993 0.172090 0.384461 0.267001 0.308221 0.177303 0.482558 0.629717 0.059553 0.196887 0.279208 0.707821 0.327429 0.425294 0.704768 0.159077 0.439404 0.629330 0.925058 0.356988 0.819133 0.640143 0.715052 0.738423 0.971000 0.246049 0.042582 0.247797 0.045017 0.705462 0.834707 0.934136 0.470793 0.512360 0.262980 0.098897 0.798846 0.142803 0.805666 0.879526 0.499920 0.914252 0.412413 0.200374 0.643571 0.399865 0.614231 0.437311 0.593104 0.951440 0.553120 0.903735 0.138505 0.001544 0.612834 0.541703 0.780631 0.821494 0.953410 0.609956 0.290082 0.826808 0.376017 0.609177 0.620022 0.441500 0.198772 0.443954 0.924767 0.671434 0.081523 0.572659 0.120662 0.999964 0.997380 0.694712 0.373880 0.401511 0.925099 0.244859 0.664013 0.481221 0.034944 0.110895 0.895369 0.892101 15 0.915161 0.307402 0.298420 0.659252 0.595138 0.984369 0.615622 0.154716 0.831469 0.815697 0.092042 0.887790 0.016439 0.863930 0.898771 0.850367 0.652869 0.328755 0.545379 0.237935 0.871785 0.176734 0.370636 0.759585 0.306184 0.616139 0.219291 0.872524 0.647865 0.473003 0.234190 0.833822 0.098516 0.205027 0.234731 0.596974 0.628468 0.982651 0.176317 0.799977 0.155264 0.360992 0.955075 0.130158 0.272847 0.809118 0.965657 0.092422 0.583424 0.405426 0.899324 0.084783 0.181136 0.050665 0.301279 0.190184 0.008605 0.127523 0.112865 0.730414 0.239375 0.901570 0.328514 0.874043 0.236530 0.378455 0.542291 0.515794 0.413802 0.388867 0.340331 0.402235 0.457110 0.382991 0.658090 0.881214 0.572673 0.428040 0.258378 0.865269 0.173278 0.579953 0.727377 0.730402 0.403996 0.117927 0.763595 0.533661 0.722035 0.597632 0.422866 0.933486 0.993971 0.547839 0.730235 0.027984 0.863591 0.998631 0.325310 0.850261 0.611492 0.441113 0.527205 0.779821 0.251350 0.462892 0.847187 0.678024 0.367862 0.137983 0.402809 0.931297 0.673163 0.419230 0.568667 0.041711 0.995634 0.619004 0.779466 0.931339 0.341619 0.811299 0.872710 0.665988 0.588787 0.893352 0.448584 0.244384 0.814173 0.697149 0.409605 0.005715 0.954014 0.605253 0.006546 0.498493 0.598635 0.300147 0.461757 0.495600 0.585659 0.869638 0.832227 0.778563 0.548433 0.613452 0.734270 0.566369 0.775210 0.986546 0.789206 0.614336 0.158285 0.476386 0.283649 0.153812 0.777220 0.058963 0.079123 0.664238 0.523345 0.446407 0.306830 0.017285 0.293527 0.267794 0.036726 0.804040 0.333800 0.891887 0.810353 0.964101 0.957714 0.591166 0.311503 0.918421 0.050650 0.225743 0.362706 0.412256 0.944621 0.579383 0.186760 0.596485 0.933756 0.134138 0.982284 0.153876 0.820728 0.677536 0.404089 0.217287 0.773585 0.847696 0.529700 0.409600 0.018308 0.148660 0.804335 0.419778 0.339218 0.297146 0.717986 0.997807 0.551902 0.889896 0.168468 0.883564 0.702776 0.422109 0.863315 0.235043 0.563751 0.026560 0.380051 0.361835 0.091244 0.045254 0.342452 0.355414 0.995838 0.944205 0.131144 0.518355 0.477245 0.401106 0.930614 0.515336 0.809582 0.881772 0.762668 0.464421 0.073920 0.470499 0.200572 0.208567 0.177540 0.354241 0.911753 0.433521 0.178296 0.752162 0.050471 0.082669 0.038583 0.465279 0.603829 0.181789 0.519030 0.710153 20 0.924980 0.529132 0.971186 0.547253 0.006438 0.472777 0.522088 0.152209 0.654234 0.130444 0.454149 0.982361 0.132514 0.060279 0.067872 0.096146 0.819342 0.629607 0.783631 0.077135 0.971998 0.882834 0.630433 0.929073 0.398127 0.488926 0.685803 0.281597 0.676700 0.495732 0.791142 0.218834 0.746196 0.454111 0.408262 0.922643 0.150721 0.130983 0.381890 0.541987 0.944370 0.323259 0.097442 0.947387 0.857035 0.026037 0.117894 0.829376 0.138474 0.569471 0.477775 0.306686 0.056607 0.936835 0.904004 0.722235 0.654078 0.392210 0.973675 0.711008 0.493048 0.326441 0.115853 0.972061 0.274428 0.187450 0.214895 0.760854 0.944889 0.501551 0.662084 0.347404 0.128570 0.681296 0.391283 0.026204 0.500600 0.528127 0.223268 0.001690 0.446563 0.505326 0.081019 0.822740 0.240195 0.009690 0.225042 0.084190 0.825965 0.714767 0.244556 0.063837 0.426754 0.706042 0.718154 0.193434 0.740048 0.956730 0.515894 0.430045 0.023037 0.507326 0.686562 0.138175 0.672423 0.039311 0.431777 0.854276 0.422328 0.088449 0.056695 0.103273 0.485316 0.674657 0.090895 0.832212 0.776464 0.061163 0.322418 0.377140 0.502265 0.449965 0.467362 0.104714 0.373751 0.021772 0.448338 0.893115 0.775881 0.249161 0.731545 0.315138 0.496130 0.050426 0.582520 0.282594 0.293336 0.126054 0.893181 0.451273 0.650871 0.809305 0.282963 0.273382 0.385737 0.421608 0.932165 0.867633 0.445174 0.029189 0.107198 0.196259 0.238236 0.824510 0.896486 0.027887 0.573301 0.805643 0.420512 0.525267 0.286143 0.658646 0.090300 0.822265 0.807414 0.191617 0.638303 0.832465 0.469827 0.580353 0.108903 0.462419 0.886352 0.464983 0.466215 0.196005 0.038729 0.615862 0.564631 0.108231 0.741633 0.183482 0.348641 0.480481 0.643926 0.621626 0.761193 0.286651 0.492385 0.681910 0.509970 0.768211 0.299936 0.546881 0.244645 0.443597 0.810014 0.103867 0.229985 0.791764 0.566541 0.827417 0.974934 0.965224 0.949703 0.551787 0.092536 0.740431 0.564297 0.745858 0.455390 0.155663 0.995953 0.889691 0.803749 0.411476 0.213157 0.294440 0.622931 0.248217 0.799681 0.018882 0.480351 0.734015 0.683568 0.421283 0.693203 0.367198 0.232441 0.785051 0.726705 0.706457 0.031742 0.239439 0.371058 0.581879 0.087989 0.232129 0.812516 0.752740 0.804777 0.294443 0.780619 0.224786 0.289849 0.848982 0.744166 0.332253 0.567334 0.699648 25 0.091935 0.053802 0.862796 0.629970 0.869653 0.087691 0.865904 0.041248 0.958174 0.227368 0.751862 0.400749 0.403075 0.253287 0.506838 0.184560 0.057084 0.366927 0.719837 0.474175 0.922620 0.170122 0.718382 0.869798 0.129310 0.974056 0.754216 0.916677 0.037703 0.285849 0.822908 0.274734 0.051724 0.147943 0.016493 0.782417 0.349122 0.749678 0.054953 0.078198 0.789341 0.370438 0.110962 0.405216 0.651299 0.436288 0.202475 0.443890 0.742148 0.096015 0.511721 0.843616 0.993544 0.630756 0.285473 0.126022 0.013356 0.338156 0.484320 0.546191 0.036039 0.061224 0.023161 0.507389 0.266591 0.445306 0.973609 0.797776 0.284126 0.555786 0.197588 0.790601 0.087620 0.837925 0.241263 0.434262 0.427862 0.102117 0.908814 0.149011 0.907568 0.010454 0.173534 0.839139 0.310255 0.848986 0.911344 0.899901 0.867229 0.837762 0.253643 0.843008 0.617628 0.984177 0.610540 0.651698 0.654465 0.552327 0.185730 0.980976 0.095937 0.748263 0.524618 0.290760 0.228191 0.503739 0.931957 0.574130 0.989745 0.429773 0.416356 0.627877 0.907368 0.069423 0.550403 0.154606 0.718561 0.135407 0.004546 0.537516 0.016593 0.535515 0.045692 0.153979 0.843010 0.797929 0.303594 0.398493 0.449294 0.020535 0.617554 0.804555 0.234172 0.400128 0.493314 0.501881 0.333488 0.105420 0.640808 0.151469 0.195177 0.057391 0.914745 0.830670 0.159004 0.867707 0.507298 0.128301 0.113118 0.222798 0.889837 0.450922 0.697210 0.390290 0.622624 0.716448 0.500859 0.760081 0.832604 0.559944 0.543853 0.587972 0.025154 0.989102 0.070774 0.817615 0.137036 0.153020 0.359242 0.253462 0.218764 0.563383 0.480062 0.093047 0.513907 0.197418 0.480270 0.277020 0.010484 0.887781 0.373898 0.416217 0.877583 0.760771 0.548112 0.902341 0.920701 0.465441 0.565647 0.028558 0.140331 0.399998 0.827098 0.455391 0.693583 0.637980 0.655883 0.769319 0.000227 0.300695 0.695471 0.090887 0.523648 0.894595 0.963706 0.705688 0.764029 0.344608 0.251850 0.667209 0.149305 0.470917 0.022292 0.759711 0.205657 0.840590 0.774854 0.176164 0.884559 0.093064 0.202188 0.372405 0.173656 0.345234 0.567738 0.156952 0.775740 0.033662 0.010959 0.320123 0.526727 0.629727 0.881514 0.641139 0.022688 0.695190 0.884652 0.751394 0.263078 0.507574 0.124191 0.608726 0.693313 0.077786 0.404081 0.513487 0.807066 0.098966 0.472532 0.567342 30 0.535984 0.184263 0.586783 0.785911 0.998069 0.052730 0.042679 0.528934 0.603379 0.082253 0.709816 0.116138 0.197302 0.779285 0.455879 0.308850 0.367676 0.763308 0.180447 0.137743 0.500861 0.014903 0.347294 0.337737 0.504631 0.492379 0.350296 0.202390 0.159944 0.561017 0.446176 0.855299 0.911181 0.876565 0.965429 0.202311 0.726376 0.571318 0.583927 0.456722 0.582111 0.067748 0.005951 0.468012 0.790228 0.922949 0.720241 0.125931 0.719092 0.615494 0.386718 0.785214 0.101344 0.402938 0.742397 0.012434 0.928183 0.460223 0.069625 0.909931 0.123304 0.894782 0.205060 0.931989 0.497929 0.360004 0.160871 0.726376 0.835275 0.318879 0.978065 0.472711 0.566144 0.146684 0.099744 0.086517 0.753014 0.742289 0.315506 0.845314 0.534547 0.832570 0.186034 0.275953 0.583058 0.929951 0.057295 0.818297 0.164623 0.439706 0.366521 0.065779 0.787809 0.512388 0.176839 0.520831 0.591009 0.444539 0.143008 0.961264 0.417770 0.130025 0.650993 0.587905 0.254738 0.287433 0.805279 0.056208 0.278192 0.936711 0.838407 0.908174 0.594617 0.773501 0.344100 0.383867 0.377941 0.425964 0.706898 0.244171 0.177781 0.216465 0.724390 0.631487 0.794535 0.785719 0.144977 0.087488 0.256757 0.640117 0.569708 0.889343 0.918142 0.152385 0.118985 0.010054 0.279839 0.122922 0.457636 0.188401 0.007611 0.896338 0.997553 0.806004 0.030168 0.535323 0.610001 0.851424 0.409958 0.261397 0.754791 0.791076 0.787290 0.947782 0.400208 0.658216 0.525636 0.418143 0.407535 0.371269 0.843894 0.470826 0.584900 0.165718 0.547158 0.538819 0.507630 0.643081 0.544684 0.318254 0.788191 0.564425 0.415579 0.327931 0.920648 0.930469 0.010692 0.093082 0.134089 0.627337 0.261692 0.582543 0.554902 0.200341 0.657080 0.943207 0.356768 0.031801 0.473527 0.067458 0.499254 0.692144 0.076118 0.229830 0.497368 0.542791 0.494935 0.801283 0.307959 0.114020 0.560281 0.769109 0.435187 0.083534 0.598594 0.846064 0.183256 0.912969 0.103652 0.126824 0.004074 0.784589 0.560478 0.660520 0.521822 0.795746 0.251644 0.530482 0.056304 0.137333 0.141988 0.663768 0.107840 0.757035 0.454255 0.694128 0.356727 0.768018 0.817328 0.926744 0.207038 0.600957 0.034478 0.292990 0.042301 0.097268 0.300917 0.060267 0.583178 0.724417 0.750768 0.698099 0.113359 0.286959 0.262145 0.066228 0.002177 0.269344 0.033811 0.467862 35 0.540827 0.204775 0.228798 0.653331 0.019622 0.299416 0.480416 0.521274 0.958998 0.760680 0.803426 0.329561 0.376779 0.687057 0.707253 0.110846 0.267753 0.030533 0.400994 0.006122 0.668872 0.868899 0.375823 0.281110 0.101351 0.805716 0.484274 0.876675 0.152222 0.219716 0.733271 0.065521 0.815511 0.859773 0.031611 0.066803 0.473045 0.205618 0.225444 0.434379 0.232910 0.828409 0.891442 0.814307 0.961300 0.663847 0.045957 0.875554 0.942950 0.969106 0.749301 0.932736 0.088412 0.834347 0.731381 0.961446 0.114667 0.263481 0.197428 0.145589 0.269552 0.083905 0.195744 0.947519 0.969737 0.003897 0.184293 0.927966 0.686500 0.814794 0.990079 0.080690 0.573512 0.866518 0.246405 0.593335 0.975862 0.193811 0.292937 0.496420 0.653752 0.606613 0.707874 0.314201 0.445854 0.301090 0.437284 0.050775 0.953511 0.178893 0.076753 0.936787 0.835825 0.972619 0.823175 0.033104 0.569497 0.147515 0.580054 0.083491 0.455287 0.859157 0.325576 0.337595 0.918137 0.637203 0.165951 0.874123 0.095567 0.590123 0.632750 0.332655 0.161180 0.640775 0.387740 0.816528 0.229320 0.665116 0.575607 0.079372 0.042322 0.857022 0.147542 0.103078 0.106349 0.293252 0.530117 0.866326 0.868784 0.356503 0.098127 0.438227 0.947486 0.041595 0.310848 0.782287 0.795510 0.852581 0.543891 0.590629 0.139108 0.034724 0.155260 0.756919 0.368782 0.282812 0.241799 0.871352 0.367094 0.888036 0.949879 0.872759 0.372045 0.645857 0.432691 0.730600 0.698945 0.430268 0.403872 0.601106 0.648886 0.114080 0.705351 0.359729 0.422926 0.313542 0.568306 0.033565 0.976840 0.970589 0.282273 0.657821 0.777086 0.945495 0.668108 0.450520 0.840253 0.822521 0.930183 0.702058 0.800625 0.958945 0.385884 0.732510 0.128525 0.205334 0.883182 0.695824 0.710903 0.369791 0.578274 0.083809 0.594183 0.176973 0.420867 0.239874 0.127528 0.895602 0.910076 0.628879 0.157561 0.730590 0.989096 0.779748 0.979535 0.804948 0.419733 0.465054 0.756395 0.810150 0.141343 0.081162 0.357551 0.968792 0.691728 0.022632 0.212822 0.726247 0.673218 0.038396 0.358687 0.751141 0.865672 0.954217 0.879565 0.865962 0.712057 0.663771 0.384795 0.179304 0.744532 0.767024 0.156547 0.002302 0.521688 0.758571 0.441169 0.888558 0.529315 0.884992 0.452677 0.683913 0.061200 0.740537 0.672597 0.010396 0.673222 0.789925 0.830775 0.994382 40 0.567055 0.861562 0.091171 0.168913 0.330607 0.954729 0.103698 0.497233 0.989088 0.615534 0.556942 0.237431 0.468376 0.468848 0.563128 0.411660 0.177537 0.152329 0.749635 0.488856 0.729277 0.424968 0.725626 0.588008 0.137071 0.564747 0.294475 0.633995 0.643235 0.552502 0.390021 0.267122 0.802166 0.899636 0.101588 0.453780 0.394023 0.671643 0.553794 0.842909 0.005965 0.645908 0.114857 0.756375 0.228041 0.218538 0.411340 0.068495 0.468752 0.354438 0.082702 0.135829 0.912707 0.871928 0.783664 0.714836 0.670339 0.634191 0.390674 0.766654 0.221710 0.706757 0.304554 0.783722 0.605998 0.871748 0.085938 0.545652 0.915545 0.612661 0.559332 0.328905 0.832604 0.245167 0.288022 0.602608 0.389300 0.412311 0.532853 0.875927 0.764314 0.851718 0.839969 0.547091 0.544742 0.008815 0.619703 0.912370 0.212596 0.322151 0.261646 0.893362 0.822451 0.261651 0.206884 0.526370 0.017429 0.417386 0.089023 0.987280 0.949489 0.752157 0.640264 0.208378 0.167866 0.143720 0.452846 0.004471 0.314781 0.610455 0.993893 0.521580 0.102677 0.052393 0.328584 0.559762 0.837754 0.056114 0.894920 0.895800 0.452258 0.904928 0.153190 0.736461 0.474704 0.974107 0.525682 0.858111 0.818830 0.838557 0.188383 0.924869 0.706319 0.058788 0.883631 0.337729 0.995801 0.462074 0.810919 0.703423 0.960680 0.714931 0.495214 0.974745 0.726947 0.496895 0.330621 0.369228 0.076864 0.767658 0.927281 0.422665 0.520893 0.008954 0.430327 0.153062 0.481389 0.502224 0.491142 0.793016 0.049501 0.716341 0.551266 0.344468 0.941414 0.753313 0.801208 0.627031 0.548285 0.717608 0.528846 0.519311 0.238971 0.729545 0.996075 0.671874 0.571339 0.357099 0.064273 0.707820 0.234793 0.785274 0.359955 0.071322 0.785205 0.388502 0.356767 0.516093 0.832568 0.301645 0.541366 0.323027 0.124823 0.169384 0.080594 0.463017 0.858633 0.104355 0.852988 0.597123 0.915584 0.177716 0.982103 0.654384 0.142018 0.086705 0.404565 0.426588 0.827747 0.650577 0.018883 0.904682 0.530450 0.079920 0.624670 0.465973 0.316980 0.276635 0.948245 0.716780 0.258265 0.348911 0.470768 0.038167 0.299057 0.371766 0.621670 0.026973 0.919548 0.238866 0.137410 0.937422 0.756219 0.952888 0.859659 0.863723 0.405497 0.696508 0.123494 0.982620 0.293409 0.056364 0.934541 0.648780 0.345582 0.543310 0.877491 0.870595 0.976589 0.981365 45 0.435896 0.084300 0.377124 0.945298 0.520845 0.785690 0.861567 0.429375 0.685440 0.264764 0.195004 0.820517 0.520284 0.393495 0.041082 0.120945 0.697498 0.326377 0.305887 0.874624 0.864712 0.734766 0.277136 0.357538 0.277351 0.640994 0.121815 0.372180 0.450532 0.390561 0.815684 0.973644 0.920614 0.098980 0.118454 0.980459 0.144418 0.903213 0.745915 0.348459 0.009344 0.661247 0.776138 0.926618 0.109416 0.069886 0.887843 0.705902 0.215822 0.722514 0.979047 0.060294 0.714918 0.178970 0.862010 0.629278 0.315053 0.233932 0.682952 0.634371 0.593990 0.653278 0.159169 0.889291 0.429808 0.923676 0.459204 0.629589 0.121809 0.230698 0.020491 0.082925 0.162064 0.347280 0.383500 0.716614 0.480750 0.872787 0.508041 0.983035 0.426215 0.522011 0.946744 0.514818 0.375233 0.557168 0.455269 0.944596 0.747060 0.592756 0.934595 0.857946 0.624452 0.767187 0.798426 0.796431 0.090590 0.025660 0.354053 0.282802 0.055717 0.067824 0.101680 0.616696 0.202065 0.879832 0.997008 0.783719 0.725194 0.287213 0.717101 0.650733 0.134864 0.064811 0.305191 0.694090 0.588757 0.717307 0.873950 0.805325 0.098874 0.306914 0.308738 0.640191 0.334083 0.215230 0.018401 0.862986 0.111205 0.252069 0.992470 0.827876 0.245056 0.053551 0.213206 0.642479 0.766173 0.751022 0.055933 0.618649 0.346320 0.181844 0.419066 0.278752 0.049216 0.833784 0.813803 0.346955 0.751757 0.019138 0.718495 0.140949 0.655543 0.435220 0.768017 0.209411 0.337796 0.506579 0.291200 0.671636 0.025366 0.323817 0.092897 0.184227 0.829370 0.076155 0.521568 0.984066 0.424239 0.635826 0.435568 0.552829 0.784889 0.162503 0.582840 0.570419 0.155016 0.248884 0.198053 0.286789 0.577720 0.575723 0.772101 0.409807 0.666905 0.868316 0.329513 0.053548 0.794044 0.643382 0.446025 0.013421 0.384898 0.051798 0.530531 0.209515 0.273766 0.211940 0.326714 0.000043 0.266803 0.950126 0.417866 0.544910 0.610801 0.050281 0.121625 0.506297 0.597427 0.274121 0.029152 0.880673 0.911668 0.090484 0.599309 0.688165 0.057613 0.084610 0.333317 0.590575 0.928670 0.105424 0.389376 0.754646 0.789828 0.010146 0.559552 0.481031 0.303689 0.990786 0.615370 0.835726 0.459765 0.632420 0.844262 0.671021 0.146877 0.180228 0.911061 0.644661 0.912471 0.553464 0.768097 0.043843 0.327812 0.921682 0.581901 0.721112 0.617171 0.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:
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 )
Apples Oranges Kiwi Miracle Fruit Durian 0 100 200 300 400
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:
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.00 1.57 3.14 4.71 6.28 -1.0 -0.5 0.0 0.5 1.0
Finally, you can supply both locations and labels to an Explicit
locator:
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 2π -1.0 -0.5 0.0 0.5 1.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:
import toyplot.data
data = toyplot . data . commute ()
data [: 6 ]
Datetime Name Value Units 2014-05-01T14:08:53.587607Z Status Since DTC Cleared 0011110100110 2014-05-01T14:08:53.656972Z Fuel System Status 0000 2014-05-01T14:08:53.726403Z Calculated Load Value 0.0 % 2014-05-01T14:08:53.734393Z Coolant Temperature 13 C 2014-05-01T14:08:53.804349Z Short Term Fuel Trim 0.0 % 2014-05-01T14:08:53.873862Z Long 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:
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:
observations = numpy . logical_and ( data [ "Name" ] == "Vehicle Speed" , data [ "Value" ] != "NODATA" )
x = timestamps [ observations ]
y = data [ "Value" ][ observations ]
canvas , axes , mark = toyplot . plot (
x , y , label = "Vehicle Speed" , xlabel = "Time" , ylabel = "km/h" ,
width = 600 , height = 300 )
1398953000 1398954000 1398955000 1398956000 Time 0 40 80 120 km/h Vehicle Speed
As you would expect, the timestamps make very unfriendly tick labels.
Let’s use the timestamp locator instead:
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:10 5/1 14:15 5/1 14:20 5/1 14:25 5/1 14:30 5/1 14:35 5/1 14:40 5/1 14:45 5/1 14:50 Time 0 40 80 120 km/h Vehicle 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”.
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:10 5/1 08:15 5/1 08:20 5/1 08:25 5/1 08:30 5/1 08:35 5/1 08:40 5/1 08:45 5/1 08:50 Time (US/Mountain) 0 40 80 120 km/h Vehicle 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:
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:10 5/1 08:20 5/1 08:30 5/1 08:40 5/1 08:50 Time (US/Mountain) 0 40 80 120 km/h Vehicle 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:
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:10 08:15 08:20 08:25 08:30 08:35 08:40 08:45 08:50 Time (US/Mountain) 0 40 80 120 km/h Vehicle 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:
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:14 5/1 08:21 5/1 08:28 5/1 08:35 5/1 08:42 5/1 08:49 Time (US/Mountain) 0 40 80 120 km/h Vehicle 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):
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 am May 4, 2014 8:15 am May 4, 2014 8:20 am May 4, 2014 8:25 am May 4, 2014 8:30 am May 4, 2014 8:35 am May 4, 2014 8:40 am May 4, 2014 8:45 am May 4, 2014 8:50 am 0 40 80 120 km/h Vehicle Speed Time (US/Mountain)