Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
700 views
in Technique[技术] by (71.8m points)

python - Plotting scatter of several polar plots

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 94948 98.00
1.0 0.8 0.1 0.3 78483 97.13

I want to represent visually the relation between all five variables and the dependent variable. For this purpose I was thinking of combining two types of plots:

  • scatter plot between s and a
  • polar plot for v1, v2, v3 and v4

So essentially I want to display a small polar plot for each data point in my dataset. Something like this:

enter image description here

An example polar plot is:

import numpy as np
import matplotlib.pyplot as plt

theta = np.linspace(0.0, 2 * np.pi, 4, endpoint=False)
radii = [90, 90, 90, 90]
width = np.pi / 4 * np.array([1.0, 0.7, 0.6, 0.2])

ax = plt.subplot(111, projection='polar')
bars = ax.bar(theta, radii, width=width, bottom=0.0)

plt.show()

enter image description here

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

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()

enter image description here


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...