南丁格尔玫瑰图可视化重叠

问题描述 投票:0回答:1

我使用以下代码绘制夜莺玫瑰图:

mydata = pd.DataFrame(dict(model=["1", "2", "3", "4", "5", "6", "7", "8"], auc_roc=[99.7, 80.2, 85.3, 87.9, 89.2, 88.6, 91.0, 96.6]))

n_row = mydata.shape[0]
angle = np.arange(0, 2*np.pi, 2*np.pi/n_row)
radius = np.array(mydata.auc_roc)

fig = plt.figure(figsize=(4, 4), dpi=180)
ax = fig.add_axes([0.1, 0.1, 0.8, 0.8], polar=True)

ax.set_theta_offset(np.pi/2-np.pi/n_row)
ax.set_theta_direction(-1)
ax.set_rlabel_position(360-180/n_row)

# colors = ['#FFB300', '#803E75', '#FF6800', '#A6BDD7', '#C10020', '#CEA262', '#817066', '#007D34']
cmap=cm.get_cmap('Reds', n_col)
all_color=[colors.rgb2hex(cmap(i)[:8]) for i in range(cmap.N)]
bars = ax.bar(angle, radius, color=all_color, edgecolor="k", width=0.90, alpha=0.9)

for bar in bars:
    height = bar.get_height()
    ax.text(bar.get_x() + bar.get_width()/2.0, height, f'{height:.1f}', ha='center', va='bottom')

plt.xticks(angle, labels=mydata.model, size=8)
plt.ylim(0, 105)
plt.yticks(np.arange(0, 100, 10))

plt.grid(which='major', axis="x", linestyle='-', linewidth='0.5', color='gray', alpha=0.5)
plt.grid(which='major', axis="y", linestyle='-', linewidth='0.5', color='gray', alpha=0.5)

plt.show()

此图如图所示: 在此输入图片描述

但是各个类别都有图中重叠的内容。怎么解决?

charts visualization overlap
1个回答
0
投票

角度设置不正确,正确代码如下:

import pandas as pd
import numpy as np
import matplotlib.pyplot as plt

categories = ["1", "2", "3", "4", "5", "6", "7", "8"]
values = [0.997, 0.802, 0.853, 0.879, 0.892, 0.886, 0.910, 0.966]

# Calculate the angle and width of each category
total = sum(values)
angles = [2 * np.pi * (i / len(categories)) for i in range(len(categories))]
widths = [2 * np.pi / len(categories)] * len(categories)

fig = plt.figure(figsize=(6, 6), dpi=180)
ax = fig.add_subplot(111, polar=True)

# Plot each category as a bar
bars = ax.bar(angles, values, width=widths, bottom=0.0, color='skyblue', edgecolor="k", alpha=0.7)

# Customize the plot
ax.set_xticks(angles)
ax.set_xticklabels(categories)
ax.set_ylim(0, 1.1)
ax.set_yticks(np.arange(0, 1.1, 0.2))


# Add labels to each bar
for i, bar in enumerate(bars):
    angle = angles[i]
    width = bar.get_width()
    height = bar.get_height()
    x = bar.get_x() + width/2.0
    y = height
    ax.text(x, y, f'{height:.3f}', ha='center', va='bottom')

# Add a title
plt.title('Nightingale Rose Chart')
plt.grid(which='major', axis='x', linestyle='-', linewidth='0.5', color='gray', alpha=0.5)
plt.grid(which='major', axis='y', linestyle='-', linewidth='0.5', color='gray', alpha=0.5)

# Display the chart
plt.show()

如图所示:

© www.soinside.com 2019 - 2024. All rights reserved.