Issue
I need to draw diagrams similar to this one:
Matplotlib doesn't seem to have anything similar to it. I found a third-party module that does something similar but using third-party modules is not a preferable option.
How this can be done with Matplotlib?
DataFrame structure example
df = pd.DataFrame({"Value":[50],"Future":[60], "Past": [70],"Health":[100], "Income": [20]})
Solution
You can make a polar plot and interpolate values between the points:
# create dataframe
df = pd.DataFrame(
{"Value":[50],"Future":[60], "Past": [70],"Health":[100], "Income": [20]})
# calculate values at different angles
z = df.rename(index={0: 'value'}).T.reset_index()
z = z.append(z.iloc[0], ignore_index=True)
z = z.reindex(np.arange(z.index.min(), z.index.max() + 1e-10, 0.05))
z['value'] = z['value'].interpolate(method='pchip')
z['angle'] = np.linspace(0, 2*np.pi, len(z))
# plot
fig = plt.figure()
ax = fig.add_subplot(projection='polar')
z.plot('angle', 'value', ax=ax, legend=False)
ax.fill_between(z['angle'], 0, z['value'], alpha=0.1)
ax.set_xticks(z.dropna()['angle'].iloc[:-1])
ax.set_xticklabels(z.dropna()['index'].iloc[:-1])
ax.set_xlabel(None)
ax.set_ylabel(None)
ax.set_yticks([])
# striped background
n = 5
vmax = z['value'].max()
for i in np.arange(n):
ax.fill_between(
np.linspace(0, 2*np.pi, 100), vmax/n/2*i*2, vmax/n/2*(i*2+1),
color='silver', alpha=0.1)
plt.show()
Output:
P.S. You can check different method
options in interpolate
to get different smoothing between the points.
Answered By - perl
0 comments:
Post a Comment
Note: Only a member of this blog may post a comment.