Graph 如何使用具有特定值的matplotlib创建图形。python

Graph 如何使用具有特定值的matplotlib创建图形。python,graph,Graph,因此,我有一个file.txt文件,我必须显示温度,我这样做了,现在的问题是取决于您想要的图表类型(条形图、线条图等)和您想要添加的轴信息(提取“数据”信息似乎相关)。在最简单的形式中,当temp-值存储在列表中时,应提供条形图可视化: import matplotlib.pyplot as plt import numpy as np def temperature(): with open("text.txt", 'r') as f: temp

因此,我有一个file.txt文件,我必须显示温度,我这样做了,现在的问题是

取决于您想要的图表类型(条形图、线条图等)和您想要添加的轴信息(提取“数据”信息似乎相关)。在最简单的形式中,当
temp
-值存储在列表中时,应提供条形图可视化:

import matplotlib.pyplot as plt
import numpy as np


def temperature():
    with open("text.txt", 'r') as f:
        temps = []
        for line in f:
            if "Temperature" not in line: continue
            temp = line.split(" ")[1]
            temps.append(temp)
    return temps

temps = temperature()
plt.bar(x=np.arange(len(temps)), height=temps)
plt.show()

有关更多信息,请参阅matplotlib文档:

代码的一个解决方案是返回一个列表或一个温度数组,并使用它来绘制x轴

import matplotlib.pyplot as plt #importing matplotlib
def temperature():
   temps = []
   with open("text.txt", 'r') as f:
       for line in f:
           if "Temperature" not in line: continue
           temp = line.split(" ")[1]
           temps += [temp] #appending the temperature value at the end
   return temps

temps = temperature()
print(temps)
# Then, use the corresponding function fom matplotlib to fit your needs: 
# plt.plot(temps), plt.scatter(temps), ...
然而,正如我所说的,你需要一个x轴来使绘图更好,也许是日期。如果不是,x轴将是列表0,1,2,3,。。。一种方法是,正如我们对温度所做的那样,从文件中提取日期,然后用字符串修改x轴的刻度标签。一个快速解决方案可以是:

# modify the temperature function accordingly to return the following:
days, temperatures = temperature()

plt.bar(x=range(temperatures), height=temperatures)
plt.xtics(ticks=range(len(temperatures)), labels=days)

你想要什么类型的图表?条形图折线图?到目前为止你都试了些什么?我必须用条形图,但我不知道如何查看matplotlib?我建议仔细阅读,它们非常广泛,并提供了许多示例。我在下面提供了一个最小的示例。下面的最小示例有助于解决时间序列可视化的问题吗?如果没有,您还面临哪些问题?如果是,请考虑将它们标记为