如何在轴的末端显示最小值和最大值

How to show min and max values at the end of the axes

我生成如下图:

from   pylab              import *
import numpy              as np
import matplotlib.pyplot  as plt
import matplotlib.ticker
import matplotlib.ticker as ticker

rcParams['axes.linewidth']   = 2 # set the value    globally
rcParams['font.size']        = 16# set the value globally
rcParams['font.family']      = ['DejaVu Sans']
rcParams['mathtext.fontset'] = 'stix'
rcParams['legend.fontsize']  = 24
rcParams['axes.prop_cycle']  = cycler(color=['grey','b','g','r','orange']) 
rc('lines', linewidth=2, linestyle='-',marker='o')
rcParams['axes.xmargin'] = 0
rcParams['axes.ymargin'] = 0

t = arange(0,21,1) 
v = 2.0
s = v*t

plt.figure(figsize=(12, 4))
plt.plot(t,s,label='$s=%1.1f\cdot t$'%v)
plt.title('Wykres drogi w czasie $s=v\cdot t$')
plt.xlabel('Czas $t$, s')
plt.ylabel('Droga $s$, m')

plt.autoscale(enable=True, axis='both', tight=None)
legend(loc='best')
plt.xlim(min(t),max(t))
plt.ylim(min(s),max(s))
plt.grid()

plt.show()

当我将值 t = arange(0,21,1) 例如更改为 t = arange(0,20,1) 时,例如在 x 轴上给我最大值 = 19.0 我的最大值从 x 轴排出。 y轴当然也是同样的情况。

我的问题是如何强制 matplotlib 始终生成坐标轴上最大值的绘图 在坐标轴的末端 就像应该始终用于我的目的或者应该是可以选择喜欢的选项吗?

Imiage from my program in Fortan I did some years ago

Matplotlib 比我使用的效率更高,但应该有这样的选项(上图)。

通过这种方式,我始终可以观察文本中的最大最小值 windows 或采取额外的步骤来确保最大最小值。我想从轴上读取它们,问题是……mathplotlib 中是否有这样的可能性???如果不是我会关闭 post.

Axes I am thinking about more or less

我看到了两种解决问题的方法。

设置坐标轴自动限位方式为整数

rcParams 中,您可以使用

rcParams['axes.autolimit_mode'] = 'round_numbers'

并用最小值和最大值关闭手动轴限制

<s>plt.xlim(最小(t),最大(t))
plt.ylim(最小值(s),最大值(s))</s>

这将产生下图。尽管如此,轴的极值仍显示在最接近的“整数”处,但用户可以大致了解数据范围限制。如果你需要显示准确的值,你可以从rcParams.

中看到不能直接使用的第二种解决方案

或 – 手动生成坐标轴刻度

此解决方案意味着明确要求给定数量的报价。我想有一种方法可以根据轴的大小等对其进行自动化。但是如果每次处理相同的图形大小时或多或少,您可以手动确定固定的刻度数。这可以用

来完成
plt.xlim(min(t),max(t))
plt.ylim(min(s),max(s))
plt.xticks(np.linspace(t.min(), t.max(), 7))  # arbitrary chosen
plt.yticks(np.linspace(s.min(), s.max(), 5))  # arbitrary chosen

生成了下面的图像,与您的图像示例非常相似。