我有以下代码在 matplotlib 中绘制一些线条。我试图用透明圆圈而不是用圆圈填充的标准实体来显示点。
我怎样才能在图表中的每个圆圈之前和之后留出一点间隙,这样虚线就不会碰到它们。我认为这在这里看起来会更好,因为我只有点上的数据,所以中间的线不代表真实数据。
import matplotlib.pyplot as plt
import numpy as np
t = np.array([0.19641715476064042,
0.25,
0.34,
0.42])
c = np.array([0.17,
0.21,
0.27,
0.36])
plt.plot(t, '-go', markerfacecolor='w', linestyle= 'dashed', label='n=20')
plt.plot(c, '-bo', markerfacecolor='w', linestyle= 'dashed', label='n=22')
plt.show()
Run Code Online (Sandbox Code Playgroud)这就是 matplotlib 代码目前给我的。

这就是我希望它最终的样子(显然有不同的数据)。

要注意的是你似乎滥用FMT格式字符串(如"-go")在您的来电plot。实际上对于虚线fmt应该更像"--go". 我个人倾向于发现关键字参数的使用更清晰,即使更冗长(在您的情况下,linestyle="dashed"在fmt字符串上占优势)
http://matplotlib.org/api/axes_api.html?highlight=plot#matplotlib.axes.Axes.plot
无论如何,在尝试重现所需的情节下面:
import matplotlib.pyplot as plt
import numpy as np
t = np.array([0.19641715476064042,
0.25, 0.34, 0.42])
c = np.array([0.17, 0.21, 0.27, 0.36])
def my_plot(ax, tab, c="g", ls="-", marker="o", ms=6, mfc="w", mec="g", label="",
zorder=2):
"""
tab: array to plot
c: line color (default green)
ls: linestyle (default solid line)
marker: kind of marker
ms: markersize
mfc: marker face color
mec: marker edge color
label: legend label
"""
ax.plot(tab, c=c, ms=0, ls=ls, label=label, zorder=zorder-0.02)
ax.plot(tab, c=c, marker=marker, ms=ms, mec=mec, mfc=mfc, ls="none",
zorder=zorder)
ax.plot(tab, c=c, marker=marker, ms=ms*4, mfc="w", mec="w", ls="none",
zorder=zorder-0.01)
my_plot(plt, t, c="g", mec="g", ls="dashed", label="n=20")
my_plot(plt, c, c="b", mec="b", ls="dashed", label="n=22")
plt.legend(loc=2)
plt.show()
Run Code Online (Sandbox Code Playgroud)

还可以考虑阅读官方文档中的图例指南:http : //matplotlib.org/users/legend_guide.html?highlight= legend% 20guide
| 归档时间: |
|
| 查看次数: |
804 次 |
| 最近记录: |