使用不同 matplotlib 版本绘图的差异 [英] Difference in plotting with different matplotlib versions

查看:22
本文介绍了使用不同 matplotlib 版本绘图的差异的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的一位同事递给我一个脚本,用于从数据库中收集数据并绘制它.我自己用脚本的时候,情节不太一样,和Matplotlib的版本有关.

A colleague of mine handed me a script that is used to collect data from a database and plot it. When I used the script myself, the plots do not look the same, and it has to do with the version of Matplotlib.

绘制数据的脚本很短:

import matplotlib.pyplot as plt
import csv
import os
from dateutil import parser

def plot(outputDir,plotsDir,FS):
    allfiles = os.listdir(outputDir)
    flist = []
    for f in allfiles:
        if 'csv' in f.lower(): flist.append(f)
    for f in flist:
        with open(outputDir + '/' + f, 'rt') as ff:
            data = list(csv.reader(ff,delimiter=FS))
        values = [i[2] for i in data[1::]]
        values = ['NaN' if v is '' else v for v in values]
        time = [parser.parse(i[1]) for i in data[1::]]
        plt.xlabel('Time_[UTC]')
        plt.plot(time, values)
        plt.xticks(rotation=40)
        if os.path.isdir(plotsDir) != 1:
            os.mkdir(plotsDir, 777)
        plt.savefig('{}/{}_Data.png'.format(plotsDir, f[:-4]), bbox_inches='tight', dpi=160)
        plt.clf()


outputdir = 'C:/Users/matthijsk/Documents/Test'
plotsdir = outputdir + '/plots'
fs = ','
plot(outputdir, plotsdir, fs)

当我使用 Matplotlib 2.1.0 版运行它时​​,我的图像如下所示:当我使用 Matplotlib 2.0.2 版运行它时​​,它看起来像它应该的样子:

When I run it using Matplotlib version 2.1.0, my image looks like this: When I run it using Matplotlib version 2.0.2, it looks the way it is supposed to:

脚本正在读取的文件如下所示:

The file the script is reading looks like this:

stationNo,dtg(UTC),TT_[°C],source_TT,quality_TT
10381,2017-01-01 00:00:00,3.0,ob,na
10381,2017-01-01 01:00:00,3.0,ob,na
10381,2017-01-01 02:00:00,2.4,ob,na
10381,2017-01-01 03:00:00,2.5,ob,na
10381,2017-01-01 04:00:00,2.5,ob,na
10381,2017-01-01 05:00:00,2.3,ob,na
10381,2017-01-01 06:00:00,1.9,ob,na
10381,2017-01-01 07:00:00,1.0,ob,na
10381,2017-01-01 08:00:00,0.1,ob,na
10381,2017-01-01 09:00:00,0.9,ob,na

谁能解释一下导致这种情况的 Matplotlib 发生了什么变化?显然我在导致这种情况的绘图上做错了.任何人都可以注意到错误吗?我已经尝试使用

Can anyone explain me what was changed in Matplotlib that caused this? And apparently I'm doing something wrong with the plotting that is causing this. Can anyone notice a mistake? I've already tried using

values = [float(value) if value.isnumeric() else None for value in values]

但这并没有解决问题.注意:我宁愿不使用任何非标准软件包(如 Pandas),因为获得批准安装此类软件包非常麻烦.

But that didn't solve it. Note: I'd rather not use any non-standard packages (like Pandas) since it's quite a hassle to get approvement to install such packages.

推荐答案

数据以字符串形式读入.在 matplotlib 2.0 中,它们会自动转换为浮点数,以便进行绘图.

The data is read in as strings. In matplotlib 2.0 those were automatically converted to floating point numbers such that they can be plotted.

在 matplotlib 2.1 中,引入了分类图.这现在允许类似

In matplotlib 2.1, categorical plots have been introduced. This now allows for something like

plt.plot(["apple", "banana", "cherry"], [2,1,3])

虽然这对于某些应用程序来说当然很好,但它打破了先前绘制可转换为浮点数的字符串的选项.我想如果没问题的话,它只是让用户有责任自己进行转换.

While this is of course great for certain applications, it breaks the previous option of plotting strings that are convertable to floats. I guess this if fine, it just gives the user the responsibility to do the conversion himself.

在这种情况下,您可能希望像这样进行转换

In this case you would want to do this conversion like

values = [None if v is '' else float(v) for v in values]

如果你已经有一个 numpy 数组:np.array(values).astype(float)

In case you already have a numpy array: np.array(values).astype(float)

通常,可以使用 numpy.loadtxt 将文件读入浮点数组.如果文件包含日期,则使用转换器,如 使用 Python 读取带有日期对象和浮点数的逗号分隔文件 是可能的.

In general, one can use numpy.loadtxt to read files into float arrays. If the file contains dates, usage of a converter as in reading a comma-delimited file with a date object and a float with Python would be possible.

另一个读取文本文件的选项是 pandas.read_csv.

Another option to read in text files would be pandas.read_csv.

这篇关于使用不同 matplotlib 版本绘图的差异的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆