Matplotlib和datetime的OverflowError [英] OverflowError with Matplotlib and datetime

查看:47
本文介绍了Matplotlib和datetime的OverflowError的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

当我尝试使用日期时间制作散点图时出现溢出错误:

将 numpy 导入为 np将熊猫作为pd导入导入matplotlib.pyplot作为pltlist_data = ['2016-10-06','2016-09-24',55,'虚拟',0.510823,0.29431]列= ['master','slave','baseline','coh','coh_mean','coh_std']dict_data = dict(zip(列(list_data)))数据= pd.DataFrame.from_dict(dict_data,orient ='index').Tdata.slave = pd.to_datetime(data.slave)图,轴= plt.subplots()axes.scatter(data.slave,data.baseline)plt.show()

我已经尝试了多种方法,例如使用 datetime.strptime(),更改matplotlib后端,但仅将问题隔离为datetime格式(如果是字符串,则可以很好地工作)

不使用分散绘图 (axes.plot(), axes.plot_date()) 工作正常,但我最终需要为散点着色,散点方法似乎最适合.>

绘制熊猫系列的值 axes.scatter(data.slave.values,data.baseline.values)结果是一个空的图形对象.

谢谢.

解决方案

您不能使用 scatter 绘制 Pandas 系列.您想要的只是绘制其值.

axes.scatter(data.slave.values, data.baseline.values)

完整示例:

将 numpy 导入为 np将熊猫作为pd导入导入matplotlib.pyplot作为pltlist_data = ['2016-10-06', '2016-09-24', 55, 'dummy', 0.510823, 0.29431]列 = ['master', 'slave', 'baseline', 'coh', 'coh_mean', 'coh_std']dict_data = dict(zip(列(list_data)))数据= pd.DataFrame.from_dict(dict_data,orient ='index').Tdata.slave = pd.to_datetime(data.slave)图,轴 = plt.subplots()轴散点(data.slave.values,data.baseline.values)plt.show()

结果

I am getting an overflow error when I try and make a scatter plot with a datetime:

import numpy as np
import pandas as pd
import matplotlib.pyplot as plt

list_data  = ['2016-10-06', '2016-09-24', 55, 'dummy', 0.510823, 0.29431]
columns    = ['master', 'slave', 'baseline', 'coh', 'coh_mean', 'coh_std']
dict_data  = dict(zip(columns, list_data))
data       = pd.DataFrame.from_dict(dict_data, orient='index').T
data.slave = pd.to_datetime(data.slave)
fig, axes  = plt.subplots()
axes.scatter(data.slave, data.baseline)
plt.show()

I've tried a variety of things, such as using datetime.strptime(), changing matplotlib backends but only have been isolate the problem to the datetime format (works fine if it's a string).

Plotting without scatter (axes.plot(), axes.plot_date()) works fine, but I eventually need to color the scatter points, which the scatter method seems best suited for.

EDIT: Plotting the values of the panda series axes.scatter(data.slave.values, data.baseline.values) Results in an empty figure object.

Thanks.

解决方案

You cannot plot a pandas series with scatter. What you want is to plot its values only.

axes.scatter(data.slave.values, data.baseline.values)

Complete example:

import numpy as np
import pandas as pd
import matplotlib.pyplot as plt

list_data  = ['2016-10-06', '2016-09-24', 55, 'dummy', 0.510823, 0.29431]
columns    = ['master', 'slave', 'baseline', 'coh', 'coh_mean', 'coh_std']
dict_data  = dict(zip(columns, list_data))
data       = pd.DataFrame.from_dict(dict_data, orient='index').T
data.slave = pd.to_datetime(data.slave)
fig, axes  = plt.subplots()
axes.scatter(data.slave.values, data.baseline.values)
plt.show()

results in

这篇关于Matplotlib和datetime的OverflowError的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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