python - Matplotlib - 绘制时间时,它会在秒后添加小数零

标签 python datetime numpy matplotlib format

这是我目前的情节:

enter image description here

我导入的“时间”字符串是这样的:08:12:46,所以我想把末尾的零去掉,但我似乎找不到问题。另外,有没有一种方法可以以指数格式显示 Y 轴上的 float ,这是我从 csv 导入的 float ?

我刚刚开始研究 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()

这些是数据: enter image description here

最佳答案

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

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 with time formatting

关于python - Matplotlib - 绘制时间时,它会在秒后添加小数零,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34761977/

相关文章:

python - takeOrdered 降序 Pyspark

Python - 字符串到 JSON。错误: AttributeError: 'str' object has no attribute 'get'

mysql - 随时返回当前日期的结果

python - 如何将复数放入 numpy 的数组中?

numpy - 如何使用 Numpy 屏蔽数组的一部分?

python - 使用 cv2 检测 2048 年的瓷砖方 block

python - get_variable 的自定义初始值设定项

java - 是否有更快的方法将任意字符串与 Java 中的月份名称匹配

c# - 将一天中的时间转换为 .net 中的 fr-CA - 错误的短时间格式字符串?

python - 将 Pandas 数据框转换为字典,其中一列作为键,另一列作为多个值