Huge proglem to load Plotly plots in Jupyter Notebook Python

放肆的年华 提交于 2021-02-08 10:37:23

问题


I have a huge problem with Jupyter Notebook. When he builds graphs using the plot packet, to display the next graph I have to reload the dataset, otherwise an error pops up: TypeError: list indices must be integers or slices, not str. When I reopen the project, all the graphs created using Plotly are not visible, just a white background and I have to reload them if I want to see them, and everything after reloading the dataset, that is:

1) I open the project in Jupyte Notebook, all graphs created by Plotly are not visible (others from Seaborn for example are visible.

2) I load the Plotly charts, but I can load them one by one, because first I have to load the dataset, then the chart, and so an error occurs to all Plotly charts differently: TypeError: list indices must be integers or slices

what can I do about it how to fix it? it seriously hinders the work

this is my libraries:

import pandas as pd
import numpy as np
import matplotlib.pyplot as plt
import seaborn as sns
%matplotlib inline
import plotly.offline as py
import plotly.graph_objs as go
import plotly.tools as tls

This is an example of this error, but if I load the dataset again and again load this plot everything will be ok:

and the example of the code of plot:

#Distribution of credit risk in dataset (target variable)

#Size of the plot
figsize=(10,5)

#Sums of good and bad credits in the dataset
goodCount = data[data["Risk"]== 'good']["Risk"].value_counts().values
badCount = data[data["Risk"]== 'bad']["Risk"].value_counts().values

#Bar fo good credit
trace0 = go.Bar(x = data[data["Risk"]== 'good']["Risk"].value_counts().index.values,
               y = data[data["Risk"]== 'good']["Risk"].value_counts().values,
               name='Good credit',
               text= goodCount, 
               textposition="auto", 
               marker = dict(color = "green", line=dict(color="black", width=1),),opacity=1)

#Bar of bad credit
trace1 = go.Bar(x = data[data["Risk"]== 'bad']["Risk"].value_counts().index.values,
               y = data[data["Risk"]== 'bad']["Risk"].value_counts().values,
               name='Bad credit', 
               text= badCount, 
               textposition="auto", 
               marker = dict(color = "red", line=dict(color="black", width=1),),opacity=1)

#Creation of bar plot 
data = [trace0, trace1]
layout = go.Layout()
layout = go.Layout(yaxis=dict(title='Count'),
                   xaxis=dict(title='Risk variable'),
                   title='Distribution of target variable in the dataset')


fig = go.Figure(data=data, layout=layout)
fig.show()

来源:https://stackoverflow.com/questions/60476959/huge-proglem-to-load-plotly-plots-in-jupyter-notebook-python

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!