Plotting scatter of several polar plots

后端 未结 1 708
盖世英雄少女心
盖世英雄少女心 2021-01-24 12:25

I have a dataset with five variables and one dependent variable. A sample of that would be:

v1  v2  v3  v4  s     a
1.0 0.6 0.8 0.2 56890 98.67
0.8 0.3 1.0 0.5 9         


        
1条回答
  •  夕颜
    夕颜 (楼主)
    2021-01-24 13:13

    The idea can be to place a lot of small polar axes at the positions of the points. To this end, mpl_toolkits.axes_grid1.inset_locator.inset_axes may be used. This would be placed at coordinates x, y (bbox_to_anchor=(x,y)) specified in data coordinates of the main axes (bbox_transform=axis_main.transData). The loc parameter should be set to "center" (loc=10), such that the middle of the polar plot sits at position (x,y).

    You may then plot whatever you like into the polar axes.

    import numpy as np
    import matplotlib.pyplot as plt
    from mpl_toolkits.axes_grid1.inset_locator import inset_axes
    from matplotlib.projections import get_projection_class
    
    d = np.array([[ 1.0, 0.6, 0.8, 0.2, 56890, 98.67],
                  [ 0.8, 0.3, 1.0, 0.5, 94948, 98.00],
                  [ 1.0, 0.8, 0.1, 0.3, 78483, 97.13]])
    
    fig, ax = plt.subplots()
    ax.margins(0.15)
    
    
    def plot_inset(data, x,y, axis_main, width ):
        ax_sub= inset_axes(axis_main, width=width, height=width, loc=10, 
                           bbox_to_anchor=(x,y),
                           bbox_transform=axis_main.transData, 
                           borderpad=0.0, axes_class=get_projection_class("polar"))
    
        theta = np.linspace(0.0, 2 * np.pi, 4, endpoint=False)
        radii = [90, 90, 90, 90]
        width = np.pi / 4 * data
        bars = ax_sub.bar(theta, radii, width=width, bottom=0.0)
        ax_sub.set_thetagrids(theta*180/np.pi, frac=1.4)
        ax_sub.set_xticklabels(["v{}".format(i) for i in range(1,5)])
        ax_sub.set_yticks([])
    
    
    for da in d:    
        plot_inset(da[:4], da[4],da[5], ax, 0.5 )
    
    #plot invisible scatter plot for the axes to autoscale
    ax.scatter(d[:,4], d[:,5], s=1, alpha=0.0)
    
    plt.show()
    

    0 讨论(0)
提交回复
热议问题