我试图修复python如何绘制我的数据。 说:
x = [0,5,9,10,15]
y = [0,1,2,3,4]
matplotlib.pyplot.plot(x,y)
matplotlib.pyplot.show()
x轴的刻度以5为间隔绘制。有没有办法让它显示1的间隔?
我试图修复python如何绘制我的数据。 说:
x = [0,5,9,10,15]
y = [0,1,2,3,4]
matplotlib.pyplot.plot(x,y)
matplotlib.pyplot.show()
x轴的刻度以5为间隔绘制。有没有办法让它显示1的间隔?
当前回答
我喜欢这个解决方案(来自Matplotlib绘图Cookbook):
import matplotlib.pyplot as plt
import matplotlib.ticker as ticker
x = [0,5,9,10,15]
y = [0,1,2,3,4]
tick_spacing = 1
fig, ax = plt.subplots(1,1)
ax.plot(x,y)
ax.xaxis.set_major_locator(ticker.MultipleLocator(tick_spacing))
plt.show()
这个解决方案让你通过给ticker.MultipleLocater()的数字显式控制刻度间距,允许自动确定限制,并且便于以后读取。
其他回答
我想出了一个不优雅的解决方案。假设我们有X轴和X中每个点的标签列表。
Example:import matplotlib.pyplot as plt
x = [0,1,2,3,4,5]
y = [10,20,15,18,7,19]
xlabels = ['jan','feb','mar','apr','may','jun']
Let's say that I want to show ticks labels only for 'feb' and 'jun'
xlabelsnew = []
for i in xlabels:
if i not in ['feb','jun']:
i = ' '
xlabelsnew.append(i)
else:
xlabelsnew.append(i)
Good, now we have a fake list of labels. First, we plotted the original version.
plt.plot(x,y)
plt.xticks(range(0,len(x)),xlabels,rotation=45)
plt.show()
Now, the modified version.
plt.plot(x,y)
plt.xticks(range(0,len(x)),xlabelsnew,rotation=45)
plt.show()
这是一个老话题了,但我偶尔会遇到这个问题,然后做了这个功能。非常方便:
import matplotlib.pyplot as pp
import numpy as np
def resadjust(ax, xres=None, yres=None):
"""
Send in an axis and I fix the resolution as desired.
"""
if xres:
start, stop = ax.get_xlim()
ticks = np.arange(start, stop + xres, xres)
ax.set_xticks(ticks)
if yres:
start, stop = ax.get_ylim()
ticks = np.arange(start, stop + yres, yres)
ax.set_yticks(ticks)
像这样控制刻度的一个警告是,一个人不再享受在添加一行后最大刻度的交互式自动更新。然后做
gca().set_ylim(top=new_top) # for example
并再次运行resadjust函数。
另一种方法是设置轴定位器:
import matplotlib.ticker as plticker
loc = plticker.MultipleLocator(base=1.0) # this locator puts ticks at regular intervals
ax.xaxis.set_major_locator(loc)
根据您的需要,有几种不同类型的定位器。
下面是一个完整的例子:
import matplotlib.pyplot as plt
import matplotlib.ticker as plticker
x = [0,5,9,10,15]
y = [0,1,2,3,4]
fig, ax = plt.subplots()
ax.plot(x,y)
loc = plticker.MultipleLocator(base=1.0) # this locator puts ticks at regular intervals
ax.xaxis.set_major_locator(loc)
plt.show()
我喜欢这个解决方案(来自Matplotlib绘图Cookbook):
import matplotlib.pyplot as plt
import matplotlib.ticker as ticker
x = [0,5,9,10,15]
y = [0,1,2,3,4]
tick_spacing = 1
fig, ax = plt.subplots(1,1)
ax.plot(x,y)
ax.xaxis.set_major_locator(ticker.MultipleLocator(tick_spacing))
plt.show()
这个解决方案让你通过给ticker.MultipleLocater()的数字显式控制刻度间距,允许自动确定限制,并且便于以后读取。
你可以使用plt.xticks显式地设置你想要标记的位置:
plt.xticks(np.arange(min(x), max(x)+1, 1.0))
例如,
import numpy as np
import matplotlib.pyplot as plt
x = [0,5,9,10,15]
y = [0,1,2,3,4]
plt.plot(x,y)
plt.xticks(np.arange(min(x), max(x)+1, 1.0))
plt.show()
(np。使用arange而不是Python的range函数,以防min(x)和max(x)是浮点数而不是整数。)
plt。Plot(或ax.plot)函数将自动设置默认的x和y限制。如果希望保留这些限制,而只是改变标记的步长,那么可以使用ax.get_xlim()来发现Matplotlib已经设置了哪些限制。
start, end = ax.get_xlim()
ax.xaxis.set_ticks(np.arange(start, end, stepsize))
默认的刻度格式化器应该能很好地将刻度值舍入为合理的有效数字。但是,如果希望对格式有更多的控制,可以定义自己的格式化程序。例如,
ax.xaxis.set_major_formatter(ticker.FormatStrFormatter('%0.1f'))
下面是一个可运行的例子:
import numpy as np
import matplotlib.pyplot as plt
import matplotlib.ticker as ticker
x = [0,5,9,10,15]
y = [0,1,2,3,4]
fig, ax = plt.subplots()
ax.plot(x,y)
start, end = ax.get_xlim()
ax.xaxis.set_ticks(np.arange(start, end, 0.712123))
ax.xaxis.set_major_formatter(ticker.FormatStrFormatter('%0.1f'))
plt.show()