Max*_*amy 39 python matplotlib
我正在使用matplotlib来绘制数据.这是一个类似的代码:
import matplotlib.pyplot as plt
f, ax = plt.subplots(1)
xdata = [1, 4, 8]
ydata = [10, 20, 30]
ax.plot(xdata, ydata)
plt.show(f)
Run Code Online (Sandbox Code Playgroud)
这显示了y轴从10到30的图形中的线.虽然我对x范围感到满意,但我想将y范围从0开始更改并调整ymax以显示所有内容.
我目前的解决方案是:
ax.set_ylim(0, max(ydata))
Run Code Online (Sandbox Code Playgroud)
但是我想知道是否有办法说:autoscale但是从0开始.
Max*_*amy 62
解决方案实际上很简单:必须在绘图之后设置范围.
import matplotlib.pyplot as plt
f, ax = plt.subplots(1)
xdata = [1, 4, 8]
ydata = [10, 20, 30]
ax.plot(xdata, ydata)
ax.set_ylim(ymin=0)
plt.show(f)
Run Code Online (Sandbox Code Playgroud)
当我尝试使用这种方法时,我已经改变了ymin
之前的绘图,产生了[0,1]的范围.
编辑:ymin
参数将替换为bottom
:
ax.set_ylim(bottom=0)
Run Code Online (Sandbox Code Playgroud)
Wei*_*gTu 26
试试这个
import matplotlib.pyplot as plt
xdata = [1, 4, 8]
ydata = [10, 20, 30]
plt.plot(xdata, ydata)
plt.ylim(ymin=0) # this line
plt.show()
Run Code Online (Sandbox Code Playgroud)
doc字符串如下:
>>> help(plt.ylim)
Help on function ylim in module matplotlib.pyplot:
ylim(*args, **kwargs)
Get or set the *y*-limits of the current axes.
::
ymin, ymax = ylim() # return the current ylim
ylim( (ymin, ymax) ) # set the ylim to ymin, ymax
ylim( ymin, ymax ) # set the ylim to ymin, ymax
If you do not specify args, you can pass the *ymin* and *ymax* as
kwargs, e.g.::
ylim(ymax=3) # adjust the max leaving min unchanged
ylim(ymin=1) # adjust the min leaving max unchanged
Setting limits turns autoscaling off for the y-axis.
The new axis limits are returned as a length 2 tuple.
Run Code Online (Sandbox Code Playgroud)
请注意,ymin
将在 Matplotlib 3.2 Matplotlib 3.0.2 文档中删除。使用bottom
来代替:
import matplotlib.pyplot as plt
f, ax = plt.subplots(1)
xdata = [1, 4, 8]
ydata = [10, 20, 30]
ax.plot(xdata, ydata)
ax.set_ylim(bottom=0)
plt.show(f)
Run Code Online (Sandbox Code Playgroud)
归档时间: |
|
查看次数: |
59501 次 |
最近记录: |