简体   繁体   English

在matplotlib图上设置自定义刻度线间距

[英]Set custom tick spacing on matplotlib graph

I want to set the space between ticks to a custom distance and not automatically generated. 我想将节拍之间的空格设置为自定义距离而不是自动生成。 I want to achieve what this graph does on the y-axis. 我想实现这个图在y轴上的作用。 https://pasteboard.co/H0XUbpq.jpg https://pasteboard.co/H0XUbpq.jpg

I found ways on how to set custom ticks but then the spacing is still the same on the axis and then most of the ticks are still close to each other. 我找到了如何设置自定义刻度的方法,但是轴上的间距仍然相同,然后大多数刻度仍然彼此接近。 So I want the tick intervals to be different than the space between the ticks on the y-axis like the graph I linked to above. 所以我希望刻度线间隔与y轴上刻度线之间的间距不同,就像我上面链接的图形一样。

I think the image you post is log plot in y axis . 我认为您发布的图像是y轴的对数图 Each tick value in y axis is the lower tick value multiplied by 2. In normal plot, their distance is not the same, but in log plot, their distance is the same. y轴中的每个刻度值是较低的刻度值乘以2.在正常绘图中,它们的距离不相同,但在对数图中,它们的距离是相同的。

Since you do not have the data, I will show a toy example demonstrating how to use log plot in y axis: 由于您没有数据,我将展示一个玩具示例,演示如何在y轴上使用对数图:

import matplotlib.pyplot as plt
import matplotlib
import numpy as np

x = np.linspace(20, 51, 7)

y1 = np.exp(x**0.2)
y2 = np.exp(x**0.4)
y3 = np.exp(x**0.6)
y4 = np.exp(x**0.8)

fig, ax = plt.subplots(ncols=1, nrows=1)

ax.plot(x, y1, x, y2, x, y3, x, y4)
ax.set_yscale('log')
ax.minorticks_off()
ax.set_xticks(range(18, 54, 5))
ax.grid(linestyle='--')

ax.grid(linestyle='--')

plt.show()

The produced plot is shown below, 制作的情节如下所示, 在此输入图像描述

You can use seperate FuncFormatters for the axis: 您可以为轴使用单独的FuncFormatters:

from matplotlib.ticker import FuncFormatter
from matplotlib.pyplot import show
import matplotlib.pyplot as plt
import numpy as np

a=np.random.random((100,100))

# create scaled formatters / for Y with Atom prefix
formatterY = FuncFormatter(lambda y, pos: '{0:g}'.format(y))
formatterX = FuncFormatter(lambda x, pos: '{0:g}'.format(x))

# apply formatters 
fig, ax = plt.subplots()
ax.yaxis.set_major_formatter(formatterY)
ax.xaxis.set_major_formatter(formatterX)

plt.imshow(a) 

# create labels
plt.xlabel('x label')
plt.ylabel('y label')
plt.xticks(list(range(0, 100,5))) 

plt.yticks(list(range(100, 0,-20)))

plt.show()

绘制图像

to style them idependently. 独立地塑造它们。 See pylab_examples - might give you a starting point. 请参阅pylab_examples - 可能会给您一个起点。

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

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