简体   繁体   English

Python for loop matplotlib 条形图不显示所有数据

[英]Python for loop matplotlib bar chart not displaying all data

I have a dictionary where each key represents a year, and each value represents a rainfall anomaly over a certain domain for that year.我有一个字典,其中每个键代表一年,每个值代表该年某个域的降雨异常。

anoms_yearly.keys()

dict_keys([1980, 1981, 1982, 1983, 1984, 1985, 1986, 1987, 1988, 1989, 1990, 1991, 1992, 1993, 1994, 1995, 1996, 1997, 1998, 1999, 2000, 2001, 2002, 2003, 2004, 2005, 2006, 2007, 2008, 2009, 2010, 2011, 2012, 2013, 2014, 2015, 2016, 2017, 2018, 2019])

Each key, value pair is simply an xarray dataarray, which has the value and the time of the value.每个键值对只是一个 xarray 数据数组,其中包含值和值的时间。

anoms_yearly[1980].values
array(-3.49641086)

Now, I try and plot the data in a simple bar graph.现在,我尝试将 plot 数据放在一个简单的条形图中。

fig,ax=plt.subplots(figsize=(20,12))
for i in anoms_yearly.keys():
    if anoms_yearly[i].values<0:
        colors='brown'
    else:
        colors='green'
    ax.bar(anoms_yearly[i]['time'], anoms_yearly[i].values, color=colors)

Here, anoms_yearly[i]['time'] represents one year, as it loops through time and anoms_yearly[i].values is just a single array with one value.这里, anoms_yearly[i]['time']代表一年,因为它循环通过时间,而anoms_yearly[i].values只是一个具有一个值的数组。 The plot is missing all sorts of bars and randomly plots a few. plot 缺少各种条形图,并随机绘制了一些条形图。 What is going on here?这里发生了什么?

在此处输入图像描述

I am a bit puzzled what you do with the keys and entries of your dictionary in the plotting function.我有点困惑你在绘图 function 中对字典的键和条目做了什么。 Let's create a minimum working example:让我们创建一个最小的工作示例:

import matplotlib.pyplot as plt
import random

D = dict()
for i in range(1900,2000):
    D[str(i)] = random.random()-0.5

The dictionary D contains the years from 1900-1999 as strings as keys and a random number between -0.5 and 0.5 as corresponding values.字典D包含从 1900 年到 1999 年的年份,作为键的字符串和一个介于-0.50.5之间的随机数作为对应的值。

You can loop through the keys of the dictionary as you did.您可以像以前一样遍历字典的键。 But if you want access it, just call the dictionary with the key.但是,如果您想访问它,只需使用密钥调用字典即可。 There is no need to do this sort of indexing and calling .values() and .keys() afterwards again.没有必要再做这种索引和调用.values().keys() See:看:

fig,ax = plt.subplots()
for key in D.keys():
    if D[key] < 0:
        colors='brown'
    else:
        colors='green'
    ax.bar(key, D[key], color=colors)

The xticks are now messed up because they are strings and python/matplotlib does not know that they are ordered and can be grouped... xticks 现在搞砸了,因为它们是字符串,而 python/matplotlib 不知道它们是有序的并且可以分组......

阴谋

anyway, I guess that your odd output came from the strange indexing anoms_yearly[i]['time'] , where I frankly don't understand why you index it with the key (in the case there D[key] and index the return again with time as if another dictionary would be returned...)无论如何,我猜你奇怪的 output 来自奇怪的索引anoms_yearly[i]['time'] ,坦率地说我不明白为什么你用键索引它(在这种情况下D[key]并索引返回再次随着time的推移,好像另一个字典会被返回......)

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

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