guotree
guotree

Reputation: 31

Draw semicircle chart using matplotlib

Is matplotlib capable of creating semicircle charts like this:

Five segments in an arc, each in a different color and a text label above it. A line indicating the current value is drawn from the center of the semicircle out to the edge. The number value is below the chart and centered.

I have tried matplotlib.pyplot.pie without success.

Upvotes: 1

Views: 4996

Answers (2)

guotree
guotree

Reputation: 31

My solution:

import matplotlib.pyplot as plt

# data
label = ["A", "B", "C"]
val = [1,2,3]

# append data and assign color
label.append("")
val.append(sum(val))  # 50% blank
colors = ['red', 'blue', 'green', 'k']

# plot
plt.figure(figsize=(8,6),dpi=100)

wedges, labels=plt.pie(val, wedgeprops=dict(width=0.4,edgecolor='w'),labels=label, colors=colors)
# I tried this method
wedges[-1].set_visible(False)
plt.show()

Output: enter image description here

Upvotes: 1

Bill Huang
Bill Huang

Reputation: 4648

It doesn't seem like there is a built-in half-circle type in matplotlib. However, a workaround can be made based on matplotlib.pyplot.pie:

  1. Append the total sum of the data and assign white color to it.
  2. Overlay a white circle in the center by an Artist object (reference).

Sample Code:

import matplotlib.pyplot as plt

# data
label = ["A", "B", "C"]
val = [1,2,3]

# append data and assign color
label.append("")
val.append(sum(val))  # 50% blank
colors = ['red', 'blue', 'green', 'white']

# plot
fig = plt.figure(figsize=(8,6),dpi=100)
ax = fig.add_subplot(1,1,1)
ax.pie(val, labels=label, colors=colors)
ax.add_artist(plt.Circle((0, 0), 0.6, color='white'))
fig.show()

Output:

Imgur

Upvotes: 2

Related Questions