pyplot:以对数刻度间距绘制 x 轴,但不以指数形式对其进行标记 [英] pyplot: plotting x-axis in log scale spacing but not labeling it in exponential form

查看:67
本文介绍了pyplot:以对数刻度间距绘制 x 轴,但不以指数形式对其进行标记的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想绘制两个值 x = [0, 10,20,50,100] y=[1,2,3,10,100] 使用 pylab.我想以对数形式保持 x 轴的间距.但是我想在 x i'e 在 10、20、50、100 处打勾并打印它们,因为它不是 10e1 或 10e2 的形式.我是这样做的:

I would like to plot say two values x = [0, 10,20,50,100] and y=[1,2,3,10,100] using pylab. I want to keep the spacing of x-axis in log form. But I want to tick at the values of x i'e at 10, 20, 50, 100 and print them as it not in the form of 10e1 or 10e2. I am doing it as follows:

 import matplotlib.pylab as plt
 plt.xscale('log')
 plt.plot(x, y)
 plt.xticks(x)
 plt.grid()

但它保留了 10e1, 10e2 形式的值.

But it keeps the values in the form of 10e1, 10e2.

你能帮我吗?

推荐答案

我觉得你想要的是改变x轴的major_formatter?

I think what you want is to change the major_formatter of the x axis?

import matplotlib.pylab as plt
import numpy as np
from matplotlib.ticker import ScalarFormatter

x = [0, 10,20,50,100]
y=[1,2,3,10,100]

plt.plot(x, y)
plt.xscale('log')
plt.grid()

ax = plt.gca()
ax.set_xticks(x[1:]) # note that with a log axis, you can't have x = 0 so that value isn't plotted.
ax.xaxis.set_major_formatter(ScalarFormatter())

plt.show()

这篇关于pyplot:以对数刻度间距绘制 x 轴,但不以指数形式对其进行标记的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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