
1. Plotly cheat sheet
1.1. Plotly mapping can be tricky to get used to; see attached cheat sheet
2. Simple Choropleth map example
2.1. We can create an example without any source data file, which also demonstrates the 4 steps to plot a Choropleth map
2.1.1. Define the data (as a dictionary)
2.1.1.1. data = dict(type = 'choropleth', locations = ['AZ','CA','NY'], locationmode = 'USA-states', colorscale= 'oranges', text= ['text1','text2','text3'], z=[1.0,2.0,3.0], colorbar = {'title':'Colorbar Title'})
2.1.1.1.1. type is always 'choropleth'
2.1.1.1.2. locations can reference an array (as in this case) or a Pandas DataFrame column
2.1.1.1.3. locationmode sets the context for interpreting the values passed for locations
2.1.1.1.4. colorscale defines the colouring used to represent the spectrum of "low" to "high" values
2.1.1.1.5. text can reference an array (as in this case) or a Pandas DataFrame column
2.1.1.1.6. z can reference an array (as in this case) or a Pandas DataFrame column
2.1.1.1.7. colorbar is a dictionary argument that can take a whole bunch of individual arguments for properties of the colour bar
2.1.2. Define the layout (as a dictionary)
2.1.2.1. layout = dict(geo = {'scope':'usa'})
2.1.2.1.1. geo is set also as a dictionary (meaning it supports multiple nested arguments)
2.1.3. Create the Choropleth map object
2.1.3.1. choromap = go.Figure(data = [data],layout = layout)
2.1.3.1.1. The Figure constructor takes in our predefined data and layout variables
2.1.3.1.2. Note the peculiar requirement to pas the data variable into Figure() as a list
2.1.4. Plot the Choropleth map
2.1.4.1. iplot(choromap)
2.1.4.1.1. see attached
3. USA Choropleth map example using Pandas DataFrame
3.1. Setup
3.1.1. import pandas as pd df = pd.read_csv('2011_US_AGRI_Exports') df.head()
3.1.1.1. Note that "2011_US_AGRI_Exports" is the name of a CSV file local to my notebook and supplied by the Udemy course I took
3.1.1.2. See attached for peek at DataFrame
3.2. Data
3.2.1. data = dict(type='choropleth', colorscale = 'ylorrd', locations = df['code'], z = df['total exports'], locationmode = 'USA-states', text = df['text'], marker = dict(line = dict(color = 'rgb(255,255,255)',width = 2)), colorbar = {'title':"Millions USD"} )
3.2.1.1. Note that the marker refers to the lines dividing the locations on the map
3.3. Layout
3.3.1. layout = dict(title = '2011 US Agriculture Exports by State', geo = dict(scope='usa', showlakes = True, lakecolor = 'rgb(85,173,240)') )
3.3.1.1. Note that the showlakes and lakecolor options for geo add the actual big lakes of USA into the map, and set to a nice "blue" colour via the rgb() setting
3.4. Choropleth object
3.4.1. choromap = go.Figure(data = [data],layout = layout)
3.5. Plot Choropleth map
3.5.1. iplot(choromap)
3.5.1.1. see attached
4. World Choropleth map example using Pandas DataFrame
4.1. Setup
4.1.1. import pandas as pd df = pd.read_csv('2014_World_GDP') df.head()
4.1.1.1. Note that "2014_World_GDP" is the name of a CSV file local to my notebook and supplied by the Udemy course I took
4.1.1.2. See attached for peek at DataFrame
4.2. Data
4.2.1. data = dict( type = 'choropleth', locations = df['CODE'], z = df['GDP (BILLIONS)'], text = df['COUNTRY'], colorbar = {'title' : 'GDP Billions US'}, )
4.3. Layout
4.3.1. layout = dict( title = '2014 Global GDP', geo = dict( showframe = False, projection = {'type':'mercator'} ) )
4.4. Choropleth object
4.4.1. choromap = go.Figure(data = [data],layout = layout)
4.5. Plot Choropleth map
4.5.1. iplot(choromap)
4.5.1.1. see attached
5. Why Plotly for geographical plots?
5.1. Plotly provides interactive geographical plotting capabilities for national and worlwide plots
6. Challenges of geographical data
6.1. Geographical data can come in a variety of forms, which generally forces you to select different Python libraries to plot that data, depending on its format
6.1.1. Plotly may not be able to handle the geo data you are working with; at least, not until it's been transformed into the right format that Plotly can handle
7. Additional libraries to install
7.1. See my Plotly mindmap for standard statistical plots for DataFrames for info setting up a notebook
7.2. pip install chart-studio
7.2.1. I did this via the Anaconda PowerShell Prompt because this automatically places you inside the anaconda3 virtual environment
8. Choropleth Maps
8.1. Choropleth Maps display divided geographical areas or regions that are coloured, shaded or patterned in relation to a data variable
8.1.1. This provides a way to visualise values over a geographical area, which can show variation or patterns across the displayed location
8.2. The data variable uses colour progression to represent itself in each region of the map
8.2.1. Typically, this can be a blending from one colour to another, a single hue progression, transparent to opaque, light to dark or an entire colour spectrum