Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

In matplotlib, how do you draw R-style axis ticks that point outward from the axes?

Tags:

Because they are drawn inside the plot area, axis ticks are obscured by the data in many matplotlib plots. A better approach is to draw the ticks extending from the axes outward, as is the default in ggplot, R's plotting system.

In theory, this can be done by redrawing the tick lines with the TICKDOWN and TICKLEFT line-styles for the x-axis and y-axis ticks respectively:

import matplotlib.pyplot as plt import matplotlib.ticker as mplticker import matplotlib.lines as mpllines  # Create everything, plot some data stored in `x` and `y` fig = plt.figure() ax = fig.gca() plt.plot(x, y)  # Set position and labels of major and minor ticks on the y-axis # Ignore the details: the point is that there are both major and minor ticks ax.yaxis.set_major_locator(mplticker.MultipleLocator(1.0)) ax.yaxis.set_minor_locator(mplticker.MultipleLocator(0.5))  ax.xaxis.set_major_locator(mplticker.MultipleLocator(1.0)) ax.xaxis.set_minor_locator(mplticker.MultipleLocator(0.5))  # Try to set the tick markers to extend outward from the axes, R-style for line in ax.get_xticklines():     line.set_marker(mpllines.TICKDOWN)  for line in ax.get_yticklines():     line.set_marker(mpllines.TICKLEFT)  # In real life, we would now move the tick labels farther from the axes so our # outward-facing ticks don't cover them up  plt.show() 

But in practice, that's only half the solution because the get_xticklines and get_yticklines methods return only the major tick lines. The minor ticks remain pointing inward.

What's the work-around for the minor ticks?

like image 298
pash Avatar asked Jun 07 '11 02:06

pash


People also ask

How do I create a tick in MatPlotLib?

MatPlotLib with Python Plot x and y points over the plot, where x ticks could be from 1 to 10 (100 data points) on the curve. To add extra ticks, use xticks() method and increase the range of ticks to 1 to 20 from 1 to 10. To display the figure, use the show() method.

How do I change the direction of my axis in MatPlotLib?

In Matplotlib we can reverse axes of a graph using multiple methods. Most common method is by using invert_xaxis() and invert_yaxis() for the axes objects. Other than that we can also use xlim() and ylim(), and axis() methods for the pyplot object.


2 Answers

In your matplotlib config file, matplotlibrc, you can set:

xtick.direction      : out     # direction: in or out ytick.direction      : out     # direction: in or out 

and this will draw both the major and minor ticks outward by default, like R. For a single program, simply do:

>> from matplotlib import rcParams >> rcParams['xtick.direction'] = 'out' >> rcParams['ytick.direction'] = 'out' 
like image 135
ChrisM Avatar answered Sep 20 '22 23:09

ChrisM


You can get the minors in at least two ways:

>>> ax.xaxis.get_ticklines() # the majors <a list of 20 Line2D ticklines objects> >>> ax.xaxis.get_ticklines(minor=True) # the minors <a list of 38 Line2D ticklines objects> >>> ax.xaxis.get_minorticklines() <a list of 38 Line2D ticklines objects> 

Note that the 38 is because minor tick lines have also been drawn at the "major" locations by the MultipleLocator call.

like image 22
DSM Avatar answered Sep 20 '22 23:09

DSM