2

我正在生成一个散点图,我想知道是否可以避免网格线和绘图上的文本之间的任何重叠。

示例图

例如,第一个点的文本在网格线上,这使得它难以阅读。

我的代码如下:

for i, j in zip(path_loss_list,throughput_values):
    plt.annotate( "%s" %str(j), xy=(i,j), xytext=(-5, 5), ha='right', textcoords='offset points')
4

2 回答 2

2

我认为如果您将所有文本直接放在绘图点上方,那么您应该没有问题。试试,例如:

    for i, j in zip(path_loss_list,throughput_values):
        plt.annotate( "%s" %str(j), xy=(i,j), xytext=(0, 5), ha='right', textcoords='offset points')

但是,这可能会导致与右边的点有一些重叠,在这种情况下,您可以更改xytext = (0, 8). 您的所有点可能没有一致的解决方案。因此,您可能必须逐点指定文本高度,例如:

   for i, j in zip(path_loss_list,throughput_values):
       if not j> 59:
           plt.annotate( "%s" %str(j), xy=(i,j), xytext=(-5, 5), ha='right', textcoords='offset points')
       else:
          plt.annotate( "%s" %str(j), xy=(i,j), xytext=(0, 5), ha='right', textcoords='offset points')

这将移动最高点的文​​本。您可以推断 y = 54.615。

于 2012-11-06T19:14:57.097 回答
2

如果我理解正确,您希望网格线位于您的笔记和点下方。为此,请使用ax.set_axisbelow(True),其中 ax 是包含网格线的轴。

http://matplotlib.org/api/axes_api.html?highlight=set_axisbelow#matplotlib.axes.Axes.set_axisbelow

您也可以将其设置为脚本的参数,这样您就不必每次跟踪它时都更改它。另外,很简单,只要matplotlib.rc('axes', axisbelow=True)

要了解有关 rcParams 的更多信息,请查看http://matplotlib.org/api/matplotlib_configuration_api.html#matplotlib.rc

以及参数列表http://matplotlib.org/users/customizing.html?highlight=rcparams

于 2012-12-06T16:42:44.010 回答