在matplotlib中绘制图形时出现KeyError [英] KeyError while plotting a graph in matplotlib

查看:273
本文介绍了在matplotlib中绘制图形时出现KeyError的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试为下面的数据框绘制一个简单的图形

I am trying to plot a simple graph for the dataframe below

  indeces      Zeitstempel     Ergebnis
0   382    16.04.2020 16:12:07  PASS
1   383    16.04.2020 16:13:07  PASS
2   392    16.04.2020 16:13:20  FAIL
3   382    16.04.2020 16:13:22  PASS
4   383    16.04.2020 16:14:22  PASS

它有三列.x轴应该是Zeitstempel,y轴应该是indeces,我也想在Ergebnis列中指定值(对于PASS,颜色可能是绿色,对于FAIL,颜色是红色,对于BLOCKED,颜色是灰色)关于哪个索引在什么时间通过,失败或阻塞.实际的数据帧具有1172行×3列的值,但是在上面我只提到了很少的几个.我正在尝试的代码如下,但是以某种方式我无法弄清楚如何根据需要绘制所有3个代码.

It has three columns. The x-axis should be Zeitstempel, y-axis should be indeces and I would also want to specify the values in Ergebnis column(maybe color coding green for PASS,red for FAIL and grey for BLOCKED) as to which index is passing or failing or blocking at what time. The actual dataframe has 1172 rows × 3 columns values but in the above i have only mentioned few. The code I am trying is as below but somehow I am not able to figure out how to plot all the 3 as required.

times = pd.date_range('2020-04-16 04:12 AM', '2020-04-16 11:00 PM', freq='1H')

fig, ax = plt.subplots(1)
fig.autofmt_xdate()

df.plot(kind='line',x='times',y='Index',ax=ax)
xfmt = mdates.DateFormatter('%d-%m-%y %H:%M')
ax.xaxis.set_major_formatter(xfmt)
ax = plt.gca()

plt.show()

times具有Zeitstempel值,而Index具有indeces值存储在其中.这给了我KeyError.有没有更简单的方法可以做到这一点?我是matplotlib的新手,我已无所适从.请提出建议.

times has Zeitstempel values and Index has indeces values stored in them. This gives me KeyError. Is there a simpler way to do this? I am new to matplotlib and I am running out of possibilities. Please suggest.

推荐答案

看看我发布在以下位置的答案:如何阅读np.genfromtxt中的数据框,而不是matplotlib 中的文件.它显示了如何使用 np.genfromtxt()从csv文件加载数据,然后生成所需的颜色编码图(类似于您要执行的操作).如果您可以将Pandas数据映射到NumPy数组,则其余过程仍将相同.

Take a look at the answer I posted at: How to read a dataframe in np.genfromtxt instead of a file in matplotlib. It shows how to load data from a csv file with np.genfromtxt() then generate the desired color coded plot (similar to what you want to do). If you can map your Pandas data to a NumPy recarray, the rest of the process will still work the same.

我不熟悉Pandas,因此只能提供伪代码.它看起来像这样.将此调用替换为 np.genfromtxt()(可从文件中读取csv数据):

I'm not familiar with Pandas, so can only supply pseudo-code. It will look something like this. Replace this call to np.genfromtxt() (that reads the csv data from a file):

csv_arr = np.genfromtxt(csv,    # Data to be read
                        ...) 

使用以下几行来创建Recarray.(我简化了 csv_arr 名称的使用.请随意使用您喜欢的任何名称):

With the following lines to create the recarray. (I reused the csv_arr name to simplify. Feel free to use any name you like):

csv_dt = np.dtype([ ('indeces', '<i4'), ('Zeitstempel', 'O'), ('Ergebnis', '<U7') ])
csv_arr = np.empty(shape=(nrows,), dtype=csv_dt)
csv_arr['Zeitstempel'] = # pandas Zeitstempel data goes here as a numpy array
csv_arr['indeces'] = # pandas indeces data goes here as a numpy array 
csv_arr['Ergebnis'] = # pandas Ergebnis data goes here as a numpy array

将Pandas数据添加到 csv_arr 后,其余代码应相同,并创建参考答案中所示的相同图.祝你好运.

After you add your Pandas data to csv_arr, the rest of the code should work the same and create the same plot shown in the referenced answer. Good luck.

这篇关于在matplotlib中绘制图形时出现KeyError的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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