matplotlib 使用线条和形状自定义精度图表

s6fujrry  于 2023-05-23  发布在  其他
关注(0)|答案(1)|浏览(131)

我写了一个简单的代码来构造一个图,它工作得很好。但是,我不知道如何修改它像下面这样?如何在matplotlib中自定义行?

要求:

我的密码

import matplotlib.pyplot as plt

# Data for different models and their corresponding accuracies
models = ['10', '60', '110', '160']
accuracies = [73.2, 75.6, 77.1, 78.3]
transmix_accuracies = [74.8, 76.4, 78.2, 79.1]

# Create a figure and axis object
fig, ax = plt.subplots()

# Plot the accuracy of ViT-based models
ax.plot(models, accuracies, marker='o', label='ViT')
# Plot the accuracy of TransMix models
ax.plot(models, transmix_accuracies, marker='o', label='TransMix')

# Set the chart title and axis labels
#ax.set_title("Improvement of TransMix on ViT-based Models")
ax.set_xlabel("Number of Parameters")
ax.set_ylabel("ImageNet Top-1 Acc (%)")

# Add a legend
ax.legend()

# Show the plot
plt.show()

输出

3bygqnnd

3bygqnnd1#

您可以添加一些说明,如:

# Modify lines with linestyle parameter
ax.plot(models, accuracies, marker='o', ls='--', label='ViT')
ax.plot(models, transmix_accuracies, marker='^', ls='-', label='TransMix')

# Set limits
xmin, xmax = ax.get_xlim()
ymin, ymax = ax.get_ylim()
ymin, ymax = ymin - 2, ymax + 2
ax.set_ylim(ymin, ymax)

# Add arrows
ax.annotate('', xy=(0, transmix_accuracies[0]), xytext=(0, accuracies[0]), arrowprops=dict(arrowstyle='->', ls='--'))
ax.annotate('', xy=(3, transmix_accuracies[3]), xytext=(3, accuracies[3]), arrowprops=dict(arrowstyle='->', ls='--'))

# Add text
ax.text(0.5, ymin+0.5, 'small', ha='center')
ax.text(1.5, ymin+0.5, 'base', ha='center')
ax.text(2.5, ymin+0.5, 'large', ha='center')

# Enable grid
ax.grid(ls='--')

现在,您可以使用annotatetext自定义图形
输出

相关问题