Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

version 0.12.0: problems interacting subplots and DataFrame().hist() #4408

Closed
bluefir opened this issue Jul 30, 2013 · 5 comments · Fixed by #4414
Closed

version 0.12.0: problems interacting subplots and DataFrame().hist() #4408

bluefir opened this issue Jul 30, 2013 · 5 comments · Fixed by #4414
Labels
Regression Functionality that used to work in a prior pandas version Visualization plotting
Milestone

Comments

@bluefir
Copy link

bluefir commented Jul 30, 2013

I run Python 2.7.5 on Windows 7 64-bit and IPython 0.13.2.

I have a data frame with some portfolio weights and I do the following:

def weight_histograms(field, name, bins=100):
    if flag_inline:
        plt.figure(figsize=(16, 8))
    else:
        plt.figure()
    plt.subplot(121)
    portfolio_data[field].hist(normed=True, bins=bins)
    plt.title('{}: All'.format(name))
    plt.subplot(122)
    portfolio_data.ix[portfolio_data[field] != 0, field].hist(normed=True, bins=bins)
    plt.title('{}: Non-Zero'.format(name))

With pandas 0.11.0, running

weight_histograms(field_benchmark_weight, 'Benchmark Weights')

in IPython with

%pylab inline

and

flag_inline=True

I get

pandas0 11 0

Doing the same thing with pandas 0.12.0 results in

pandas0 12 0

@cpcloud
Copy link
Member

cpcloud commented Jul 30, 2013

@bluefir while this is being fixed:

fyi you should really be using the plotting API not the pylab stuff that tries to imitate matlab by mutating a lot of global state in the interactive shell (i'm not dissing it, but it makes it difficult to make a somewhat uniform API (in pandas) when you have to keep track of a bunch of global state) for the record i like using the matlab-ish API, but not when i write plotting code for reuse.

here's some code to do what you want that uses the matplotlib API (and doesn't have the above error)

def weight_hists(field, name, bins=100, flag_inline=False):
    if flag_inline:
        figsize = 16, 8
    fig, (ax1, ax2) = plt.subplots(1, 2, figsize=figsize)
    portfolio_data[field].hist(ax=ax1, normed=True, bins=bins)
    ax1.set_title('{}: All'.format(name))
    portfolio_data.ix[portfolio_data[field] != 0, field].hist(ax=ax2, normed=True, bins=bins)
    ax2.set_title('{}: Non-Zero'.format(name))

@ghost ghost assigned cpcloud Jul 30, 2013
@halleygithub
Copy link

I meet this kind problem after upgrading from 0.11 to 0.12 .

In 0.11, df_hist(df[['RT','TExg','TAmt']]) will draw 3 subplot in 1 figure, one subplot for one column data , while in 0.12, only 1 subplot in 1 figure (seems all subplots mix together).

def df_hist(df, title_str="", p=0, bins=None):

    #draw histgram for each dataframe column, filter_dt = dt[(dt>dt.quantile(p)) & (dt<dt.quantile(1-p))]    

    fig = plt.figure()
    plt.subplots_adjust(left=0.05, right=0.95, bottom=0.05, top=0.90)
    plt.subplots_adjust(hspace = 0.3) # the amount of height reserved for white space between subplots, wspace

    col_qty = len(df.columns)

    for i, col in enumerate(df.columns):
        dt = df[col]
        filter_dt = dt[(dt>dt.quantile(p)) & (dt<dt.quantile(1-p))]
        stat = filter_dt.describe().to_dict()
        draw_bins = (math.sqrt(len(dt)) if bins is None else bins) 

        plt.subplot(col_qty, 1, i+1)
        filter_dt.hist(normed=True, color='green', alpha=0.5, bins=draw_bins)
        plt.axis(xmin=stat['min'],xmax=stat['max'])

        mu, sigma, med = stat['mean'], stat['std'], stat['50%']

        plt.title('%s '%(col) + r'$\mu=%0.4f,\ \sigma=%0.4f,\ median=%0.4f, \ p=%0.2f $'%(mu, sigma, med, p), fontproperties=TITLE_FONT)
        plt.axvline(med, linewidth=3, color='r')

    plt.suptitle('Histogram')

    return fig

@cpcloud
Copy link
Member

cpcloud commented Jul 31, 2013

ok i can fix this tonight probably...i've figured out what issue is..

@cpcloud
Copy link
Member

cpcloud commented Jul 31, 2013

nice... this was simple fix...now i need a way to test that there's no overlap.... to the matplotlib source code mobile!

@cpcloud
Copy link
Member

cpcloud commented Jul 31, 2013

@halleygithub use some code here to show code (those are backticks, ASCII code 96) also checkout the github flavored markdown link just up and to the right of where you write a comment in.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Regression Functionality that used to work in a prior pandas version Visualization plotting
Projects
None yet
Development

Successfully merging a pull request may close this issue.

3 participants