python - matplotlib 中的自定义标记

标签 python matplotlib

我有一个 matplotlib 脚本,用于绘制多条线,显示 8、16、32 和 64 位配置的结果。该图有点拥挤,为了使其更易于阅读,我想添加这些数字作为标记。另外,我更喜欢在文本周围有一个圆圈。这意味着标记将是带有圆圈的文本。

我的代码是这样的:

#!/usr/bin/env python
import matplotlib.pyplot as plt
import matplotlib.ticker as mtick
import numpy as np

# Turn off DISPLAY
#matplotlib.use('Agg')
import pylab

fig = plt.figure()
ax = fig.add_subplot(1,1,1)

# T = 16
plt.plot((1,2,3,4),(18.25,15.5,14.15,14.07), linestyle='--', lw=2, marker="D", markersize=7, color='r', label="Stepped Parity 16-bit");
# T = 32
plt.plot((1,2,3,4),(11.90,10.29,9.23,9.11), linestyle='--', lw=2, marker="*", markersize=10, color='r', label="Stepped Parity 32-bit");
# T = 64
plt.plot((1,2,3,4),(6.64,6.20,5.53,5.39), linestyle='--', lw=2, marker="H", markersize=8, color='r', label="Stepped Parity 64-bit");

# T = 16
plt.plot((1,2,3,4),(25.52,22.51,21.05,21.02), linestyle='-', lw=2, marker="D", markersize=7, color='b', label="Interleaving 16-bit");
# T = 32
plt.plot((1,2,3,4),(17.18, 15.13, 13.56, 13.51), linestyle='-', lw=2, marker="*", markersize=10, color='b', label="Interleaving 32-bit");
# T = 64
plt.plot((1,2,3,4),(8.84, 8.31, 7.97, 7.91), linestyle='-', lw=2, marker="h", markersize=8, color='b', label="Interleaving 64-bit");

plt.xlim([0.75, 4.25])
plt.ylim([0, 30])

plt.xticks([1,2,3,4],[512, 1024, 2048, 4096], fontsize=14)
plt.yticks(fontsize=14)

plt.xlabel('# of Words', fontsize=16)
plt.ylabel('Area Overhead', fontsize=16)

# Percentage sign in Y-axis
fmt = '%.0f%%' # Format you want the ticks, e.g. '40%'
yticks = mtick.FormatStrFormatter(fmt)
ax.yaxis.set_major_formatter(yticks)  

plt.legend(ncol=2)

# Save in PDF file
pylab.savefig("Output.pdf", dpi=400, bbox_inches='tight', pad_inches=0.05)

最佳答案

您可以将所有古怪的 latex 作为您想要的标记,包括文本。但它可能最终看起来会很糟糕...... 但至少现在你有一个 wider arrangement可供选择的符号。

plt.plot((1,2,3,4),(11.90,10.29,9.23,9.11), linestyle='--',
         lw=2, marker="$32$", markersize=17, color='r',
         label="Stepped Parity 32-bit")

只需确保它们以 described in the manual 形式发送到 "$ $" 标记内即可.

Python 3 matplotlib 1.3.1 Win7 已测试。另外,请以 65 个字符长度换行。阅读起来更加舒适。

关于python - matplotlib 中的自定义标记,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28706385/

相关文章:

python - matplotlib 图表可以交互吗?

python - 在绘图上绘制括号

python - Matplotlib 动画 fill_between 形状

python - 循环遍历字典键的元素而不是枚举所有元素

python - 如何在散点图上有多个分类标记

python - 使用 python-pptx 将 matplotlib 图形保存在 ppt 文件中,而不保存图形

python - 从 matplotlib 中的轮廓获取坐标?

Python训练Kmeans算法预测图像的主色

python - scrapy:使用 itemLoader 填充嵌套项目

python - 将 Linux 内核 add_key 和 keyctl 系统调用与组 key 环一起使用