缩放y轴并在Matplotlib图中设置ytick标签

时间:2020-05-23 18:22:31

标签: python matplotlib

我想用一个例子来解释我的查询,这是一个示例代码片段

x = [i for i in range(1, 129)]
y = [i**2 for i in x]
plt.plot(x, y)
plt.show()

输出:

enter image description here

我想要缩放y轴,以便它仅显示三个等距的ytick标签:[0,5000,15000],为此,我使用了matplotlib的set_yticks功能:

x = [i for i in range(1, 129)]
y = [i**2 for i in x]

plt.plot(x, y)
plt.yticks([0, 7500, 15000], [0, 5000, 15000])
plt.show()

输出:

enter image description here

虽然ytick标签如我所愿显示,但曲线没有改变,我希望曲线会根据新的ytick标签进行缩放。我不想更改图中的xtick标签。有什么办法吗?

2 个答案:

答案 0 :(得分:0)

我认为您在正确的轨道上...

x = [i for i in range(1, 129)]
y = [i**2 for i in x]
plt.plot(x, y)
plt.yticks([0, 7500, 15000])
plt.show()

应给出3个等距的y勾号。

x = [i for i in range(1, 129)]
y = [i**2 for i in x]
plt.plot(x, y)
plt.yticks([0, 5000, 15000])
plt.show()

给出您提到的y滴答声,但它们不会均匀分布。

答案 1 :(得分:0)

您可以在此处使用matplotlib.ticker.MultipleLocator来放置等距的 Y轴

{
            android: {
                notification: {
                    defaultSound: true,
                    notificationCount: 1,
                    sound: 'notifications.wav',
                    channelId: 'fcm_default_channel'
                },
                ttl: 20000,
                collapseKey
            },
            apns: {
                payload: {
                    aps: {
                        badge: 1,
                        sound: 'default'
                    }
                }
            },
            notification: {
                title,
                body: message,
            },
            token: token
        };

输出: enter image description here