使用 matplotlib 进行轴标记太稀疏 [英] Axis labelling with matplotlib too sparse

查看:165
本文介绍了使用 matplotlib 进行轴标记太稀疏的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

Matplotlib 尝试智能地标记此 x 轴上的刻度,但它有点过于稀疏.应该有 0 的标签,可能还有 10 和 100 的标签.

Matplotlib tries to label the ticks on this x-axis intelligently, but it is a little too sparse. There should be a label for 0 and maybe one for 10 and 100.

这是生成图形的代码.如何使 x 轴上的标签更详细?

This is the code which produces the figure. How can I make the labelling on the x-axis more verbose?

   def timePlot2(data, saveto=None, leg=None, annotate=False, limits=timeLimits, labelColumn="# Threads", valueColumn="Average (s)", size=screenMedium):
        labels = list(data[labelColumn])
        figure(figsize=size)
    ax = gca()
    ax.grid(True)
    xi = range(len(labels))
    rts = data[valueColumn] # running time in seconds
    ax.scatter(rts, xi, color='r')
    if annotate:
        for i,j in zip(rts, xi):
            ax.annotate("%0.2f" % i, xy=(i,j), xytext=(7,0), textcoords="offset points")

    ax.set_yticks(range(len(labels)))
    ax.set_yticklabels(labels)
    ax.set_xscale('log')
    plt.xlim(limits)
    if leg:
        legend(leg, loc="upper left", fontsize=10)
    else:
        legend([r"$t$"], fontsize=10)
    plt.draw()
    if saveto:
        plt.savefig(saveto, transparent=True, bbox_inches="tight")

推荐答案

您可以在示例中使用 ax.set_xticks() 定义自己的 X-Axis-Ticks 及其标签>

You can define your own X-Axis-Ticks together with their labels using ax.set_xticks(), in your example

ax.set_xticks((10,100,1000))

应该可以解决问题.

如果你想保留 10^x-labels,你可以显式添加标签:

If you like to keep the 10^x-labels, you can add the labels explicitly:

ax.set_xticks((10,100,1000),('$10^1$','$10^2$','$10^3$'))

这篇关于使用 matplotlib 进行轴标记太稀疏的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆