windrose icon indicating copy to clipboard operation
windrose copied to clipboard

Smooth plot

Open georgebv opened this issue 9 years ago • 9 comments

I have found some real dark magic. Your module plots roses as triangles emerging from center - with straight, blocky edges. However, if you add this code (i.e. any histogram with pyplot) plt.hist([0, 1]); plt.close() before executing your function (i.e. WindroseAxes), you get smooth curvy roses like here: http://climate.ncsu.edu/windrose.php krdu_windrose This doesn't make any sense and I couldn't figure out why this works, but it does. Any idea on why this happens and if you can add this "feature" to your module?

georgebv avatar Oct 04 '16 23:10 georgebv

Hello @georgebv ,

Please, could you provide sample data (or random data), full code and screenshots comparing these 2 behaviors ?

Kind regards

s-celles avatar Oct 05 '16 08:10 s-celles

This is the regular rose plot (vanilla): rose This is what happens when I add and remove a pyplot histogram before your function: rose

This is my code:

def rose_plot(df, **kwargs):
    Plots rose of values *val* for directions *direction*
    in dtaframe *df* and saves it to folder *savepath*.

    Parameters
    ----------
    df : pandas dataframe
        Pandas dataframe with columns *val* and *direction*
    val : string
        Column name of values in dataframe *df* (i.e. 'Hs')
    direction : string
        Column name of directions in dataframe *df* (i.e. 'Dp)
    valbins : int
        Number or value bins (default = 6)
    dirbins : int
        Number of directional bins (default = 12)
    savepath : string
        Path to output folder (default = doesn't save) (i.e. 'C:\\folder')
    savename : string
        Name of file
    startfromzero : bool
        Indicates if plot starts from 0 (N) (default = False)
    valbinsize : float
        Set binsize to override *valbins* parameter (default = None, assigns bins automatically)
    title : str
        Plot title

    direction = kwargs.get('direction', 'Dp')
    val = kwargs.get('val', 'Hs')
    valbins = kwargs.get('valbins', 6)
    dirbins = kwargs.get('dirbins', 12)
    savepath = kwargs.get('savepath', None)
    savename = kwargs.get('savename', 'Rose')
    title = kwargs.get('title', 'Rose')
    legend = kwargs.get('legend', 'Value [unit]')
    startfromzero = kwargs.get('startfromzero', False)
    valbinsize = kwargs.get('valbinsize', None)

    plt.hist([0, 1])
    plt.close()
    a = df[pd.notnull(df[val])]
    a = a[pd.notnull(a[direction])]
    if startfromzero:
        a[direction] = a[direction].apply(lambda x: x - 0.5 * 360 / dirbins)
    ax = windrose.WindroseAxes.from_ax()
    if valbinsize is not None:
        valbins = np.arange(0, a[val].max() + valbinsize, valbinsize)
    ax.bar(
        a[direction],
        a[val],
        normed=True,
        opening=1,
        edgecolor='black',
        bins=valbins,
        nsector=dirbins,
        cmap=cm.jet,
        startfromzero=startfromzero
    )
    ax.set_legend()
    ax.legend(loc=(-0.12, 0.75), title=legend, fontsize=9)
    ax.get_legend().get_title().set_fontsize('9')
    plt.title(title, y=1.08, fontsize=16)
    if savepath is not None:
        plt.savefig(savepath + '\\' + savename + '.png')
        plt.close()

georgebv avatar Oct 05 '16 12:10 georgebv

Oh yes I see it now clearly!

capture d ecran 2016-10-05 a 14 33 40

is better than

capture d ecran 2016-10-05 a 14 33 05

Any idea @LionelR ?

PR is welcome!

s-celles avatar Oct 05 '16 12:10 s-celles

Pinging @wanglongqi

s-celles avatar Apr 10 '17 07:04 s-celles

@scls19fr Any idea about what is happening here? calling bar function also changes the behavior.

wanglongqi avatar Apr 27 '17 12:04 wanglongqi

Sorry @wanglongqi I haven't been able to understand what is happening. Any help on this is welcome

s-celles avatar Apr 27 '17 16:04 s-celles

@scls19fr I think this is a bug in upstream, i.e. matplotlib. Probably should fire an issue in matplotlib repository.

@janschulz if you have time, can you please check this bug? This bug is reproducible.

wanglongqi avatar Apr 28 '17 11:04 wanglongqi

Maybe passing the transform parameter to the plots methods calls can solve this problem. See https://stackoverflow.com/questions/19827792/matplotlib-drawing-a-smooth-circle-in-a-polar-plot

lnlrbr avatar May 23 '17 05:05 lnlrbr

Likely related, with matplotlib 2.0 and windrose 1.6 the contour example from the front page has straight lines on the contours, but round edges on the fill

so

tacaswell avatar Jul 26 '17 03:07 tacaswell