简体   繁体   English

将自定义对数刻度位置添加到 matplotlib

[英]Add custom logarithmic tick location to matplotlib

I have a line plot, where the x-axis is logarithmic.我有一条线 plot,其中 x 轴是对数的。 I would like to mark a specific value on this axis (40000).我想在这个轴上标记一个特定的值(40000)。 This means I would like to insert a custom major tick at this location.这意味着我想在这个位置插入一个自定义的主要刻度。 I've tried to explicitly set the xticks using set_xticks() generated from numpy's logspace which kinda works, but does not automatically add a label for the extra tick.我尝试使用从 numpy 的日志空间生成的set_xticks()显式设置logspace ,这有点工作,但不会自动为额外的刻度添加 label。 How can I do that?我怎样才能做到这一点? Also, can I customize the grid line for this extra tick?另外,我可以为这个额外的刻度自定义网格线吗?

fig = plt.figure(figsize=(12, 8))
ax = fig.add_subplot(1, 1, 1)
ax.plot(np.power(sizes, 2), hsv_mat_times, label="HSV Material Shader")
ax.plot(np.power(sizes, 2), brick_mat_times, label="Brick Material Shader")
ax.set_title("Python Shader Rendering Performance")
ax.set_xlabel("Number of Pixels")
ax.set_ylabel("CPU Rendering Time (ms)")
formatter = FuncFormatter(lambda x, pos: "{:.3}".format(x / 1000000))
xticks = np.logspace(2,6,num=5)
xticks = np.insert(xticks,4,200*200)
ax.set_xscale("log")
ax.set_xticks(xticks)
ax.legend()
plt.show()

And this is the output I get with the desired output marked.这是我得到的 output,上面标有所需的 output。 在此处输入图像描述

Edit Thanks for the answers, they are great solutions using FuncFormatter .编辑感谢您的回答,它们是使用FuncFormatter的绝佳解决方案。 I did however stumble across a solution using axvline to add a vertical line at 200x200.然而,我确实偶然发现了一个使用axvline在 200x200 处添加垂直线的解决方案。 This is my new solution, using minor ticks for the extra tick.这是我的新解决方案,使用次要刻度作为额外刻度。

def log_format(x, pos):
    return "$200\\times 200$"

fig = plt.figure(figsize=(12, 8))
ax = fig.add_subplot(1, 1, 1)
ax.plot(np.power(sizes, 2), hsv_mat_times, label="HSV Material Shader")
ax.plot(np.power(sizes, 2), brick_mat_times, label="Brick Material Shader")
ax.plot(np.power(sizes, 2), hsv_mat_times2, label="PLACEHOLDER FOR 3rd shader")
ax.set_title("Python Shader Rendering Performance", fontsize=18)
ax.set_xlabel("Number of Pixels")
ax.set_ylabel("CPU Rendering Time (ms)")
ax.set_xscale("log")
ax.set_xticks([200*200], minor=True)
ax.xaxis.grid(False, which="minor")
ax.axvline(200*200, color='white', linestyle="--")
ymin,ymax = ax.get_ylim()
ax.text(200*200 - 200*50, (ymax-ymin)/2, "Default render size", rotation=90, va="center", style="italic")
ax.xaxis.set_minor_formatter(FuncFormatter(log_format))
ax.legend()
plt.show()

Which produces the final result:产生最终结果: 在此处输入图像描述

You can give a try to use ScalarFormatter() after setting the xticks.您可以在设置 xticks 后尝试使用ScalarFormatter()

xticks = np.logspace(2,6,num=5)
xticks = np.insert(xticks,4,200*200)
ax.set_xscale("log")
ax.set_xticks(xticks)

# add this line
ax.get_xaxis().set_major_formatter(matplotlib.ticker.ScalarFormatter())

A similar question has been asked previously.之前有人问过类似的问题。 You can check that as well set ticks with logarithmic scale您还可以检查是否使用对数刻度设置刻度

There is a post explaining in-depth for the log scalehttps://atmamani.github.io/cheatsheets/matplotlib/matplotlib_2/有一篇文章深入解释了对数刻度https://atmamani.github.io/cheatsheets/matplotlib/matplotlib_2/

I hope it helps:-)我希望它有帮助:-)

from matplotlib.ticker import FuncFormatter, LogFormatterMathtext
ax.xaxis.set_major_formatter(FuncFormatter(lambda x,_: '$\\mathdefault{0.4*10^{5}}$' if x == 40000 else LogFormatterMathtext()(x)))

Format set corresponding grid line:格式设置对应的网格线:

ax.xaxis.get_gridlines()[4].set_c('r')

例子

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM