5

I'm trying to define a matplotlib style file (.mplstyle) to only show horizontal gridlines. I know I can do it in python with

fig, ax = plt.subplots() 
ax.yaxis.grid(True)

but I want to do it in the mplstyle file. The classic.mplstyle file has the option axes.grid.axis: both set. I tried changing this to axes.grid.axis: y, but this doesn't seem to change anything.

Is it possible to do this in a style file?

EDIT:

An attempt at getting this to work:

%matplotlib inline
import pandas as pd
import matplotlib.pyplot as plt
month = ['Jan', 'Feb', 'Mar', 'Apr']
volume = [1, 2, 3, 4]
plt.style.use('https://gist.githubusercontent.com/luisdelatorre012/b36899e6dca07d05e73aca80eceb3098/raw/43ae73605b5e33dfc3f0d7e5d423ff997fc8325c/tiny.mplstyle')
d = pd.DataFrame({'Month': month, 'Volume': volume})
fig, ax = plt.subplots()
b = d.plot(kind='bar', y="Volume", x="Month", ax=ax)
plt.title('Title')
plt.show()

The file tiny.mplstyle contains

axes.grid.axis: y
axes.grid: True

and nothing else.

This is the result:

gridlines

2
  • Did you restart Matplotlib? Commented Sep 28, 2017 at 5:35
  • @Joe, I'm testing this in a jupyter notebook and restart the kernel before I re-run, so that multiple matplotlib style files don't get applied together. Is that what you're referring to? Commented Sep 28, 2017 at 16:31

1 Answer 1

4

You need to turn the grid on as well,

import matplotlib.pyplot as plt
plt.rcParams["axes.grid.axis"] ="y"
plt.rcParams["axes.grid"] = True

or in the matplotlibrc file:

axes.grid.axis : y
axes.grid : True
Sign up to request clarification or add additional context in comments.

7 Comments

Thanks. I've tried two things so far, both of which result in both horizontal and vertical lines being shown. 1) changing axes.grid.axis to y and axes.grid to True in ggplot.mplstyle 2) using a .mplstyle file that contains only the lines axes.grid.axis: y and axes.grid: True.
Which version of matplotlib are you using?
matplotlib 2.0.2
Ok, what destroys the setting somehow is the use of pandas. I don't know the reason yet, but for a matplotlib plot alone it seems to work fine. Would you like to file a bug report at pandas, or do you want me to do it?
Thanks. I filed a bug report on pandas' github.
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.