1

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

ImportanceOfBeingErnest
  • 321,279
  • 53
  • 665
  • 712
Interfector
  • 1,868
  • 1
  • 23
  • 43
  • 2
    How about saving your polar plot to image first and then putting it on the final plot [as an image marker](https://stackoverflow.com/questions/2318288/how-to-use-custom-marker-with-plot)? – alex Sep 17 '17 at 10:41
  • That could work. Is that the only alternative that anyone can think of? – Interfector Sep 17 '17 at 10:42

1 Answers1

6

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

ImportanceOfBeingErnest
  • 321,279
  • 53
  • 665
  • 712