Search code examples
pythondataframenumpyplotradar-chart

How to fix FixedLocator issue in my radar chart in Python?


I have a problem about showing a radar chart with the usage of some features of a defined dataframe. When I run the code , I got an error message.

How can I fix the issue?

Here is my dataframe column names which is shown below.

Name    PrimaryRole Manufacturer    Country Number  ActiveSince LastBuilt   Retired State   Crew    Length  Wingspan    Height  WingArea    MaxSpeed    Country_Flag_URL

Here is my function to draw radar chart which is defined below.

def radar_chart(values=[], title= "", saveFile = ""):

    labels=np.array(['Crew',
                     'Length',
                     'Wingspan',
                     'Height',
                     'WingArea',
                     'MaxSpeed'
                    ]
                   )
    
    
    angles=np.linspace(0, 2*np.pi, len(labels), endpoint=False)
    angles=np.concatenate((angles,[angles[0]]))

    fig=plt.figure(figsize=(6,6))
    plt.suptitle(title, y=1.04)
    for v in values:
        stats=np.array(ww2aircraft_df[ww2aircraft_df["Name"]==v][labels])[0]
        print(stats)
        stats=np.concatenate((stats,[stats[0]]))
        print(stats)
        ax = fig.add_subplot(111, polar=True)
        ax.plot(angles, stats, 'o-', linewidth=2, label = v)
        ax.fill(angles, stats, alpha=0.25)
        ax.set_thetagrids(angles * 180/np.pi, labels) // ISSUE IS HERE

    ax.grid(True)
    #plt.legend(loc="upper right",bbox_to_anchor=(1.2,1.0))
    ax.legend(loc='upper center', bbox_to_anchor=(0.5, -0.10),
      fancybox=True, shadow=True, ncol=5, fontsize=13)
    plt.tight_layout()
    plt.savefig('images/' + saveFile, bbox_inches = "tight")
    plt.show()

Here are my code snippets which are shown below.

ww2aircraft_df_top_5 = ww2aircraft_df.sort_values(by="MaxSpeed", ascending = False)
ww2aircraft_df_top_5 = ww2aircraft_df_top_5[:5]
radar_chart(values=ww2aircraft_df_top_5["Name"],
           title="Title", saveFile = "image.png")

Here is the error message which is shown below.

ValueError: The number of FixedLocator locations (7), usually from a call to set_ticks, does not match the number of ticklabels (6).

Solution

  • After I removed these 2 lines from the fucntion, the issue disappeared.

    angles=np.concatenate((angles,[angles[0]]))
    stats=np.concatenate((stats,[stats[0]]))