pyplot:以对数刻度间距绘制x轴,但不以指数形式标记它

时间:2015-09-25 17:39:27

标签: python matplotlib plot

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

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

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

你能帮帮我吗?

2 个答案:

答案 0 :(得分:3)

我认为你想要的是改变x轴的major_formatter

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()

scalar_formatter

答案 1 :(得分:1)

以下

import matplotlib.pyplot as plt

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

f,ax = plt.subplots()
ax.plot(x,y)
ax.set_xscale('log')
ax.set_xticks(x)
ax.set_xticklabels(x)
ax.set_xlim([0,100])

将产生

enter image description here