Code Monkey home page Code Monkey logo

Comments (5)

lukelbd avatar lukelbd commented on May 22, 2024

Thanks for the examples (and your interest)! Never thought of using seaborn with ProPlot. This is probably related to my fill_between_wrapper overrides. All of ProPlot's plotting wrappers are supposed to be strict supersets/not interfer with usage of the existing matplotlib API, but I appear to have overlooked something. Will get to it within the next few days.

from proplot.

tommylees112 avatar tommylees112 commented on May 22, 2024

from proplot.

lukelbd avatar lukelbd commented on May 22, 2024

Sorry for the delay -- the latest commit (c45c88a) fixes this. The source of the bug is complicated, but basically I was wrapping the matplotlib.axes.Axes.hist method improperly. Try running pip uninstall proplot then re-installing with pip install git+https://github.com/lukelbd/proplot.git#egg=proplot (when ProPlot is officially released it will use a versioning system, but for now I'm just pushing bugfixes directly to master).

Also note that you cannot specify both the figure dimensions and the axes dimensions. If you input the former the latter will be ignored. ProPlot now issues a warning when this happens.

from proplot.

lukelbd avatar lukelbd commented on May 22, 2024

And thanks again for posting the example, you made it a very quick fix.

from proplot.

Ntkrell avatar Ntkrell commented on May 22, 2024

@tommylees112 maybe you can help me with this. When I try and run your code above I get ValueError: The truth value of an array with more than one element is ambiguous. Use a.any() or a.all(). Did you ever run into this error?

I just installed proplot so I have version 0.5.0.

Here's the code I used:

import pandas as pd
import xarray as xr
import numpy as np
import seaborn as sns

import proplot as plot
import calendar

def drop_nans_and_flatten(dataArray: xr.DataArray) -> np.ndarray:
    """flatten the array and drop nans from that array. Useful for plotting histograms.

    Arguments:
    ---------
    : dataArray (xr.DataArray)
        the DataArray of your value you want to flatten
    """
    # drop NaNs and flatten
    return dataArray.values[~np.isnan(dataArray.values)]


# create dimensions of xarray object
times = pd.date_range(start='1981-01-31', end='2019-04-30', freq='M')
lat = np.linspace(0, 1, 224)
lon = np.linspace(0, 1, 176)

rand_arr = np.random.randn(len(times), len(lat), len(lon))

# create xr.Dataset
coords = {'time': times, 'lat':lat, 'lon':lon}
dims = ['time', 'lat', 'lon']
ds = xr.Dataset({'precip': (dims, rand_arr)}, coords=coords)
ds['month'], ds['year'] = ds['time.month'], ds['time.year']

f, axs = plot.subplots(nrows=4, ncols=3, axwidth=1.5, figsize=(8,12), share=2) # share=3, span=1,
axs.format(
    xlabel='Precip', ylabel='Density', suptitle='Distribution', 
)

month_abbrs = list(calendar.month_abbr)
mean_ds = ds.groupby('time.month').mean(dim='time')
flattened = []
for mth in np.arange(1, 13):
    ax = axs[mth - 1]
    ax.set_title(month_abbrs[mth])
    print(f"Plotting {month_abbrs[mth]}")
    flat = drop_nans_and_flatten(mean_ds.sel(month=mth).precip)
    flattened.append(flat)
    sns.distplot(flat, ax=ax, **{'kde': False})

And the error:

Plotting Jan
---------------------------------------------------------------------------
ValueError                                Traceback (most recent call last)
<ipython-input-1-bea6a445aa3a> in <module>
     46     flat = drop_nans_and_flatten(mean_ds.sel(month=mth).precip)
     47     flattened.append(flat)
---> 48     sns.distplot(flat, ax=ax, **{'kde': False})
     49

~/miniconda3/envs/maize-Toff/lib/python3.7/site-packages/seaborn/distributions.py in distplot(a, bins, hist, kde, rug, fit, hist_kws, kde_kws, rug_kws, fit_kws, color, vertical, norm_hist, axlabel, label, ax)
    226         ax.hist(a, bins, orientation=orientation,
    227                 color=hist_color, **hist_kws)
--> 228         if hist_color != color:
    229             hist_kws["color"] = hist_color
    230

ValueError: The truth value of an array with more than one element is ambiguous. Use a.any() or a.all()

Bringing this up because I'm trying to make a similar plot (proplot subplots + sns.distplot()) but am running into that ValueError for my code as well. Hoping it's an easy fix.

from proplot.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.