作为标题,我目前正在 4 个图中绘制 1 个雷达图并遇到这个问题:我的 xtick 标签的某些部分在图表内,使其难以阅读。
正如你在这里看到的,右下角的xlabel(三氟乙酸酐(无法清晰地阅读。
如何将 xtick 标签向外移动?
这是我的代码 ((第一部分有点混乱,因为为了让您能够复制粘贴以复制图形,我必须从头开始创建整个数据帧而不是调用read_csv。
我创建了 ax1 ax2 ax3 ax4,它们都是一样的,所以你可以忽略 ax2-4
data = pd.DataFrame({
'1. materials': np.array(['Chloropyrazine', 'NH2NH2', 'Propanol', 'DMSO', 'Heptane',
"Meldrum's acid", 'HCl', 'IPEA', 'Pivaloyl chloride',
'Trifluoroacetic anhydride ', '2,4,5-Trifluorophenylacetic acid',
'TFA', 'PrNH2', '10M NaOH', 'Brine', 'IPAc', 'H2O',
'Dichloromethane', 'Phosphoric acid', 'Ethanol', 'Hydrogen']),
'GWP100 (year)': np.array([ 1.25600000e+01, 3.23000000e+00, 1.10000000e-01,
3.21000000e+00, 6.00000000e-02, 1.80000000e+00,
5.05000000e+00, 1.09000000e+00, 1.04000000e+00,
1.63600000e+01, 3.50000000e-01, 2.80000000e-01,
1.88000000e+00, 4.00000000e-02, 0.00000000e+00,
2.70000000e+00, 1.00000000e-02, 7.60000000e-01,
1.62000000e+00, 2.10000000e-01, 1.00000000e-02]),
'ODP (year)': np.array([ 7.88000000e+00, 3.88000000e+00, 3.00000000e-02,
2.04000000e+00, 1.40000000e-01, 3.30000000e-01,
5.72000000e+00, 3.50000000e-01, 2.90000000e-01,
1.76000000e+01, 7.00000000e-02, 3.30000000e-01,
3.50000000e-01, 8.00000000e-02, 0.00000000e+00,
8.80000000e-01, 0.00000000e+00, 5.58100000e+01,
6.40000000e-01, 1.00000000e-02, 1.00000000e-02]),
'POFP (year)': np.array([ 1.46500000e+01, 3.78000000e+00, 4.80000000e-01,
4.59000000e+00, 3.50000000e-01, 3.59000000e+00,
6.85000000e+00, 3.00000000e+00, 1.53000000e+00,
2.28400000e+01, 7.20000000e-01, 4.00000000e-01,
2.82000000e+00, 6.00000000e-02, 0.00000000e+00,
7.45000000e+00, 1.00000000e-02, 1.55000000e+00,
3.53000000e+00, 4.50000000e-01, 1.00000000e-02]),
'TAP (year)': np.array([ 1.60300000e+01, 4.77000000e+00, 1.60000000e-01,
4.08000000e+00, 1.40000000e-01, 2.28000000e+00,
8.71000000e+00, 1.42000000e+00, 1.63000000e+00,
2.69000000e+01, 4.00000000e-01, 4.70000000e-01,
2.15000000e+00, 6.00000000e-02, 0.00000000e+00,
3.52000000e+00, 1.00000000e-02, 1.42000000e+00,
6.80000000e+00, 2.20000000e-01, 1.00000000e-02])
})
#%%
number = len(data)
my_label = list(data[data.columns[0]])
#%%
#np.random.seed(223)
np.random.seed(165)
fig1 = plt.figure(1, figsize = (16,13))
# to draw the figure and set some parameters
ax1 = fig1.add_subplot(3,2,1, polar = True)
ax1.set_theta_offset(np.pi / 2)
ax1.set_theta_direction(-1)
theta = np.linspace(0.0, 2 * np.pi, number, endpoint=False)
radii_1 = data[data.columns[1]]
width = 0.7
bars_1 = ax1.bar(theta, radii_1, width, bottom =0, label = my_label)
# to assign colors
for_color_number_range = np.random.uniform(0, 1, size= len(data))
for bar, color_number in zip(bars_1, for_color_number_range):
bar.set_facecolor(plt.cm.tab20( color_number))
bar.set_alpha(0.8)
# =============================================================================
# set x axis, and last one overlaps the 1st one so we eliminate it
# =============================================================================
divide_angles = np.linspace(0.0, 2 * np.pi, 8)
divide_angles = divide_angles[:-1]
ax1.set_xticks(divide_angles)
ax1.set_xticklabels(np.array(['Chloropyrazine', 'DMSO', 'HCl', 'Trifluoroaceticnanhydride ',
'PrNH2', 'IPAc', 'Phosphoricnacid']))# =============================================================================
# set y axis
# =============================================================================
ax1.set_yticks([10,20,30,40])
ax1.set_yticklabels(['10%', '20%', '30%', '40%',],fontsize = 8)
ax1.set_ylim(0,50)
# =============================================================================
# 設定title
# =============================================================================
ax1.set_title('nnGWPn', loc = 'left')
# ax2
ax2 = fig1.add_subplot(3,2,2, polar = True)
ax2.set_theta_offset(np.pi / 2)
ax2.set_theta_direction(-1)
radii_2 = data[data.columns[2]]
bars_2 = ax2.bar(theta, radii_2, width, bottom =0, label = my_label)
for bar, color_number in zip(bars_2, for_color_number_range):
bar.set_facecolor(plt.cm.tab20( color_number))
bar.set_alpha(0.8)
ax2.set_xticks(divide_angles)
ax2.set_xticklabels(np.array(['Chloropyrazine', 'DMSO', 'HCl', 'Trifluoroaceticnanhydride ',
'PrNH2', 'IPAc', 'Phosphoricnacid']))
ax2.set_yticks([10,20,30,40])
ax2.set_yticklabels(['10%', '20%', '30%', '40%',],fontsize = 8)
ax2.set_ylim(0,50)
ax2.set_title('nnFDPn', loc = 'left')
# ax3
ax3 = fig1.add_subplot(3,2,3, polar = True)
ax3.set_theta_offset(np.pi / 2)
ax3.set_theta_direction(-1)
radii_3 = data[data.columns[3]]
bars_3 = ax3.bar(theta, radii_3, width, bottom =0, label = my_label)
for bar, color_number in zip(bars_3, for_color_number_range):
bar.set_facecolor(plt.cm.tab20( color_number))
bar.set_alpha(0.8)
ax3.set_xticks(divide_angles)
ax3.set_xticklabels(np.array(['Chloropyrazine', 'DMSO', 'HCl', 'Trifluoroaceticnanhydride ',
'PrNH2', 'IPAc', 'Phosphoricnacid']))
ax3.set_yticks([10,20,30,40])
ax3.set_yticklabels(['10%', '20%', '30%', '40%',],fontsize = 8)
ax3.set_ylim(0,50)
ax3.set_title('nnFEPn', loc = 'left')
# ax4
ax4 = fig1.add_subplot(3,2,4, polar = True)
ax4.set_theta_offset(np.pi / 2)
ax4.set_theta_direction(-1)
radii_4 = data[data.columns[4]]
bars_4 = ax4.bar(theta, radii_4, width, bottom =0, label = my_label)
for bar, color_number in zip(bars_4, for_color_number_range):
bar.set_facecolor(plt.cm.tab20( color_number))
bar.set_alpha(0.8)
ax4.set_xticks(divide_angles)
ax4.set_xticklabels(np.array(['Chloropyrazine', 'DMSO', 'HCl', 'Trifluoroaceticnanhydride ',
'PrNH2', 'IPAc', 'Phosphoricnacid']))
ax4.set_yticks([10,20,30,40])
ax4.set_yticklabels(['10%', '20%', '30%', '40%',],fontsize = 8)
ax4.set_ylim(0,50)
ax4.set_title('nnHTPn', loc = 'left')
# =============================================================================
#
# =============================================================================
my_legend = fig1.legend(bars_1, my_label, loc = 'lower center', facecolor='black', ncol = 4, bbox_to_anchor=(0.5,0))
for text in my_legend.get_texts():
text.set_color("White")
fig1.tight_layout()
TBH - 我能想象到的唯一解决方法是预置或附加换行符,如下所示:
ax4.set_xticklabels(np.array(['Chloropyrazine', 'DMSO', 'HCl', 'nnTrifluoroaceticnanhydride ',
'PrNH2', 'IPAc', 'Phosphoricnacidn']))
当然,专用功能会更好...