Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can I fill a matplotlib grid?

I would like to render a ggplot2 style filled grid, a la this
(source: had.co.nz)

I haven't been able to find any online resources that deal with styling the grid in such a way. Do I have to resort to doing something like plotting my own rectangular patches?

Edit: after trying Chris' solution, I have written a script to help make matplotlib graphs look like ggplot2 if anyone is interested.

http://messymind.net/making-matplotlib-look-like-ggplot/

like image 302
Bicubic Avatar asked Jul 27 '12 08:07

Bicubic


1 Answers

The following code uses matplotlib.pyplot.grid to turn on a grid and set the grid properties (line colour, style and width) and then uses plt.gca().patch.set_facecolor('0.8') to change the axes color (I'm not sure if there is, but there must be convenience function to do this). The argument to patch.set_facecolor is any matplotlib colour.

import numpy
import matplotlib.pyplot as plt

x = numpy.random.rand(10)
x = numpy.random.rand(10)

plt.plot(x, y, 'o')

plt.grid(True, color='w', linestyle='-', linewidth=2)
plt.gca().patch.set_facecolor('0.8')

plt.show()

The result is

Figure generated by the code above

like image 104
Chris Avatar answered Oct 11 '22 21:10

Chris