[英]How do I define the range of the Y-Axis in MatPlotLib?
I have read countless posts on stackoverflow suggesting to use plt.ylim(low,high)
to define the desired range.我已经阅读了无数关于 stackoverflow 的帖子,建议使用plt.ylim(low,high)
来定义所需的范围。
I also tried plt.axis([x_low, x_high, y_low, y_high])
.我也试过plt.axis([x_low, x_high, y_low, y_high])
。 But it none of them work for me.但它们都不适合我。
I am trying to plot voltage over time.我正在尝试随着时间的推移 plot 电压。 I have 530 values that I want to plot, which I have stored in a list.我有 530 个值想要 plot 存储在列表中。 The changes between the values are very little.值之间的变化非常小。
The data is in the range [4198, 4199]
数据在[4198, 4199]
范围内
I want to see a flat, horizontal line.我想看到一条平坦的水平线。 Instead, I see a badly scaled plot where the line-graph ends up looking like a bar graph, highlighting the smallest changes, like this:相反,我看到缩放严重的 plot 折线图最终看起来像条形图,突出了最小的变化,如下所示:
enter image description here在此处输入图像描述
Here is the code that I have:这是我拥有的代码:
start = findStart(lines)
end = findEnd(lines, start)
q = enqueueVoltages(start, end)
x = range(len(q))
plt.plot(x, q)
#plt.axis([0,550,3000,4200])
plt.ylabel('Voltage (mV)')
plt.xlabel('Time (s)')
plt.show()
When I try it with the lines defining the desired axes ranges, I get this image: enter image description here当我尝试使用定义所需轴范围的线条时,我得到了这个图像:在此处输入图像描述
For reference, here is the whole script I am running (with the function definitions):作为参考,这是我正在运行的整个脚本(使用 function 定义):
def findStart(lines):
count = 0
for line in lines:
count += 1
if(line.rfind('***') > -1):
start = count
break
return start
def findEnd(lines, start):
count = 0
for line in lines[start:]:
count += 1
if(line.rfind('***') > -1):
end = count + start
break
return end
def enqueueVoltages(lines, start, end):
q = []
counter=0
for i in range(start+1, end-1):
beg = lines[i].find('\t\t')
voltage = lines[i][beg+2:beg+6]
counter += 1
q.append(voltage)
print(voltage)
return q
f = open(data_path, 'r')
lines = f.readlines()
start = findStart(lines)
end = findEnd(lines, start)
q = enqueueVoltages(lines, start, end)
x = range(len(q))
plt.plot(x, q)
plt.ylim([3000,4200])
plt.gca().invert_yaxis()
plt.ylabel('Voltage (mV)')
plt.xlabel('Time (s)')
plt.show()
print("Starting:\t{}\nEnding:\t\t{}\n".format(start, end))
f.close()
Here is the sample data I am processing (.txt file):这是我正在处理的示例数据(.txt 文件):
Start time: Wed Sep 14 15:05:14 2022
***
Wed Sep 14 15:05:16 2022 4199
Wed Sep 14 15:05:17 2022 4199
Wed Sep 14 15:05:18 2022 4199
Wed Sep 14 15:05:20 2022 4199
Wed Sep 14 15:05:21 2022 4199
Wed Sep 14 15:05:22 2022 4199
Wed Sep 14 15:05:23 2022 4199
Wed Sep 14 15:05:24 2022 4199
Wed Sep 14 15:05:25 2022 4199
Wed Sep 14 15:05:26 2022 4199
Wed Sep 14 15:05:27 2022 4199
Wed Sep 14 15:05:29 2022 4199
Wed Sep 14 15:05:30 2022 4199
Wed Sep 14 15:05:31 2022 4199
Wed Sep 14 15:05:32 2022 4199
Wed Sep 14 15:05:33 2022 4199
Wed Sep 14 15:05:34 2022 4199
Wed Sep 14 15:05:35 2022 4199
Wed Sep 14 15:05:36 2022 4199
Wed Sep 14 15:05:38 2022 4199
Wed Sep 14 15:05:39 2022 4199
Wed Sep 14 15:05:40 2022 4199
Wed Sep 14 15:05:41 2022 4199
Wed Sep 14 15:05:42 2022 4199
Wed Sep 14 15:05:43 2022 4199
Wed Sep 14 15:05:44 2022 4199
Wed Sep 14 15:05:46 2022 4199
Wed Sep 14 15:05:47 2022 4199
Wed Sep 14 15:05:48 2022 4199
Wed Sep 14 15:05:49 2022 4199
Wed Sep 14 15:05:50 2022 4199
Wed Sep 14 15:05:51 2022 4199
Wed Sep 14 15:05:52 2022 4199
Wed Sep 14 15:05:53 2022 4199
Wed Sep 14 15:05:55 2022 4199
Wed Sep 14 15:05:56 2022 4199
Wed Sep 14 15:05:57 2022 4199
Wed Sep 14 15:05:58 2022 4199
Wed Sep 14 15:05:59 2022 4199
Thanks in advance for looking into it.提前感谢您的调查。
You need to provide the limits as tuple or list:您需要以元组或列表的形式提供限制:
plt.ylim([y_low, y_high])
# or
plt.ylim((y_low, y_high))
import matplotlib.pyplot as plt
import numpy as np
# This emulates data like yours.
t = np.linspace(0, 550, 50)
sample = np.random.uniform(low=4198, high=4199, size=(50,))
plt.plot(t, sample)
plt.ylim(4100, 4300) # This DOES produce the effect that you want!!
plt.ylabel('Voltage (mV)')
plt.xlabel('Time (s)')
plt.show()
The issue must come from how you pack your data into the two lists that you plot.问题必须来自您如何将数据打包到 plot 的两个列表中。
According to your whole script, the problem comes from how you process your data.根据您的整个脚本,问题来自您处理数据的方式。 In particular, the return
statements of findStart
and findEnd
make references to a local variable that is NOT referenced unless the if
statement returns true
.特别是findStart
和findEnd
的return
语句引用了一个不被引用的局部变量,除非if
语句返回true
。 This throws an error using Python 3.8 and 3.10 (I didn't try others)这使用 Python 3.8 和 3.10 会引发错误(我没有尝试其他)
Like others suspected, I'd suggest you to debug these functions in order to make sure they do what you want.像其他人怀疑的那样,我建议您调试这些功能,以确保它们按照您的意愿行事。 In any case, there is nothing wrong with how you use matplotlib
to achieve what you want to see in your plot.无论如何,您如何使用matplotlib
来实现您希望在 plot 中看到的内容并没有错。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.