4

Using Matplotlib I'd like to remove the grid lines inside the plot, while keeping the frame (i.e. the axes lines). I've tried the code below and other options as well, but I can't get it to work. How do I simply keep the frame while removing the grid lines?

I'm doing this to reproduce a ggplot2 plot in matplotlib. I've created a MWE below. Be aware that you need a relatively new version of matplotlib to use the ggplot2 style.

enter image description here

import matplotlib
matplotlib.use('Agg')
import matplotlib.pyplot as plt
import pylab as P
import numpy as np

if __name__ == '__main__':


    values = np.random.uniform(size=20)

    plt.style.use('ggplot')
    fig = plt.figure()
    _, ax1 = P.subplots()    

    weights = np.ones_like(values)/len(values)
    plt.hist(values, bins=20, weights=weights)
    ax1.set_xlabel('Value')
    ax1.set_ylabel('Probability')    

    ax1.grid(b=False)
    #ax1.yaxis.grid(False)
    #ax1.xaxis.grid(False)

    ax1.set_axis_bgcolor('white')    
    ax1.set_xlim([0,1])

    P.savefig('hist.pdf', bbox_inches='tight')
pir
  • 5,513
  • 12
  • 63
  • 101
  • 1
    `ax1.grid(False)` works for me. Could you paste an example of code that it is not working? – kikocorreoso Nov 18 '15 at 10:07
  • `ggplot` doesn't look anything like your examples though. And doesn't appear to have a frame at all (just a grey background). Removing the grid lines doesn't change that. For example, see the examples [here](http://matplotlib.org/1.5.0/examples/style_sheets/plot_ggplot.html). None of them have a frame. If you want to reproduce the images above, I would recommend `matplotlib's` default style, which is much closer. – tmdavison Nov 18 '15 at 10:17
  • I want to reproduce a viz. created using ggplot2, which is why I'm starting from that style. See my updated post. – pir Nov 18 '15 at 11:00

1 Answers1

4

OK, I think this is what you are asking (but correct me if I misunderstood):

You need to change the colour of the spines. You need to do this for each spine individually, using the set_color method:

for spine in ['left','right','top','bottom']:
    ax1.spines[spine].set_color('k')

enter image description here

You can see this example and this example for more about using spines.

However, if you have removed the grey background and the grid lines, and added the spines, this is not really in the ggplot style any more; is that really the style you want to use?

EDIT

To make the edge of the histogram bars touch the frame, you need to either:

  1. Change your binning, so the bin edges go to 0 and 1

    n,bins,patches = plt.hist(values, bins=np.linspace(0,1,21), weights=weights)
    # Check, by printing bins:
    print bins[0], bins[-1]
    # 0.0, 1.0
    

    enter image description here

  2. If you really want to keep the bins to go between values.min() and values.max(), you would need to change your plot limits to no longer be 0 and 1:

    n,bins,patches = plt.hist(values, bins=20, weights=weights)
    ax.set_xlim(bins[0],bins[-1])
    

enter image description here

tmdavison
  • 64,360
  • 12
  • 187
  • 165
  • Well, I thought this would be easier than understanding all the smaller changes done by setting the ggplot2 style. I can't really decide the style, I need it to match an identical plot from ggplot2, where they removed the background. – pir Nov 18 '15 at 15:06
  • It seems the frame is a bit off near the edges? – pir Nov 18 '15 at 15:06
  • Fair enough. Can you be more specific about what "a bit off near the edges" means? – tmdavison Nov 18 '15 at 15:08
  • Of course. I'm curious as to why there is white space between the bar the farthest to left and the edge. Same thing for the right edge. – pir Nov 18 '15 at 15:13
  • I believe that's a problem with how you make your `hist`. You have 20 bins, between the min and max of `values`. So, if `values.min > 0` and `values.max < 1`, the edge of the bars is not at 0 and 1. I'll make an edit to the Q to show you the difference if you set `bins=np.linspace(0,1,21)` instead. – tmdavison Nov 18 '15 at 15:30