简体   繁体   English

如何在 MatPlotLib 中定义 Y 轴的范围?

[英]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 .特别是findStartfindEndreturn语句引用了一个不被引用的局部变量,除非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.

相关问题 matplotlib 中 Y 轴非常拥挤,如何隐藏 y 轴值? - Y-axis is very crowded in matplotlib,How do I hide y-axis values? 如何在 pandas 数据帧和简单变量之间格式化 matplotlib 中的 y 轴“y”? - How do I format a y-axis 'y' in matplotlib going between pandas dataframes and simple variables? 你如何限制 matplotlib 中的 y 轴高度? - How do you limit the y-axis height in matplotlib? 我如何在 Matplotlib 上为我的 y 轴限制设置一个精确的浮点数? - How do I set an exact float as the scaling factor for my y-axis limits on Matplotlib? 如何增加 3D 条形图在 y 轴上的间隙(matplotlib 问题)? - How do I increase the gap of 3D bar chart in y-axis (matplotlib question)? 如何在matplotlib.animation中使y轴升序? Sentdex教程 - How do i make y-axis to be in an ascending order in matplotlib.animation? Sentdex tutorial 如何使用 Matplotlib 对齐两个 y 轴刻度的网格线? - How do I align gridlines for two y-axis scales using Matplotlib? 如何使用matplotlib有选择地擦除2 y轴子图上的线。(Python) - How do I selectively erase lines on a 2 y-axis subplot using matplotlib.(Python) 如何让 matplotlib.pyplot 沿 Y 轴标记每隔几个标签? - How do I get matplotlib.pyplot to label every few labels down the Y-Axis? 如何在 matplotlib 中使 y 轴上的数字显示以百万为单位的值而不是科学计数法? - How do I make the numbers on the y-axis show values in millions instead of in scientific notation in matplotlib?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM