Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Python Matplotlib style file: show only horizontal gridlines

Tags:

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

like image 613
AnonymousCowherd Avatar asked Sep 27 '17 21:09

AnonymousCowherd


People also ask

How do I add horizontal gridlines in Matplotlib?

Use matplotlib. axis. YAxis. grid() to only plot horizontal gridlines in a graph.

How do I show gridlines in Matplotlib?

By default, Matplotlib does not display gridlines on plots. However, you can use the matplotlib. pyplot. grid() function to easily display and customize gridlines on a plot.


1 Answers

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
like image 144
ImportanceOfBeingErnest Avatar answered Sep 20 '22 14:09

ImportanceOfBeingErnest