pandas-dev / pandas

Flexible and powerful data analysis / manipulation library for Python, providing labeled data structures similar to R data.frame objects, statistical functions, and much more
https://pandas.pydata.org
BSD 3-Clause "New" or "Revised" License
43.95k stars 18.04k forks source link

DataFrame.plot() with mplstyle file option axes.grid.axis: y behaves like axes.grid.axis: both #17725

Open luisdelatorre012 opened 7 years ago

luisdelatorre012 commented 7 years ago

Problem description

Matplotlib style files have an option axes.grid.axis: with valid values both, x, or y. Using the value y should result in plotting only horizontal gridlines. When plotting with DataFrame.plot(), both vertical and horizontal gridlines are displayed. The code below plots an example of the behavior. The mplstyle file linked to contains only these two options.

axes.grid.axis: y
axes.grid: True
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()

If instead you plot with Matplotlib directly, using ax.bar(d['Month'], d['Volume']), you get the expected output of only horizontal lines.

sinhrks commented 7 years ago

It looks current impl doesn't care axes.grid.axis. PR is welcome.

https://github.com/pandas-dev/pandas/blob/master/pandas/plotting/_core.py#L140

ryancasburn-KAI commented 3 years ago

This is still an issue.

This is due to https://github.com/pandas-dev/pandas/blob/6fd01648b6d14b24690b85f993b193699df12439/pandas/plotting/_matplotlib/core.py#L574

Pandas adds a grid to an axes using https://matplotlib.org/stable/api/_as_gen/matplotlib.axes.Axes.grid.html

This function takes two optional parameters, one to set which axis to apply the grid to and one to set which ticks (major or minor). Both parameters default to specific values (both axis, major ticks) not the rcParams setting. This means that pandas users have no way to set these parameters as the rcParams setting isn't being used and the pandas API has no way to set these either.

At a minimum, pandas should be pulling the rcParams "axes.grid.axis" and "axes.grid.which" and using those in the function call to axes.grid. There may be some interest in also extending the pandas api to include these parameters, but I don't think that is necessary now.

I'm not comfortable contributing to this large of a project as I'm not set up to run tests and such. However, I've tried out the following fix and think it should work. Could someone else put in a pull request?

Fix: https://github.com/pandas-dev/pandas/blob/6fd01648b6d14b24690b85f993b193699df12439/pandas/plotting/_matplotlib/core.py#L574

Changes to:

ax.grid(self.grid, axis=self.plt.rcParams["axes.grid.axis"], which=self.plt.rcParams["axes.grid.which"])
Krisselack commented 1 year ago

Found the same issue. Seems to be still open in 2023.

stefmolin commented 1 year ago

take