from matplotlib import pyplot as plt import numpy as np from matplotlib.font_manager import FontProperties import matplotlib.font_manager as font_manager # 设置中文字体,这里以微软雅黑为例 my_font = font_manager.FontProperties(fname="C:/Windows/Fonts/msyh.ttc") font = FontProperties(fname=r"C:\Windows\Fonts\simhei.ttf", size=14) # 指定中文字体路径和字体大小 # 构造数据 x_labels2 = ['A→B','A→C','B→C',"平均"] x_labels = ['A→B$_{1}$', 'A→B$_{2}$', 'A→B$_{3}$', 'A→C$_{1}$', 'A→C$_{2}$', 'A→C$_{1}$', "平均"] y_values = np.array([[90.72,77.86,38.67], [97.42,76.16,42.86], [91.67,72.37,41.56], [97.07,64.25,40.12], [88.80,71.56,45.15], [92.26,68.87,43.32], [92.99,71.85,41.95]]) # 二维数组,每个元素包含 3 个类别的值 y_values2 = np.array([[93.96,71.64,66.04], [96.00,76.56,65.27], [89.51,72.39,64.23], [93.16,73.53,65.18]]) # 绘制多类直方图 x = np.arange(len(x_labels2)) width = 0.2 # 每个类别之间的宽度 fig, ax = plt.subplots() rects1 = ax.bar(x - width, y_values2[:, 0], width, label="提出方法") rects2 = ax.bar(x, y_values2[:, 1], width, label="DCNN") rects3 = ax.bar(x + width, y_values2[:, 2], width, label="DDC") # 设置 x 标签、标题和图例 ax.set_xticks(x) ax.set_xticklabels(x_labels2,fontproperties=font) ax.legend() ax.set_xlabel("迁移诊断任务", fontproperties=font) ax.set_ylabel("诊断精度(%)", fontproperties=font) #ax.set_title("迁移诊断结果对比",fontproperties=font) # 设置中文字体 plt.legend(prop=my_font) plt.subplots_adjust(left=0.12, right=0.9, top=0.9, bottom=0.15) # 调整边缘 plt.show()如何修改上述代码使得“提出方法”,“DCNN”,“DDC”移到图表的上方拍成横着的一行
时间: 2024-04-22 20:26:05 浏览: 254
要将“提出方法”,“DCNN”和“DDC”移到图表的上方,可以使用annotate函数添加文本注释。以下是修改后的代码:
```python
from matplotlib import pyplot as plt
import numpy as np
from matplotlib.font_manager import FontProperties
import matplotlib.font_manager as font_manager
# 设置中文字体,这里以微软雅黑为例
my_font = font_manager.FontProperties(fname="C:/Windows/Fonts/msyh.ttc")
font = FontProperties(fname=r"C:\Windows\Fonts\simhei.ttf", size=14) # 指定中文字体路径和字体大小
# 构造数据
x_labels2 = ['A→B','A→C','B→C',"平均"]
x_labels = ['A→B$_{1}$', 'A→B$_{2}$', 'A→B$_{3}$', 'A→C$_{1}$', 'A→C$_{2}$', 'A→C$_{1}$', "平均"]
y_values = np.array([[90.72,77.86,38.67], [97.42,76.16,42.86], [91.67,72.37,41.56], [97.07,64.25,40.12], [88.80,71.56,45.15], [92.26,68.87,43.32], [92.99,71.85,41.95]]) # 二维数组,每个元素包含 3 个类别的值
y_values2 = np.array([[93.96,71.64,66.04], [96.00,76.56,65.27], [89.51,72.39,64.23], [93.16,73.53,65.18]])
# 绘制多类直方图
x = np.arange(len(x_labels2))
width = 0.2 # 每个类别之间的宽度
fig, ax = plt.subplots()
rects1 = ax.bar(x - width, y_values2[:, 0], width, label="提出方法")
rects2 = ax.bar(x, y_values2[:, 1], width, label="DCNN")
rects3 = ax.bar(x + width, y_values2[:, 2], width, label="DDC")
# 添加文本注释
for i in range(len(x_labels2)):
ax.annotate(f"{y_values2[i, 0]:.2f}", (x[i] - width, y_values2[i, 0] + 2), ha='center', fontproperties=font)
ax.annotate(f"{y_values2[i, 1]:.2f}", (x[i], y_values2[i, 1] + 2), ha='center', fontproperties=font)
ax.annotate(f"{y_values2[i, 2]:.2f}", (x[i] + width, y_values2[i, 2] + 2), ha='center', fontproperties=font)
# 设置 x 标签、标题和图例
ax.set_xticks(x)
ax.set_xticklabels(x_labels2, fontproperties=font)
ax.legend()
ax.set_xlabel("迁移诊断任务", fontproperties=font)
ax.set_ylabel("诊断精度(%)", fontproperties=font)
# 设置中文字体
plt.legend(prop=my_font)
plt.subplots_adjust(left=0.12, right=0.9, top=0.9, bottom=0.15) # 调整边缘
plt.show()
```
这样修改后,"提出方法","DCNN"和"DDC"将会在每个柱状图上方以横向显示。文本注释使用annotate函数,通过指定坐标和对齐方式来放置文本。调整文本的位置可以通过调整y轴坐标的偏移量来实现。
阅读全文