pandas ValueError:Mime类型呈现要求nbformat>=4.2.0,但未安装该程序

egdjgwm8  于 2022-11-20  发布在  其他
关注(0)|答案(4)|浏览(623)

我试图在Visual Studio代码中打印一个plotly plot,并捕获了此错误:

---------------------------------------------------------------------------
ValueError                                Traceback (most recent call last)
<ipython-input-40-e07b5feb5ded> in <module>
     30 
     31 fig.update_layout(height=nrows*500)
---> 32 fig.show()

C:\Python38\lib\site-packages\plotly\basedatatypes.py in show(self, *args, **kwargs)
   3147         import plotly.io as pio
   3148 
-> 3149         return pio.show(self, *args, **kwargs)
   3150 
   3151     def to_json(self, *args, **kwargs):

C:\Python38\lib\site-packages\plotly\io\_renderers.py in show(fig, renderer, validate, **kwargs)
    383 
    384         if not nbformat or LooseVersion(nbformat.__version__) < LooseVersion("4.2.0"):
--> 385             raise ValueError(
    386                 "Mime type rendering requires nbformat>=4.2.0 but it is not installed"
    387             )

ValueError: Mime type rendering requires nbformat>=4.2.0 but it is not installed

我使用的代码:

import plotly.graph_objects as go
from plotly.subplots import make_subplots
import plotly.express as px

df = df[df['Data']>0]
df['Timestamp'] = pd.to_datetime(df['Timestamp'])
df = df[(df['Id'] ==1)|(df['Id'] ==6)]

dfp = pd.pivot_table(df,
                     values='Data',
                     index=['Timestamp'],
                     columns=['Id'],
               )
nrows = len(dfp.columns) 

fig = make_subplots(rows=nrows,
                    cols=1,
                    subplot_titles=['Id '+str(c) for c in dfp.columns])

# add traces
x = 1
for i, col in enumerate(dfp.columns):
    fig.add_trace(go.Scatter(x=dfp.index, y=dfp[col].values,
                             name = 'Id '+str(col),
                             mode = 'lines',
                             ),
                  row=i+1,
                  col=1)

fig.update_layout(height=nrows*500)
fig.show()

我在GitHub和this question on stackoverflow上尝试了控制台中的pip install nbformat,但没有成功。
但是,代码似乎可以在删除最后2行的情况下运行:

fig.update_layout(height=nrows*500)
fig.show()
crcmnpdw

crcmnpdw1#

方法1

重新安装ipykernel通过

pip install ipykernel

方法2

pip install --upgrade nbformat
q5lcpyga

q5lcpyga2#

!pip install nbformat

1.安装这个。
1.重新启动内核。
1.大坝肯定会起作用的!

ekqde3dh

ekqde3dh3#

对于那些使用conda的人来说,这对我很有效:
验证conda环境的名称:conda info --envs
假设它是“myenv”,继续:
conda activate myenv
conda install nbformat
然后重新启动内核。

v9tzhpje

v9tzhpje4#

我也遇到了这个问题。在我的例子中,我只安装了ipykernel,但是最好安装jupyter元包。
参考:https://code.visualstudio.com/docs/datascience/jupyter-notebooks

相关问题