请求协助解决chart_studio认证凭证缺失导致的PlotlyRequestError
解决
chart_studio.exceptions.PlotlyRequestError: Authentication credentials were not provided错误 我来帮你搞定这个问题,这是使用cufflinks离线生成图表时很容易碰到的小坑。
问题根源
你已经调用了cf.go_offline(),但cufflinks在某些版本下还是会尝试连接Plotly的在线服务来处理数据,而你没有配置对应的账户认证信息,所以抛出了这个错误。
可行的解决方案
方案1:强制cufflinks完全离线运行
修改你的代码,补充配置确保离线模式彻底生效:
import numpy as np import pandas as pd import matplotlib.pyplot as plt import seaborn as sns train=pd.read_csv('titanic_train.csv') #sns.heatmap(train.isnull(),yticklabels=False,cbar=False,cmap='cividis') import cufflinks as cf import plotly.offline as pyo # 强制开启离线模式,禁用在线数据上传 cf.set_config_file(offline=True, world_readable=False) pyo.init_notebook_mode() # 如果是在Jupyter环境运行需要这个,本地脚本可忽略 train['Fare'].iplot(kind='hist',bins=30) sns.set_style('whitegrid') plt.show()
方案2:改用纯Plotly离线代码实现相同效果
如果cufflinks的离线配置还是不稳定,直接用Plotly的离线模块来画直方图更可靠:
import numpy as np import pandas as pd import matplotlib.pyplot as plt import seaborn as sns import plotly.express as px train=pd.read_csv('titanic_train.csv') # 用Plotly Express快速生成直方图 fig = px.histogram(train, x='Fare', nbins=30) fig.show() sns.set_style('whitegrid') plt.show()
方案3:(可选)配置Plotly认证(如果需要在线保存图表)
要是你本来就想把图表上传到Plotly在线平台,那得先配置你的账户信息:
import chart_studio # 替换成你的Plotly用户名和API密钥 chart_studio.tools.set_credentials_file(username='你的用户名', api_key='你的API密钥') # 之后再运行iplot代码 train['Fare'].iplot(kind='hist',bins=30)
API密钥可以在Plotly官网的账户设置页面找到。
补充说明
原代码里的cf.go_offline()在部分cufflinks版本中没有完全禁用在线请求,它默认会尝试把数据上传到Plotly的在线网格服务,这就触发了认证检查。通过强制设置离线模式或者直接用Plotly原生离线方法,就能避开这个问题啦。
内容的提问来源于stack exchange,提问作者Ashutos Moharana
相关产品推荐
相关产品推荐

