简体   繁体   English

Matplotlib-绘制时间时,在秒数后放置十进制零

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

Here is the plot I have currently: 这是我目前拥有的地块:

在此处输入图片说明

The 'time' strings I import are like this: 08:12:46, so I would like to cut the zeros at the end, but I can't seem to find the problem. 我导入的“时间”字符串如下:08:12:46,所以我想在结尾处切零,但我似乎找不到问题所在。 Also, is there a way to show the floats on the Y axis in the exponential format, which is the one I am importing from the csv? 另外,有没有办法以指数格式显示Y轴上的浮点数,这就是我从csv导入的浮点数?

I just started to look into matplotlib and numpy for work, so if you have some advice it would be fantastic. 我刚刚开始研究matplotlib和numpy的工作,因此,如果您有任何建议,那将是很棒的。

Thank you in advance! 先感谢您!

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()

these are the data: 这些是数据: 在此处输入图片说明

You can specify the format to be used as follows: 您可以指定要使用的格式,如下所示:

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

This would give you an output as follows: 这将为您提供如下输出:

具有时间格式的Matplot

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM