Matplotlib 设置轴刻度间隔相等的简单方法?
Matplotlib simple way to set axes tick intervals equal?
是否有一种在子图中起作用的单线方式,可以将我的 x 轴刻度 间隔 设置为等于我的 y 轴刻度间隔?
有很多关于在 matplotlib 中设置刻度的问题,但它们似乎都涉及重建刻度数组。
import matplotlib.pyplot as plt
plt.style.use('seaborn-whitegrid')
x = [-1,5,8,2,1,1,4]
y = [0,5,-1,7,-3,3,2]
fig = plt.figure(figsize=(10,10))
ax1 = plt.subplot(311)
ax1.scatter(x,y);
ax1.set_aspect('equal')
在我的脑海中,我唯一能想到的解决方案是这个(虽然它是 3 行或更确切地说是 2 行):
>>> import matplotlib.pyplot as plt
>>> import matplotlib.ticker as ticker
>>> plt.style.use('seaborn-whitegrid')
>>> x = [-1,5,8,2,1,1,4]
>>> y = [0,5,-1,7,-3,3,2]
>>> fig = plt.figure(figsize=(10,10))
>>> ax1 = plt.subplot(311)
>>> ax1.scatter(x,y)
>>> ax1.set_aspect('equal')
>>> tick_spacing = 2 #You can skip adding this and put 2 in place in below lines
>>> ax1.xaxis.set_major_locator(ticker.MultipleLocator(tick_spacing))
>>> ax1.yaxis.set_major_locator(ticker.MultipleLocator(tick_spacing))
如果您在对此答案的评论中找到另一种方法,请分享。
是否有一种在子图中起作用的单线方式,可以将我的 x 轴刻度 间隔 设置为等于我的 y 轴刻度间隔?
有很多关于在 matplotlib 中设置刻度的问题,但它们似乎都涉及重建刻度数组。
import matplotlib.pyplot as plt
plt.style.use('seaborn-whitegrid')
x = [-1,5,8,2,1,1,4]
y = [0,5,-1,7,-3,3,2]
fig = plt.figure(figsize=(10,10))
ax1 = plt.subplot(311)
ax1.scatter(x,y);
ax1.set_aspect('equal')
在我的脑海中,我唯一能想到的解决方案是这个(虽然它是 3 行或更确切地说是 2 行):
>>> import matplotlib.pyplot as plt
>>> import matplotlib.ticker as ticker
>>> plt.style.use('seaborn-whitegrid')
>>> x = [-1,5,8,2,1,1,4]
>>> y = [0,5,-1,7,-3,3,2]
>>> fig = plt.figure(figsize=(10,10))
>>> ax1 = plt.subplot(311)
>>> ax1.scatter(x,y)
>>> ax1.set_aspect('equal')
>>> tick_spacing = 2 #You can skip adding this and put 2 in place in below lines
>>> ax1.xaxis.set_major_locator(ticker.MultipleLocator(tick_spacing))
>>> ax1.yaxis.set_major_locator(ticker.MultipleLocator(tick_spacing))
如果您在对此答案的评论中找到另一种方法,请分享。