使用django和matplotlib绘制时间戳

问题描述 投票:0回答:1

我在django中创建视图时遇到了一些麻烦,我正在尝试制作一个Web应用程序,该应用程序将从CSV读取并显示绘图,但是我需要从CSV绘图的时间轴不是浮动它是一个字符串,然后我在这里有它我不确定如何绘制此时间戳:

def showMe(request):
    fig = Figure()
    canvas = FigureCanvas(fig)
    x, y = np.loadtxt('/Users/name/Desktop/garden_app/stored.csv', delimiter=',', dtype={'names': ('time','moisture'), 'formats': ('|S1', np.float)}, unpack=True)
    plt.plot(x, y, align='center', alpha=0.5, label='Name')
    plt.ylabel('moisture')
    plt.xlabel('time')
    plt.title('Moisture V. Time')
    buf = io.BytesIO()
    plt.savefig(buf, format='png')
    plt.close(fig)
    response = HttpResponse(buf.getValue(), content_type = 'image/png')
    return response

我拥有的csv文件如下所示:

moisture,time,pump,light,temperature
1023,2019-10-27 17:22:27.367391,running,5V,25 Celsius
1023,2019-10-27 17:22:30.402280,running,5V,25 Celsius

...

““ ValueError:无法将字符串转换为浮点数:'时间'”来自第4行上的'formats':('| S1',np.float),我不确定如何将其更改为是一个数字,另一个是时间戳。

python django csv matplotlib django-views
1个回答
0
投票

我建议您使用熊猫读取csv文件,它不需要您的数据纯粹是浮点数。

df = pd.read_csv('/Users/name/Desktop/garden_app/stored.csv', parse_dates=True, index_col='time', sep=",")

阅读后,您可以将数据框列传递给matplotlib。

© www.soinside.com 2019 - 2024. All rights reserved.