# 2.1.9.2.Choropleth Maps - World

## 1. 使用library

```
import plotly.plotly as py
import plotly.graph_objs as go 
from plotly.offline import download_plotlyjs,init_notebook_mode,plot,iplot
init_notebook_mode(connected = True)
```

## 2.從實際資料中繪製Choropleth Map

* 讀取資料
  * 可在資料中放入想要顯示的country code, z值 (數量), label

    ```
    import pandas as pd
    df = pd.read_csv('2014_World_GDP')
    df.head()
    ```

    ![](/files/-M4M0SoXopAfx35v8csP)
* Choropleth Map的兩個要素 Data, Layout
  * Data的locationmode預設為country code, 因此若locations是country code則不需指定locationmode

    ```
    data = dict(
      type = 'choropleth',
      locations = df['CODE'],
      z = df['GDP (BILLIONS)'],
      text = df['COUNTRY'],
      colorbar = {'title' : 'GDP Billions US'},
    ) 

    layout = dict(
    title = '2014 Global GDP',
    geo = dict(
      showframe = False,
      projection = {'type':'Mercator'}
    )
    )
    ```
  * 若資料來源是country name, 則必須在指定locationmode中指定: locationmode = "country names"

    ```
    data = dict(
      type = 'choropleth',
      locations = df['COUNTRY'],
      locationmode = "country names",
      z = df['GDP (BILLIONS)'],
      text = df['COUNTRY'],
      colorbar = {'title' : 'GDP Billions US'},
    )

    layout = dict(
    title = '2014 Global GDP',
    geo = dict(
      showframe = False,
      projection = {'type':'Mercator'}
    )
    )
    ```
* 製作Choropleth Map

```
choromap = go.Figure(data = [data], layout = layout)
iplot(choromap)
```

![](/files/-M4M0So_cEUUXcxL9T_i)


---

# Agent Instructions: Querying This Documentation

If you need additional information that is not directly available in this page, you can query the documentation dynamically by asking a question.

Perform an HTTP GET request on the current page URL with the `ask` query parameter:

```
GET https://jen-hsuan-hsieh.gitbook.io/python/chapter-2courses/21python-for-data-science-and-machine-learning-bootcamp/dsad/2192choropleth-maps-world.md?ask=<question>
```

The question should be specific, self-contained, and written in natural language.
The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
