简体   繁体   中英

How do I make grid axes invisible for a pandas dataframe hist()?

Here is what i want to do, histogram plots of all columns of a dataframe but without the grid axes. The below code works, but preferably I'd like a more elegant solution (such as passing an argument to hist)

%matplotlib inline
import numpy as np
import pandas as pd
import matplotlib.pyplot as plt

X = np.asarray([50]*25+[30]*10)
X2 = np.asarray([90]*10+[20]*25)
X3 = np.asarray([10]*15+[70]*20)

df = pd.DataFrame(np.vstack([X, X2, X3]).T)


def plot_hists(df, nbins=10, figsize=(8, 8), disable_axis_labels = True):
    plt.close('all')
    grid_of_ax_hists = df.hist(bins=nbins, figsize=figsize)
    if disable_axis_labels:
        for row in grid_of_ax_hists:
            for ax in row:
                ax.xaxis.set_visible(False)
                ax.yaxis.set_visible(False)
    plt.show()

    df.hist()
    plt.subplots()

plot_hists(df, nbins=10, figsize=(8, 8), disable_axis_labels = True)

Even so this thread is kinda old, i'd like to add a working solution because i've just had the same issue.

At least in pandas 0.18

df.hist() takes all possible plotting keywords from pandas.DataFrame.plot

df.hist(grid=False) 

works easily.. and there is no need of dealing with matplotlib axes.

You can try:

fig = plt.figure(figsize=figsize)
ax1 = fig.add_subplot(111)
df.hist(bins=nbins, ax=ax1)
ax1.grid(b=False)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM