我正在Jupyter Notebook中运行它。我将附上完整的代码。我正在使用来自Kaggle的csv文件来绘制世界各地不同国家的累积冠状病毒病例。
以下是Kaggle数据集下载的链接:https://www.kaggle.com/sudalairajkumar/novel-corona-virus-2019-dataset我正在使用“ covid_19_data.csv”文件。
import chart_studio.plotly as py
import plotly.graph_objs as go
import pandas as pd
from plotly.offline import download_plotlyjs, init_notebook_mode, iplot, plot
init_notebook_mode(connected = True)
cf.go_offline()
df = pd.read_csv('covid_19_data.csv')
data = dict(type = 'choropleth',
locations = df['Country/Region'],
z = df['Confirmed'],
text = df['Province/State'],
colorbar = {'title':'Cases of COVID-19'} )
layout = dict(title = '2020 Global Coronavirus Cases', geo = dict(showframe = False, projection = {'type':'natural earth'}))
choromap = go.Figure(data = [data],layout = layout)
iplot(choromap)
输出是世界的灰色地图。有一个带有颜色的图例和一个标题。我很困惑为什么不绘制数据!
感谢您的帮助!
您需要添加locationmode = 'country names'
。
import pandas as pd
import plotly.graph_objs as go
from plotly.offline import init_notebook_mode, iplot
init_notebook_mode(connected = True)
df = pd.read_csv('covid_19_data.csv')
layout = dict(title = '2020 Global Coronavirus Cases',
geo = dict(showframe = False, projection = {'type':'natural earth'}))
data = go.Choropleth(locations = df['Country/Region'],
locationmode = 'country names',
z = df['Confirmed'],
text = df['Province/State'],
colorbar = {'title':'Cases of COVID-19'})
fig = go.Figure(data = data, layout = layout)
iplot(fig)