编辑:Matplotlib版本是1.4.2, numpy的版本是1.8.2Matplotlib XTICK%H后追加%F:%M%:S于图表
我建立从csv文件的曲线图具有以下格式
08:21:05,41.0
08:22:05,41.0
08:23:05,41.0
第一列是时间(当然)和第二列是一厘米的测量。
我正在更新一个应用程序,我写了一个测量坑水坑水位到Python3的活动,它需要将 的日期从字节转换为str,因为它抛出了一个错误。在Python2下可以接受的字节数。
该图形使用以下代码创建;
import time
import numpy as np
import matplotlib as mpl
mpl.use('Agg')
import matplotlib.pyplot as plt
import matplotlib.dates as mdates
from matplotlib import rcParams
rcParams.update({'figure.autolayout': True})
def bytesdate2str(fmt, encoding='utf-8'):
'''Convert strpdate2num from bytes to string as required in Python3.
This is a workaround as described in the following tread;
https://github.com/matplotlib/matplotlib/issues/4126/
Credit to github user cimarronm for this workaround.
'''
strconverter = mdates.strpdate2num(fmt)
def bytesconverter(b):
s = b.decode(encoding)
return strconverter(s)
return bytesconverter
def graph(csv_file, filename, bytes2str):
'''Create a line graph from a two column csv file.'''
unit = 'metric'
date, value = np.loadtxt(csv_file, delimiter=',', unpack=True,
converters={0: bytes2str}
)
fig = plt.figure(figsize=(10, 3.5))
fig.add_subplot(111, axisbg='white', frameon=False)
rcParams.update({'font.size': 9})
plt.plot_date(x=date, y=value, ls='solid', linewidth=2, color='#FB921D',
fmt=':'
)
title = "Sump Pit Water Level {}".format(time.strftime('%Y-%m-%d %H:%M'))
title_set = plt.title(title)
title_set.set_y(1.09)
plt.subplots_adjust(top=0.86)
if unit == 'imperial':
plt.ylabel('inches')
if unit == 'metric':
plt.ylabel('centimeters')
plt.xlabel('Time of Day')
plt.xticks(rotation=30)
plt.grid(True, color='#ECE5DE', linestyle='solid')
plt.tick_params(axis='x', bottom='off', top='off')
plt.tick_params(axis='y', left='off', right='off')
plt.savefig(filename, dpi=72)
csv_file = "file.csv"
filename = "today.png"
bytes2str = bytesdate2str('%H:%M:%S')
graph(csv_file, filename, bytes2str)
在Python 3中使用matplotlib我必须将日期从字节转换为str,这是函数bytesdate2str的作用。 这是另一个github用户在问题线程中编写的解决方法。我试了一下,现在它确实创建了图形。
但是,x轴上的时间现在追加了%f到最后。我已经能够在日期格式 周围的matplotlib网站上找到一些文档,但我很难找出如何删除。%f我认为这代表一个浮点数。
这里是什么xticks之前看起来像Python2 下
这里是xticks的样子从字节转换后为str
如何从的末尾移除%F时间?这是我使用matplotlib的唯一东西,并且当它来到 时,它是一个新手。我已经长时间地ban住了我的头。感谢您提供的任何见解。