使用列表推导绘制存储在字典列表中的数据 [英] Plot data stored in list of dicts using list comprehension

查看:112
本文介绍了使用列表推导绘制存储在字典列表中的数据的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个netCDF文件,其中包含可追溯到1948年的全球每月平均温度。要进行分配,我必须选择任何提供的数据点,提取其12月,1月,2月和3月月份的平均温度值,然后显示它。

I have a netCDF file containing monthly average temperatures around the globe dating back to 1948. For an assignment, I have to select any one provided datapoint, extract its mean temperature values for the months of December, January, February, and March, and then display it.

我已经解压缩了数据,并将其收集在词典列表中,如下所示:

I've already unpacked the data and I have it collected in a list of dictionaries, as shown here:

12月

decemberMeans = [
    {'year': 1948, 'temp': 271.24}, {'year': 1949, 'temp': 271.28},
    {'year': 1950, 'temp': 268.52}, {'year': 1951, 'temp': 269.63},
    ...,
    {'year': 2015, 'temp': 277.23}, {'year': 2016, 'temp': 271.25}
]

与一月,二月和三月相对应的数据以相同的方式构成。

Data corresponding to January, February, and March is structured in the same manner.

下一步是绘制它。我必须为每组数据在同一张图上绘制一条线,并且我使用列表推导来做到这一点。现在我的绘图代码如下所示:

My next step is to plot it. I have to plot one line on the same graph for each set of data, and I'm using list comprehensions to do it. Right now my plotting code looks like this:

import matplotlib.pyplot as plt

plt.figure(1)
plt.hold(True)
plt.plot([data['years'] for data in decemberMeans], \
    [data['temp'] for data in decemberMeans], 'k-')
plt.plot([data['years'] for data in januaryMeans], \
    [data['temp'] for data in januaryMeans], 'r-')
plt.plot([data['years'] for data in februaryMeans], \
    [data['temp'] for data in februaryMeans], 'b-')
plt.plot([data['years'] for data in marchMeans], \
    [data['temp'] for data in marchMeans], 'y-')
plt.grid(True)
plt.show()
plt.close()

它的绘制很好,但是我可以看到我所有的列表理解都是多余的。有没有一种方法可以一次将字典中的值解包,而不必为列表中的数据写 data ['...'] 每组两次?

It plots just fine, but I can see that all of my list comprehensions are really redundant. Would there be a way that I could unpack the dictionary values in one fowl swoop so that I don't have to write data['...'] for data in list twice for each set?

PS -在撰写本文时,我开始意识到我可以编写一个函数来进行绘图(这可能比写这篇文章要花更少的时间),但是我仍然很想知道。

P.S. - As I'm writing this I've started to realize that I could write a function to do the plotting (which would probably take even less time than writing out this post), but I'm still curious to know. Thanks to all in advance!

推荐答案

是的 pandas 去!在此处中查看示例(第二个示例应该是如果您希望以这种形式获取数据)。不过,更笼统地说,您可以采取一些措施来避免遇到这种重复。

Yes pandas is definitely the way to go! Have a look at the examples here (second example should be exactly what you want, if you can get your data in that form). More generally though, there are a few things you can do to avoid the kind of repetition you have come across.

为常用代码编写函数是一个不错的开始。听起来您已经想出了类似这样的东西:

Writing a function for commonly used code is a great start. It sounds like you have already come up with something like this:

def myplot(monthMeans, marker):
    plt.plot([data['years'] for data in monthMeans],
             [data['temp'] for data in monthMeans],
             marker)

然后将每个序列绘制到活动图形的代码变为:

And then your code to plot each series to the active figure becomes:

myplot(decemberMeans, 'k-')
myplot(januaryMeans, 'r-')
myplot(februaryMeans, 'b-')
myplot(marchMeans, 'y-')

但这还是不理想-如果您更改 myplot 不得不更改每一行?您可以再次缩短时间,因为Python允许您创建任何内容的列表:

But this is still not ideal - what if you changed the name of myplot and had to change every line? You can shorten things again, because Python lets you create lists of anything:

allMonthMeans = [decemberMeans, januaryMeans, ...]
markers = ['k-', 'r-', ...]

# 'zip' loops over the two lists simultaneously
for monthMeans, marker in zip(allMonthMeans, markers):
    myplot(monthMeans, marker)

这篇关于使用列表推导绘制存储在字典列表中的数据的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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