Search code examples
pythonmatplotlibradar-chart

Python radar plot join first and last points


I am trying to plot a radar plot. I am having trouble when it comes to getting the first and last points to join together. Currently the result looks like this:

radar plot

It appears that points 50 is not joining and I cannot understand why.

I am concatenating points together and I thought I had made sure they would join together with this code:

t1=np.concatenate([l1,r1,l2])
t1 += t1[:0]
theta += theta[:0]

However this returns a message ValueError: operands could not be broadcast together with shapes (101,) (0,) (101,)

The code I am using to plot is:

fig = plt.figure()

ax = fig.add_subplot(1, 1, 1, projection='radar')

ax.plot(theta, t1, color='r')

ax.set_varlabels(labels)
for label in ax.get_xticklabels()[::2]:
    label.set_visible(False)
plt.savefig("radar.png", dpi=100)

How can I fix this error?


Solution

  • I may give an example. From what I understand, you have a problem similar (but with larger scale) like below, I put two examples.

    • Additional explanation: plot([x1,x2],[y1,y2]) will plots (x1,y1), and draws a line that connects it to point (x2,y2). So you must put your points like this order : [x1, x2, x3, ...., xn, x1] and [y1, y2, y3, ...., yn, y1] to create 'the loop'.

    Is this okay?

    import matplotlib.pyplot as plt
    import numpy as np
    fig = plt.figure();
    ax = fig.add_subplot(1, 1, 1, projection='polar');
    
    theta1 = [0, 0.5*np.pi, 0];
    r1 = [1, 1, 0];
    
    ax.plot(theta1, r1, color='r');
    
    plt.show();
    

    enter image description here

    import matplotlib.pyplot as plt
    import numpy as np
    fig = plt.figure();
    ax = fig.add_subplot(1, 1, 1, projection='polar');
    
    theta1 = [0, 0.5*np.pi, 0, 0];
    r1 = [1, 1, 0, 1];
    
    ax.plot(theta1, r1, color='r');
    
    plt.show();
    

    enter image description here