簡體   English   中英

Matplotlib-繪制時間時,在秒數后放置十進制零

[英]Matplotlib - when plotting time it puts decimal zeros after the seconds

這是我目前擁有的地塊:

在此處輸入圖片說明

我導入的“時間”字符串如下:08:12:46,所以我想在結尾處切零,但我似乎找不到問題所在。 另外,有沒有辦法以指數格式顯示Y軸上的浮點數,這就是我從csv導入的浮點數?

我剛剛開始研究matplotlib和numpy的工作,因此,如果您有任何建議,那將是很棒的。

先感謝您!

import numpy as np
import datetime as dt
import matplotlib
import matplotlib.pyplot as plt
import matplotlib.animation as animation
from matplotlib import style

print(plt.style.available)

style.use('ggplot')

fig = plt.figure()
ax1 = fig.add_subplot(1,1,1)

def animate(i):
    graph_data = open('C:\\Users\\arzuffi pc test\\Desktop\\VMI WIP - Copia (2)\\Cycle info\\_Current Cycle.csv','r').read()
    #graph_data = open('C:\\Users\\arzuffi pc test\\Desktop\\Visual Machine Interface Alpha 1.4.3\\Cycle info\\_Current Cycle.csv','r').read()
    lines = graph_data.split('\n')
    xs = []
    ys = []
    skip = 0
    for line in lines:        
        if skip < 7:
            skip += 1
        else:                
            if len(line) > 1:
                time, cycle, pc, pd, co, hv, cr, ph, gd_volt, gd_amp, gd_power, eva_amp, eva_volt, p_rpm, p_amp, r1_rpm, r1_amp, r2_rpm, r2_amp, hmdso, gas, ahc, diff_l, diff_r = line.split(';')
                #x, y = line.split(';')
                print(time)
                print(pc)
                xs.append(dt.datetime.strptime(time,'%H:%M:%S'))#.date())
                ys.append(pc)

        #print(i)
    #xs = matplotlib.dates.date2num(xs)    
    print(xs)    
    if len (xs) > 100:
        xs = xs[-100:]
    if len (ys) > 100:
        ys = ys[-100:] 
    ax1.clear()
    ax1.plot(xs, ys)
    plt.gcf().autofmt_xdate()

ani = animation.FuncAnimation(fig, animate,interval = 1000)
plt.show()

這些是數據: 在此處輸入圖片說明

您可以指定要使用的格式,如下所示:

xs = matplotlib.dates.date2num(xs)    # You need to keep this line

hfmt = matplotlib.dates.DateFormatter('%H:%M:%S')
ax1.xaxis.set_major_formatter(hfmt)
ax1.plot(xs, ys)   # You have this already

這將為您提供如下輸出:

具有時間格式的Matplot

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM