发布于2019-10-05 18:12 阅读(1885) 评论(0) 点赞(18) 收藏(0)
ValueError: min() arg is an empty sequence
尝试将pandas数据框图转换为plotly时收到一个。
这是数据框的一个示例(带有NaN):
ttab.loc[:,:"Irish"]
Group English (British) Americans (White) Canadians Scots Irish
Year
1926 1 2 3 4 5
1946 3 1 2 5 4
1956 3 1 2 7 5
1966 2 1 3 9 5
1977 2 1 3 9 7
1993 2 NaN NaN 6 1
2001 4 1 3 NaN 5
2001* 4 1 3 NaN 5
2012 4 1 3 NaN 6
我做了一个漂亮的情节:
# Time It
import time
begin = time.clock()
# Inline plotting
%matplotlib inline
# Import libraries
import matplotlib
import numpy as np
import matplotlib.pyplot as plt
import pandas as pd
import seaborn as sns
from pandas.tools.plotting import scatter_matrix
# (*) To communicate with Plotly's server, sign in with credentials file
import plotly.plotly as py
# (*) Useful Python/Plotly tools
import plotly.tools as tls
# (*) Graph objects to piece together plots
from plotly.graph_objs import *
# Read, clean, slice data here...
# Rename columns, examine, rename groups here...
# Merge, set index, transpose dataframe here....
# Change datatype to float here...
# Define color palette here...
# Now Plot...
# Package all mpl plotting commands inside one function
def plot_mpl_fig():
# plot parameters
figsize = (12,12)
axfontsize = 16
legfontsize = 14
labfontsize = 18
yax = (30,0)
lw = 2
# Interpolate NaNs and plot as dashed lines
ax = ttab.loc[:,:'Asian Indians'].interpolate().plot(color=colorlist, ylim=yax, lw=lw, fontsize=axfontsize, \
figsize=figsize, style='--')
# Overplot the measured values with solid lines
ttab.loc[:,:'Asian Indians'].plot(ax=ax, color=colorlist, ylim=yax, lw=lw, fontsize=axfontsize, figsize=figsize)
# Legend handling
lines, labels = ax.get_legend_handles_labels()
ax.legend(lines[28:], labels[28:], loc='center left', bbox_to_anchor=(-0.3, 0.5), fontsize=legfontsize)
# Labeling
plt.xlabel('Year',fontsize=labfontsize)
plt.ylabel('Rank',fontsize=labfontsize)
# Plot it!
plot_mpl_fig()
# N.B. get matplotlib figure object and assign a variable to it
mpl_fig1 = plt.figure()
然后,我尝试通过以下示例进行转换:https : //plot.ly/python/matplotlib-to-plotly-tutorial/
快速开始:
>>> import plotly.plotly as py
>>> import matplotlib.pyplot as plt
>>> # auto sign-in with credentials or use py.sign_in()
>>> mpl_fig_obj= plt.figure()
>>> # ... make a matplotlib figure ...
>>> py.plot_mpl(mpl_fig1) # <== ValueError
或第6.1节的教程:
# Convert a matplotlib figure object to a Plotly figure object
help(tls.mpl_to_plotly)
py_fig1 = tls.mpl_to_plotly(mpl_fig1, verbose=True) # <== ValueError
print(py_fig1.to_string())
# Plot a matplotlib figure in Plotly
help(py.iplot_mpl)
py.iplot_mpl(mpl_fig1, filename='s6_damped_oscillation') # <== ValueError
每当我打电话plot_mpl
时mpl_to_plotly
,或iplot_mpl
得到通知ValueError: min() arg is an empty sequence
。我认为这可能与NaN有关,但我不知道该如何解决。有什么建议么?
看起来mpl_to_plotly需要使用axes.plot()而不是plt.plot()。以下代码在Jupyter Notebook中有效。
import plotly.offline as py
from plotly.offline import init_notebook_mode, iplot
import plotly.tools as tls
import matplotlib.pylab as plt
from matplotlib.backends.backend_agg import FigureCanvasAgg as FigureCanvas
init_notebook_mode(connected=True)
fig = plt.Figure()
ax = fig.gca()
x = [-2,0,4,6,7]
y = [q**2-q+3 for q in x]
ax.plot(x,y)
canvas = FigureCanvas(fig)
plotly_fig = tls.mpl_to_plotly(fig)
py.iplot(plotly_fig)
作者:黑洞官方问答小能手
链接:https://www.pythonheidong.com/blog/article/131072/be5970285e83a3797611/
来源:python黑洞网
任何形式的转载都请注明出处,如有侵权 一经发现 必将追究其法律责任
昵称:
评论内容:(最多支持255个字符)
---无人问津也好,技不如人也罢,你都要试着安静下来,去做自己该做的事,而不是让内心的烦躁、焦虑,坏掉你本来就不多的热情和定力
Copyright © 2018-2021 python黑洞网 All Rights Reserved 版权所有,并保留所有权利。 京ICP备18063182号-1
投诉与举报,广告合作请联系vgs_info@163.com或QQ3083709327
免责声明:网站文章均由用户上传,仅供读者学习交流使用,禁止用做商业用途。若文章涉及色情,反动,侵权等违法信息,请向我们举报,一经核实我们会立即删除!