pythonmatplotlibsubplotradar-chart

Supblots to include radar plot


I'm running into issues with some subplots. I've provided some sample code to generate the types of plots I would like to create. I'd like these to be the same size, side by side.

I'm am having a really hard time figuring out how to create the subplots though. I keep running into some issues with the thetagrids here. This is what i've tried. I can get these to work seprarately, but cant figure out how to combine them. Eventually I might want a third plot as well.

import numpy as np
import matplotlib.pyplot as plt


## Plot 1
x1 = np.array([0, 1, 2, 3])
y1 = np.array([7, 2, 4, 2])

plt.subplot(1, 2, 1)
plt.figure(figsize=(5, 5))
plt.scatter(x1, y1)

# plt.show()

### Plot 2
# make up data for plot
polar_list = ['a', 'b', 'c', 'd', 'a']
polar_points = [4, 3, 6, 7, 4]

# modify lists for plots
label_loc = np.linspace(start=0, stop=2 * np.pi, num=len(polar_list))
plt.figure(figsize=(5, 5))
plt.subplot(1, 2, 2, polar=True)

plt.plot(label_loc, polar_points, label='DataLable')

plt.title('DataLable comparison', size=20, y=1.05)

lines, labels = plt.thetagrids(np.degrees(label_loc), labels=polar_list)

plt.legend()
plt.show()

Basically what i'm going for


Solution

  • You are creating a new figure every time you call plt.figure(). Just place one at the very beginning and then the plt.subplot() will add subplots to the figures.

    import numpy as np
    import matplotlib.pyplot as plt
    
    
    ## Plot 1
    x1 = np.array([0, 1, 2, 3])
    y1 = np.array([7, 2, 4, 2])
    
    plt.figure(figsize= (5, 5))
    
    plt.subplot(1, 2, 1)
    
    plt.scatter(x1, y1)
    
    # plt.show()
    
    ### Plot 2
    # make up data for plot
    polar_list = ['a', 'b', 'c', 'd', 'a']
    polar_points = [4, 3, 6, 7, 4]
    
    # modify lists for plots
    label_loc = np.linspace(start=0, stop=2 * np.pi, num=len(polar_list))
    plt.subplot(1, 2, 2, polar=True)
    
    plt.plot(label_loc, polar_points, label='DataLable')
    
    plt.title('DataLable comparison', size=20, y=1.05)
    
    lines, labels = plt.thetagrids(np.degrees(label_loc), labels=polar_list)
    
    plt.legend()
    plt.show()