@hashcode55 的解决方案不起作用,因为这是我在找到此线程之前尝试的方法。
在我看来,这只是一个“错误”:
plt.yscale('log')
plt.autoscale(enable=True, axis='y')
不兼容。
这是我的示例代码:
import matplotlib.pyplot as plt
import matplotlib
import random
import numpy as np
# generate some random data and add it to the plot
x = np.array(range(1,100))
y = np.maximum(np.ones(99), np.random.randn(99))
plt.plot(x, y, markersize=4, marker='.', color='red')
# format
ax = plt.gca()
plt.ylabel('LOGARITHMIC SCALE')
plt.yscale('log')
plt.minorticks_on
ax.yaxis.set_major_formatter(matplotlib.ticker.ScalarFormatter())
ax.yaxis.set_minor_formatter(matplotlib.ticker.ScalarFormatter())
plt.autoscale(enable=True, axis='y')
#ax.set_ylim([np.min(y), np.max(y)])
#plot
plt.show()
产生:
对数刻度,但显然不是自动刻度
如果我从这一行中删除 cmets:
ax.set_ylim([np.min(y), np.max(y)])
然后它实际上按照自动缩放的预期绘制:
很好,但是如果我失去了对绘图上 y 值的参考怎么办?
虽然这个解决方案/答案是这个示例问题的一个很好的“破解”,但它对于我的情况并不是一个可靠的解决方案,因为我的图表是 a) 实时的;每分钟不断更新 b) 包含许多图 c) 正在丢弃超过过去 24 小时的数据;因此,如果每次在实时会话中从情节中添加或删除某些内容时都实施这样的解决方案,那么这种解决方案将变得非常糟糕。
我会对真正的内置“自动缩放”解决方案感兴趣,如果存在,它适用于 log y 比例,我可以使用 plt.ion() 自动更新
到那时,这个呢:
h/t @David Z
How to extract data from matplotlib plot
#if you do need to get data out of a plot, I think this should do it
gca().get_lines()[n].get_xydata()
#Alternatively you can get the x and y data sets separately:
line = gca().get_lines()[n]
xd = line.get_xdata()
yd = line.get_ydata()
在我们手头的情况下实现(用额外的蓝线来测试多行):
import matplotlib.pyplot as plt
import matplotlib
import random
import numpy as np
# generate some random data and add it to the plot
x = np.array(range(1,100))
y = np.maximum(np.ones(99), np.random.randn(99))
plt.plot(x, y, markersize=4, marker='.', color='red')
# test for compatibility with multilpes lines
x = np.array(range(1,100))
y = np.maximum(np.ones(99), 1.5*np.random.randn(99))
plt.plot(x, y, markersize=4, marker='.', color='blue')
# format
ax = plt.gca()
plt.ylabel('LOGARITHMIC SCALE')
plt.yscale('log')
plt.minorticks_on
ax.yaxis.set_major_formatter(matplotlib.ticker.ScalarFormatter())
ax.yaxis.set_minor_formatter(matplotlib.ticker.ScalarFormatter())
#plt.autoscale(enable=True, axis='y')
#####################################################
#force 'autoscale'
#####################################################
yd = [] #matrix of y values from all lines on plot
for n in range(len(plt.gca().get_lines())):
line = plt.gca().get_lines()[n]
yd.append(line.get_ydata())
ax.set_ylim([0.9*np.min(yd), 1.1*np.max(yd)])
#####################################################
#plot
plt.show()
本质上,它是从绘图的所有行中提取所有 y 数据,找到最大值和最小值;然后通过set_ylim 实现它们; “强制”自动缩放
产量:
瞧!
对于我的情况,我的格式有点复杂:
plt.plot((x1,x2), (y1,y2))
在产生“值错误”的矩阵情况下创建矩阵
为此我不得不使用:
yd = [item for sublist in yd for item in sublist]
h/t @Alex Martelli
Making a flat list out of list of lists in Python
这是最终产品:
#####################################################
#force 'autoscale'
#####################################################
yd = [] #matrix of y values from all lines on plot
for n in range(len(plt.gca().get_lines())):
line = plt.gca().get_lines()[n]
yd.append((line.get_ydata()).tolist())
yd = [item for sublist in yd for item in sublist]
ax.set_ylim([0.9*np.min(yd), 1.1*np.max(yd)])
#####################################################