我有以下Python 3.6代码。当我将代码作为一个程序运行(在Spyder IDE中)时,我应该在浏览器的两个不同选项卡中得到两个数字。但是,两个选项卡都显示了fig2。当我分别(手动)运行fig1和fig2的代码时,那么我会在两个选项卡中正确获得fig1和fig2。看来,当我一次运行整个程序时,第二个数字以某种方式覆盖了第一个数字。不知道出什么问题了。我该如何纠正?非常感谢!
这是csv文件中的数据: enter image description here
import plotly.offline as pyo
import plotly.graph_objs as go
import plotly.plotly as py
import pandas as pd
from plotly import tools
filepath = r'C:\Data'
ethnicity = pd.read_csv(filepath+'/Ethnicity.csv', header=None, names=['number'])
#chart 1
pietrace1 = {'type':'pie',
'labels':ethnicity.index.tolist(),
'values':ethnicity.number,
'direction':'clockwise'
}
data1 = [pietrace1]
layout1 = {'title':'Ethnicity of students',
'xaxis':{'title':'Ethnicity'} ,
'yaxis':{'title':'Number'}
}
fig1 = go.Figure(data=data1, layout=layout1)
pyo.plot(fig1)
#chart 2
colors = ['rgb(12,192,170)',
'rgb(190,252,250)',
'rgb(77194,84)',
'rgb(211,238,128)',
'rgb(97,167,193)'
]
pietrace2 = {'type':'pie',
'labels':ethnicity.index.tolist(),
'values':ethnicity.number,
'direction':'clockwise',
'marker':{'colors':colors, 'line':{'width':1, 'color':'white'}}
}
data2 = [pietrace2]
layout2 = {'title':'Ethnicity of students',
'xaxis':{'title':'Ethnicity'} ,
'yaxis':{'title':'Number'}
}
fig2 = go.Figure(data=data2, layout=layout2)
pyo.plot(fig2)