Home > OS >  Matplotlib polar bar chart - donut hole in middle
Matplotlib polar bar chart - donut hole in middle

Time:03-20

I have the following chart and code:

Code:

def generate_chart_capability(student, df):
    # canvas sizing
    plt.figure(figsize=(10, 10))
    # plot polar axis
    ax = plt.subplot(111, polar=True, projection='polar')
    ax.set_theta_direction(-1)
    ax.set_theta_offset(np.pi / 1.6)
    ax.set_xticklabels([])
    ax.set_yticklabels([1, 2, 3, 4, 5], size=21)

    # Compute the width of each bar. In total, we have 2*Pi = 360°
    width = 2 * np.pi / len(df.index)
    # Compute the angle each bar is centered on:
    indexes = list(range(1, len(df.index)   1))
    angles = [element * width for element in indexes]
    # Compute the heights
    heights = df.Value
    # Colour the bars
    c = ['#054998', '#3660B3', '#5579CF', '#7392EB', '#90ADFF', '#ADC8FF']

    # Draw the bars
    ax.bar(x=angles, height=heights, width=width, color=c, bottom=0, linewidth=2, edgecolor="white")
    ax.set_ylim([0, 5])

    # Save the figure
    plt.savefig('app/static/output/capability_chart_'   student   '.png')
    
    # Close the figure
    plt.clf()

Chart:

enter image description here

I would like to have a donut hole in the middle of the chart (basically unfilled) between 0 and 1 as this range of values does not occur in my data.

I have tried to set bottom=1 in ax.bar but that just adds 1 to the data values.

How can I achieve this unfilled first region?

CodePudding user response:

I have tried to set bottom=1 in ax.bar but that just adds 1 to the data values.

You can create the hole by subtracting the bottom value from heights:

bottom = 1
ax.bar(x=angles, height=heights - bottom, width=width, color=c, bottom=bottom, linewidth=2, edgecolor="white")
#                              ^^^^^^^^^                               ^^^^^^

  • Related