How to Create Map Charts in Python (w/ Plotly & Mapbox)

This Python tutorial provides a detailed and practical step-by-step demonstration of Map Charts or Geomaps.

Geomaps are fantastic visual representation tools for interpreting and presenting data which includes location.

Data at hand that has some kind of location information attached to it can come in many forms, subjects and domains. Some examples are:

  • Global events and phenomenon
    • War
    • Conflict
    • Pandemic
    • Global warming
    • Space data
    • Immigration
    • Macroeconomics
    • Aerial/Flight data
  • Nature related events:
    • Earthquare
    • Tsunami
    • Bird, fish, mammals migration
    • Solar data
    • Wind data
    • Precipitation data
  • Socio-economic data:
    • Trade data
    • Housing and Real Estate data
    • Jobs
    • Wages & Salaries
    • Population
    • Energy data
    • Life Expectancy
  • Tourism and culture:
    • Festivals
    • Voyage
    • Celebrations
    • Holidays
    • Voyage
    • Backpacking
    • Image data

And the list goes on as we count more and more niche subjects. This type of data sometimes comes fully ready with coordinates but sometimes only location data included will be a city name, county name, country name, province name or open address.

Estimated Time

20 mins

Skill Level

Upper-Intermediate

Exercises

na

Content Sections

Tip Jar

Buy Us a Ko-Fi

Thank you! 😊🙏

In cases where you don’t have a latitude-longitude data available it becomes a joy rather than a chore to tackle this issue with Python.

We have a tutorial about geocoding, which is the process of getting coordinates from a location’s name or address, and reverse geocoding which is getting the address or name of a place from its coordinate points (latitude and longitude). Like most tasks this is handled elegantly in Python with the help of Geopy library.

You can also check out this guide about creating Plotly animations to see some other Visualization options in Python.

Used Where?

  • Exploring data
  • Presentations
  • Business Strategy
  • Science and Engineering
  • Research
  • Financial markets
  • Apps & Software
  • Websites

You can find the interactive Chart Map at the bottom of this page embedded for demonstration.

Discovering the Map Service and Getting a Free Account

First of all we’ll use Mapbox API key for map services. So it might be useful to take care of that as first step. There are many different map services out there and it might make sense to invest some time and discover different benefits and costs when a commercial application is at hand.

But, Mapbox has a generous free account and it will be perfect for this tutorial.

You can sign up here and get a free account and API key.

We will read the API key from a file you can save as following: (named .mapbox_token with no file extension)

px.set_mapbox_access_token(open(".mapbox_token").read())

Thankfully, Mapbox has a generous free plan so you shouldn’t have to worry about billing unless you have a huge traffic.

Mapbox pricing sneak peak

You can find more about the pricing scheme here: Mapbox Pricing.

Creating the map chart w/Plotly (Step-by-step)

Let’s split up the code first and investigate each part for a better understanding.

First you’ll need the libraries:

Prep & Reading Data (libraries, data file, API token etc.)

import plotly
import plotly.express as px
import pandas as pd

API Access Token will be needed as explained above, so, here is the code for that part:

px.set_mapbox_access_token(open(".mapbox_token").read())

Then, also, a data file needs to be opened and read which will be used to create the map chart later. Sorry about the creative file name:

f = 'ddd.xlsx'
df = pd.read_excel(f, index_col=0)

Heart of the operation (Creating figure object)

At this point, we are ready to create the figure object that will represent the map chart using express module of plotly library.

Here are the main parameters that’s used in the creation of map chart:

  • df is the first argument representing dataframe containing input data
  • lat: latitude data for each data point
  • lon: longitude data for each data point

Here are some optional parameters that are used that make the map much prettier and functional:

  • hover_name: Defines the data to be shown when mouse pointer is hovered over each data point.
  • size: Points to data that sizes are based on
  • color_continuous_scale: Applies an appropriate color scale of choice
  • size_max: limits the maximum size of each data
  • zoom: defines map zoom level upon loading. 1 is for most zoomed out map
fig = px.scatter_mapbox(df, hover_name='location', lat="lat", lon="lon", size="total_deaths_per_million", color="total_deaths_per_million",
                            color_continuous_scale=px.colors.sequential.Rainbow, size_max=30, zoom=2)

Map Styles & Layers (mapbox_style and mapbox_layer)

After that, there are different map styles you can apply to your map. Using mapbox_style is a great way to quickly give style to the map and it will dictate the overall map color scheme (this part is also known as lowest layer or base map). Here are some of the options you can try:

  • dark
  • open-street-map
  • white-bg
  • carto-positron
  • carto-darkmatter
  • stamen-terrain
  • stamen-toner
  • stamen-watercolor
  • basic
  • streets
  • outdoors
  • light
  • dark
  • satellite
  • satellite-streets
fig.update_layout(mapbox_style="open-street-map")

Also layers of the map can be specifically assigned by using an external source. Here is an example using Nationalmap from U.S. Geological Survey’s (USGS) National Geospatial Map.

    # fig.update_layout(
    #     mapbox_style="white-bg",
    #     mapbox_layers=[
    #         {
    #             "below": 'traces',
    #             "sourcetype": "raster",
    #             "source": [
    #                 "https://basemap.nationalmap.gov/arcgis/rest/services/USGSImageryOnly/MapServer/tile/{z}/{y}/{x}"
    #             ]
    #         }
    #       ])

It’s commented out so in case you’d like to give it a try please remember to remove the sharp signs.

Saving map chart locally (plotly.offline.plot)

Finally, you can save the map chart locally as an html file by using the following code:

Just adjust the file path based on your needs.

plotly.offline.plot(fig, filename=r'C:/Users/ABC/Desktop/map_chart.html')

Map Chart (Full code)

It’s always a good idea to create a function when code is getting long or whenever it makes sense. This allows coder to not worry about correctness and integrity of each line during the development phase. You can just comment out a single line where you call the function and nothing will be executed.

But there are many more benefits. It also allows easy reproduction, easier reading and gives structure to the overall program.

Here is the full code compartmentalized as a user-defined function for easier use and reproduction:

def vijulize():
    import plotly
    import plotly.express as px
    import pandas as pd
    
    px.set_mapbox_access_token(open(".mapbox_token").read())

    f = 'ddd.xlsx'
    
    df = pd.read_excel(f, index_col=0)
    fig = px.scatter_mapbox(df, hover_name='location', lat="lat", lon="lon", size="total_deaths_per_million", color="total_deaths_per_million",
                            color_continuous_scale=px.colors.sequential.Rainbow, size_max=30, zoom=1)
    
    fig.update_layout(mapbox_style="dark")
    plotly.offline.plot(fig, filename=r'C:/Users/ABC/Desktop/map_chart.html')

vijulize()

If you take a look at the full code, here is a summary of what’s happening:

  • First 3 lines inside function imports necessary libraries
  • Next authorization is handled with a file which includes our API key
  • Next file path including the data is defined
  • After that data is read using pd.read_excel

After these pre-figure steps, which mostly handle supportive tasks such as opening and reading data, authorization, libraries etc. there are 3 major steps in the code:

  • Creating the figure as an object named fig
  • Updating fig with a cool color scheme
  • Saving fig using plotly.offline.plot() method

Saving and Embedding Geomaps (Map Chart)

There are multiple methods and ways about saving a map chart created by Plotly. It’s up to you to discover, investigate and choose the method that makes most sense to your work or application.

Map Chart File Embedding Solutions (html format)

Commonly there are 2 distinct methods approaches:

  • Saving charts, animations, maps as html, json, png, gif, jpg on local disks. (Offline method)
  • Saving charts, animations, maps as html, json, png, gif, jpg on cloud, website or a server such as Chart Stuido, Github, Hosting Service, Dropbox, Google Drive etc.  (Online method)
As mentioned above, in this tutorial, we used the little code below to save the map chart as an html file locally first.
plotly.offline.plot(fig, filename=r'C:/Users/ABC/Desktop/map_chart.html')
After that, this html file was uploaded to HolyPython’s Github repository and embedded using iframe html tags.
 
We have a detailed tutorial about Saving Plotly Animations and Charts where you can lots of information about different saving methods, take a look here: How to save Plotly Charts and Animations
 
Here is an iframe example that can be used to embed an html file like at the bottom of this page.
<iframe width="900" height="800" frameborder="0" scrollng="no" src="https://holypython.github.io/holypython2/somefile.html"></iframe>

Demonstration

Et voila! Map is ready to be presented. What do you think? Isn’t it beautiful. It’s just amazing how far we came in data visualization in recent years thanks to coding pioneers in the field such as Mapbox, Plotly, Geopy and many other libraries.

What do you think about the map? I found these points particularly interesting at first sight:

  • U.S. doesn’t seem to be doing so bad as we’ve seen in this tutorial based on nominal numbers. Belgium is taking the biggest hit by far in that compartment.
  • Sweden’s death per million numbers are off the charts compared to its Nordic neighbors Norway, Denmark and Finland. Maybe it’s time to reconsider herd immunity arguments
  • Italy, France and the U.K. all seem to have high death per million numbers. Many culprits there likely: elderly population, lack of virutic epidemic experience in recent decades in the area (compared to Asian countries like Japan or South Korea), well established global travel, tourism and trade, transparent and honest case reporting etc. etc.

How to Save Plotly Animations: The Ultimate Guide

This Python tutorial provides a detailed and practical step-by-step demonstration of different saving options regarding Plotly charts, plots, graphs and animations.

Check out this practical Plotly guide about creating Plotly animations if you’d like to read first about Creating Visualizations with Plotly in Python.

Used Where?

  • Exploring data
  • Presentations
  • Business Strategy
  • Scientific Research
  • Financial markets
  • Applications
  • Websites

In this tutorial we will work on external data regarding Covid19 (or Coronavirus).

You can find the fully functional & interactive Plotly animation embedded at the bottom of this page for demonstration.

Estimated Time

20 mins

Skill Level

Upper-Intermediate

Exercises

na

Content Sections

Course Provider

Provided by HolyPython.com

This tutorial builds on the fig object we created in the First Part.

Here is a sneak peak to the fig object that was used to create the Plotly Express Animation:

fig = px.scatter(df, x="total_cases", y="total_deaths", animation_frame="date", animation_group="location",
            size="population", color="continent", hover_name="location",
            log_x=True, log_y=False, size_max=45, range_x=[100,10000000], range_y=[25,140000])

This tutorial will be a comprehensive guide providing lots of different online and offline methods to manage your animations, save Plotly animations as well as embedding options for websites and apps.

I will also try to address the potential glitches and errors you might encounter while saving Plotly Animations.

Plotly’s animated charts are of superb quality and have a tremendous potential for use in commercial applications, education purposes and scientific research.

If you find this work useful please consider showing this page in your references, by linking directly or sharing on social media.

Let’s start with the online possibilities.

Saving Plotly Animations: Online Methods

There are multiple sophisticated ways to save Plotly Animations. This tutorial demonstrates some of those practical solutions:

Chart Studio (chart_studio library)

Chart Studio is a very convenient and resourceful cloud service provided by Plotly. It’s quite costly for enterprise options but for small operations and individual use its free account can be very useful as well.

Let’s discover options with Chart Studio. 

First all the relevant libraries can be imported like this:

import chart_studio.plotly as py
import chart_studio.tools as tls
import chart_studio

Plotly animation can be saved on the cloud with a simple 1-line code. 

But you also need to identify with your credentials as below:

Also, you can get a Free Plotly Chart Studio Account from this link.

chart_studio.tools.set_credentials_file(username='holypython', api_key='Enter your API key here')

py.plot(fig, filename = 'file', auto_open=True)
PlotlyRequestError: This file is too big! Your current subscription is limited to 524.288 KB uploads. For more information, please visit: https://plotly.com/get-pricing/.

Chart Studio Embedding Option (Hosting at Chart Studio)

Now that you have your animation on the cloud you can simply get an embedding html code in i-frame format with the code below. You’re just going to have to get the link to your animation on the cloud. You can easily find it by navigating to your account and clicking the share icon.

Chart Studio Account Overview

After navigating to Your Files page simply hover over your file and you will see the share button like in the image below:

Chart Studio Share Button

1. Using get_embed to get embedding code

url='https://plotly.com/~youraccount/3/'
a=tls.get_embed(url)
print(a)
<'iframe id="igraph" style="border: none;" src="https://plotly.com/~youraccount/3.embed" width="100%" height="525" scrolling="no" seamless="seamless"><'/iframe<

2. Simply get embedding code from your Chart Studio Account Page

You can use iframe tag you get on your console to embed the animation on websites or apps. Alternatively, when you click on share button from your Plotly Chart Studio account there will be an embedding option that will directly give the iframe code manually.

After clicking share button on your Plotly Chart Studio Page, you can navigate to Embed tab and get the code from there as shown in the image below:

Chart Studio Embed Code

Self Hosted Embedding Options (Github or Personal Server)

This part is pretty straightforward and can be very useful. The catch is you will need space and if you have a large audience strong bandwidth privileges.

1. Self-hosted on your personal server

Simply upload your animation file to your server as html to your own website or a cloud service and replace the file location in the iframe code as below:

<iframe id="igraph" scrolling="no" style="border:none;" seamless="seamless" src=https://holypython.com/covid_cases_2020.html" height="525" width="100%"></iframe>

Here  is an alternative iframe tag you can use to create your own iframe by just replacing the url with yours.

Also feel free to adjust the size of the animation via width and height parameters.

<iframe width="900" height="800" frameborder="0" scrollng="no" src="https://holypython.github.io/holypython2/covid_cases.html"></iframe>

2. Using 3rd party cloud and/or website services

Here are some more ideas to host your online animation file:

In Github case you can simply start a public repository, upload your file to it, commit the file and then share its link in the iframe. This method actually works pretty smoothly and reliability is not a concern at all with Github.

There can be other services available for this method just use some creativity if you need an alternative.

Saving Plotly Animations: Offline Methods

There are multiple sophisticated ways to save Plotly Animations. This tutorial demonstrates some of those practical solutions:

Various Offline saving options for Plotly Animation:

help(fig)
 |  write_html(self, *args, **kwargs)
| Write a figure to an HTML file representation
|
| write_image(self, *args, **kwargs)
| Convert a figure to a static image and write it to a file or writeable
| object
|
| write_json(self, *args, **kwargs)
| Convert a figure to JSON and write it to a file or writeable
| object

Besides there are more options to discover, in summary:

  • You can convert the animation to an html in string format
  • You can convert the animation to a JSON in string format
  • You can convert the animation to a JSON in dictionary format
  •  You can convert the animation to an ordered dictionary 
 |  to_html(self, *args, **kwargs)
| Convert a figure to an HTML string representation.
|
| to_image(self, *args, **kwargs)
| Convert a figure to a static image bytes string
|
| to_json(self, *args, **kwargs)
| Convert a figure to a JSON string representation
|
| to_ordered_dict(self, skip_uid=True)
|
| to_plotly_json(self)
| Convert figure to a JSON representation as a Python dict
|
| update(self, dict1=None, overwrite=False, **kwargs)
| Update the properties of the figure with a dict and/or with
| keyword arguments.
|
| update_layout(self, dict1=None, overwrite=False, **kwargs)
| Update the properties of the figure's layout with a dict and/or with
| keyword arguments.
|
| update_traces(self, patch=None, selector=None, row=None, col=None, secondary_y=None, overwrite=False, **kwargs)
| Perform a property update operation on all traces that satisfy the
| specified selection criteria

Besides there are more options to discover, in summary:

  • You can convert the animation to an html in string format
  • You can convert the animation to a JSON in string format
  • You can convert the animation to a JSON in dictionary format
  •  You can convert the animation to an ordered dictionary
Depending on your data structure and preferences it’s amazing to have all these options out of the box.
Let’s do some practical applications with some of the file writing options in Plotly.express

Saving as Html File (write_html method)

Saving the animation as an html file got to be the most straightforward option. Just execute the code below and you’ll have an offline animation file:

fig.write_html("Desktop/file.html")

Alternatively you can use this code to save animations locally:

plotly.offline.plot(fig, filename=r'C:/Users/ABC/Desktop/file.html')

Saving as Json File (write_json method)

Saving the animation as an json file is equally practical with some helpful optional parameters:

fig.write_json("Desktop/file.json")

With the code above, you’re going to have a raw json that’s hard to read and strange to the eye.

By throwing in the pretty parameter and assigning True boolean to it you can have an indented and structured JSON file that’s actually readable by humans and pleasant to look at:

fig.write_json("Desktop/file.json", pretty=True)

Saving as Image file (write_image method)

There is an extra step involved with saving images from plotly.express library.

You will need to install orca library to facilitate the image saving with plotly and then you can save in numerous image formats such as:

  • jpg
  • png
  • svg

Here is the error you will encounter without orca in Plotly:

fig.write_image("Desktop/file.jpg")
ValueError: 
The orca executable is required to export figures as static images,
but it could not be found on the system path.

Let’s get orca, it’s pretty simple. 

From your Anaconda Command Prompt:

conda install -c plotly plotly-orca

You can read more about official orca installation instructions and alternative methods here.

Now with orca it should work like a charm. Let’s try again:

fig.write_image("Desktop/file.jpg")

Please note that you might need to change the file path and name. Also you might in some cases have to type the full path and use raw string format such as:

r'c://Users/ABC/Desktop/mygraph.jpg'

Raw string are really useful and fix lots of path conflicts in Python.

 

Reducing Plotly Animation File Size (include_plotlyjs)

There is a  great parameter to handle the inclusion of plotly.js source code when saving the animation or chart.

plotly.js is the source code that your plot uses for visualization when it’s opened up. It’s not a unique part of your chart, animation or plot but rather the infrastructure that facilitates how your chart or animation is displayed.

By excluding this file when saving your chart/animation you can save a great amount of space (approximately 3 MB per file).

However, if you opt to exclude this javascript source code, your animation or chart won’t be displayed in a system where plotly.js isn’t already included. To be more clear:

  • if you embed the chart or animation in your website without plotly.js you still have to make sure plotly.js loads up somehow.
  • if you’d like to open the html file you saved locally in your browser, it won’t work unless you also load up plotly.js separately in your browser somehow, etc.

It can still be very useful in some cases to exclude this javascript code and reduce size dramatically.

  • Animation or chart will work if it’s uploaded to Chart Studio since this environment already includes plotly.js automatically.
  • Multiple animations will still work if you load ployly.js once in your webpage. So you can save lots of space (file amount times 3MB) in that case.

So use cases can vary and be more broad than the ones we explain here. But, here is the parameter and code piece that would allow you to exclude the plotly.js source code:

include_plotlyjs=False

Below you can see the Plotly Animation, play the animation and interact with it in different ways:

How to Create Plotly Animations: The Ultimate Guide

Plotly library offers amazing opportunities for

  • data visualization;
  • static charts,
    • scatter plot,
    • line graph
    • bar chart
    • geodata charts
    • geographic plots
    • pie charts
    • bubble charts
    • map charts
    • network graphs
    • area charts
    • spider charts
    • doughnut charts
    • etc. (some of these terms overlap or can be synonyms)
  • animated charts & graphs
  • interactive charts
  • data animation
  • geovisualization etc.

Where it truly shines is the possibilities it offers when it comes to embedding those beautiful visuals on websites and applications.

Used Where?

  • Exploring data
  • Presentations
  • Business Strategy
  • Scientific Research
  • Financial markets
  • Applications
  • Websites

Estimated Time

15 mins

Skill Level

Upper-Intermediate

Exercises

na

Content Sections

Course Provider

Provided by HolyPython.com

Preparation Steps for Data Visualization (Data and Libraries)

It might make sense to break up the task and process it in chunks like this:

  1. Import necessary libraries
  2. Get and Read data
  3. Create the plotly figure

Let’s take care of the 1st step already and import the needed Python libraries:

Python Libraries that will be used (Pandas & Plotly & Plotly Express)

import pandas as pd
import plotly.express as px
import plotly

Let’s take a look at the heart of the task.

fig = px.scatter(df, x="total_cases", y="total_deaths", animation_frame="date", 
    animation_group="location", range_x=[100,10000000], range_y=[25,140000])

We will use plotly.express (shortened as px) to create a scatter animation.

If you look at the parameters passed to px.scatter , we will mainly need values for these arguments:

  • x= (values for x axis)
  • y= (values for y axis)
  • animation_frame= (values for each animation frame)
  • animation_group= (values for grouping data – if available)
  • range_x= (range of x axis)
  • range_y= (range of y axis)

df is the dataframe where data is contained.

fig = px.scatter(df, x="total_cases", y="total_deaths", animation_frame="date", 
    animation_group="location", range_x=[100,10000000], range_y=[25,140000])

Passing values to plotly is so straighforward and intuitive. You just need to read a dataframe (it could be read from many sources such as excel files; .xls, .xlsx, .csv, .txt, database etc.)

Once dataframe is constructed all that’s left to be done is to pass the column names as values to the parameters mentioned above.

Now, let’s get a decent dataframe ready. We will come back to this later.

Data Source Ideas (Many different fields and sources)

As data science grows and matures, today, we have incredible sources for proper and clean data as well as raw data. You should never have a tough time finding data to explore unless you’re working on a niche or new field / subject.

On top of that you can explore a few datasets already included in plotly for your experimenting convenience. But you should definitely taste the joy of finding your own data and constructing your own Plotly Animations. Here is how you can check out already included Plotly Datasets:

import plotly.express as px
print(help(px.data))

—SUMMARIZED OUTPUT—

Name : plotly.express.data – Built-in datasets for demonstration, educational and test purposes.

Functions:
carshare() – Each row represents the availability of car-sharing services near the centroid of a zone in Montreal over a month-long period.

election() – Each row represents voting results for an electoral district in the 2013 Montreal mayoral election.

election_geojson() – Each feature represents an electoral district in the 2013 Montreal mayoral election.

gapminder() – Each row represents a country on a given year.

iris() – Each row represents a flower.

tips() – Each row represents a restaurant bill.

wind() – Each row represents a level of wind intensity in a cardinal direction, and its frequency.

You can use any of the built-in dataset by assigning them to a dataframe variable as: 

df = px.data.carshare()
df = px.data.election()
df = px.data.election()
df = px.data.election_geojson()
df = px.data.gapminder()
df = px.data.iris()
df = px.data.tips()
df = px.data.wind()

In this tutorial we will work on external data regarding Covid19 (or Coronavirus).

Reading Data into Pandas and Plotly

Now we need to get some data ready. I think Covid numbers are interesting.

Here is a Excel sneak peak of the data that I have.
I have slightly cleaned it so that:

  • Only countries with population above 1M is included.
  • Blank cells are excluded
  • Date starts from March 1

pandas.read_excel is perfectly appropriate to read data from this excel file. Here are some of the other ways to read data with pandas library of Python should you like to work with different file formats than Excel. You can even read data from clipboard, so cool.

f = 'Desktop/covid-data7.xlsx'
file = open(f, "r")
df = pd.read_excel(f, index_col=0)

Various Pandas methods to read data from different sources:

pandas.read_pickle
pandas.read_table
pandas.read_csv
pandas.read_fwf
pandas.read_clipboard
pandas.read_excel
pandas.read_json
pandas.read_html
pandas.read_hdf
pandas.read_feather
pandas.read_parquet
pandas.read_orc
pandas.read_sas
pandas.read_spss
pandas.read_sql_table
pandas.read_sql_query
pandas.read_sql
pandas.read_gbq
pandas.read_stata

Now that we the data part figured out, we can start the visualization part. But first, let’s explore it a little bit with Python without having to open Microsoft Excel.

.head() and .columns can be useful here.

print(df.head())
         continent location  ... hospital_beds_per_thousand  life_expectancy
iso_code ...
CHN Asia China ... 4.34 76.91
CHN Asia China ... 4.34 76.91
CHN Asia China ... 4.34 76.91
CHN Asia China ... 4.34 76.91
CHN Asia China ... 4.34 76.91
CHN Asia China ... 4.34 76.91
CHN Asia China ... 4.34 76.91
CHN Asia China ... 4.34 76.91
CHN Asia China ... 4.34 76.91
CHN Asia China ... 4.34 76.91
print(df.head().columns)
Index(['continent', 'location', 'date', 'total_cases', 'new_cases',
'total_deaths', 'new_deaths', 'total_cases_per_million',
'new_cases_per_million', 'total_deaths_per_million',
'new_deaths_per_million', 'total_tests', 'new_tests',
'total_tests_per_thousand', 'new_tests_per_thousand',
'new_tests_smoothed', 'new_tests_smoothed_per_thousand', 'tests_units',
'stringency_index', 'population', 'population_density', 'median_age',
'aged_65_older', 'aged_70_older', 'gdp_per_capita', 'extreme_poverty',
'cvd_death_rate', 'diabetes_prevalence', 'female_smokers',
'male_smokers', 'handwashing_facilities', 'hospital_beds_per_thousand',
'life_expectancy'],
dtype='object')
for i in (df.head().columns):
    print(i, end=' || ')
continent || location || date || total_cases || new_cases || total_deaths || new_deaths || total_cases_per_million || new_cases_per_million || total_deaths_per_million || new_deaths_per_million || total_tests || new_tests || total_tests_per_thousand || new_tests_per_thousand || new_tests_smoothed || new_tests_smoothed_per_thousand || tests_units || stringency_index || population || population_density || median_age || aged_65_older || aged_70_older || gdp_per_capita || extreme_poverty || cvd_death_rate || diabetes_prevalence || female_smokers || male_smokers || handwashing_facilities || hospital_beds_per_thousand || life_expectancy || 

Now, we’re actually ready to create our Plotly Animation. Let’s try a scatter using plotly’s px module.

fig = px.scatter(df, x="total_cases", y="total_deaths", animation_frame="date", 
    animation_group="location", range_x=[100,10000000], range_y=[25,140000])

Opening Plotly Animation

If you managed to come so far without errors, congratulations now you have an awesome Plotly animation at hand.

You might be excited to open it and see what you’ve created so let’s get to that.

Plotly offers cloud solutions for Data Visualization. That’s why you will hear or read about offline and online methods to open its visualizations.

Online method refers to using its convenient cloud service: Chart Studio while offline method refers to having a local output and opening that file.

Offline Saving Method (write_html)

Opening a plotly animation is as simple as saving it on your Desktop with a piece of code as below:

fig.write_html("Desktop/file.html")

Please note that you might need to change the file path and name. Also you might in some cases have to type the full path and use raw string format such as:

r'c://Users/ABC/Desktop/mygraph.html'

Raw string works like a charm when you encounter path conflicts in Python sometimes.

Your visualization can be opened as an html file in any browser.

Below you can see the Plotly Animation based on Covid data, play the animation and interact with it in different ways:

This animated chart is just a super simple data representation but it’s missing lots of optional parameters such as size, color and grouping. Also you can see that both x and y axes are not logarithmic. This causes lots of datapoints to be clustered while only one or two extreme points take off.

Below you can find different parameters to improve our chart:

Step up your story-telling (Size, color, grouping, hover-names and log scale)

Now, the visualization as it is right now isn’t very pleasant to the eye and it also doesn’t say that much right away. Let’s fix that.

There are very useful parameters that can be implemented to make the chart have bigger appeal. You can just add following parameters to your figure as below:

size=”population” (bubble sizes will be based on country population size)
color=”continent” (bubbles in chart will be grouped based on their continent)
hover_name=”location” (when someone hovers over the bubbles they’ll see location information)
log_x=True (will convert x axis to logarithmic scale if True)
log_y=False (will convert y axis to logarithmic scale if True)
size_max=45 (bubble size will be capped at 45)

fig = px.scatter(df, x="total_cases", y="total_deaths", animation_frame="date", animation_group="location",
            size="population", color="continent", hover_name="location",
            log_x=True, log_y=True, size_max=45, range_x=[100,10000000], range_y=[25,140000])

Now it should look much better. And, these parameters have more contribution than just the looks:

  • You can interact with each bubble by hovering on your mouse on them and see more information
  • You can also click on the continent index to include or exclude a particular continent.
  • You can also choose to do the grouping based on something else such as (size or continent) and interact with the index that way.

Online Saving Method (Plotly Chart Studio, Github and others)

You can read further regarding:

  • Plotly chart or animation saving methods
    • online saving methods
    • more offline saving methods
      • image
      • gif
      • json
      • html
  • How to embed Plotly charts and animations with interactivity in a website page

Part II: How to save Plotly charts and animations.

You can also check out Plotly’s Official Github Repository here.

Locations in Python (Geocoding w/Geopy)

Estimated Time

10 mins

Skill Level

Intermediate

Exercises

na

Content Sections

Course Provider

Provided by HolyPython.com

Geocoding is the application when you type 5th Ave. New York, NY, 10010 and you get coordinates like this: (40.740200, -73.990586).

Geocoding process maps open addresses to coordinates consisted of longitude and latitude and vice versa.

Since the new millenia started and especially after the first Iphone 2g came out, gps applications have exponentially surged. Today it’s crazy to think that people used to carry big printed foldable maps in the glow compartments of their cars and routinely stop to look up roads and cities and figure out where they are headed.

Today, there are so many applications for geocoding and it’s making people’s lives better every single day. We are also witnessing different technologies being blended for an even better optimized service such as Voice Input technology in Google maps.

As a developer you might have a specific task at hand or you might just want to learn geo-coding basics thinking you’ll probably need it sometime anyway.

Let’s take a look at how this technology can be implemented in Python.

 

Used Where?

  • Maps
  • Location apps
  • Activity tracking
  • Emergency tracking
  • Food delivery
  • e-commerce
  • Games
  • Social Media
  • Photo geo-tagging

How to install geopy

Geopy is a Python library that makes geocoding operations very accessible and practical. You can check if you have geopy in your libraries by using this code in Python:

help("modules")

If you can’t see geopy in your library selection, it might be time to have it installed. You can do so simply by opening the Anaconda Command Prompt and typing the command below:

pip install geopy

Geopy installation using pip (already installed)

Supported Geocoders (Full list)

Geopy supports majority of the major geocoders straight out of the box. 

Geocoders are a bit like APIs but for coding and they offer more services than a simple API. But you can make database calls using their APIs to retrieve data for your app, website, commercial or individual use. They usually differ in pricing, accuracy, coverage, technology, daily limits and compatibility.

Here is the full list of supported geocoders:

Specifying Parameters Once
Geopy Is Not a Service
Async Mode
Accessing Geocoders
Default Options Object
Usage with Pandas
AlgoliaPlaces
ArcGIS
AzureMaps
Baidu
BaiduV3
BANFrance
Bing
DataBC
GeocodeEarth
GeocodeFarm
Geolake

GeoNames
GoogleV3
HERE
IGNFrance
MapBox
MapQuest
MapTiler
OpenCage
OpenMapQuest
Nominatim
Pelias
Photon
PickPoint
LiveAddress
TomTom
What3Words
Yandex

Using geopy

Geocoders usually use a technique called address interpolation. This technique allows matching street, avenue, door number (street geographic information) type of parameters with geographic coordinate space that’s included in the map.

GPS is another obviously common geocoding process technique and there might be other specialized methods as well.

OpenStreetMap (OSM) is a collaborative project that offers a great geodata and map of  Earth while Nominatim is a cool geocoder that makes use of OSM geodata and it can do both geocoding and reverse geocoding. Now we will explore Nominatim with Python’s geopy library.

from geopy.geocoders import Nominatim

Normally, geocoders accept a properly written address with street name and zip code. But, let’s test Nominatim and OpenStreetMap a little.

Ben & Jerry’s is a very successful ice cream brand in the Northeastern United States. What would happen if we just pass “Ben & Jerry’s” as the address to Nominatim using geopy?

from geopy.geocoders import Nominatim
geolocator = Nominatim(user_agent="name")
address = "Ben & Jerry's, VT"
location = geolocator.geocode(address)

print(location.address)

Ben & Jerry's, Newbury Street, Back Bay, Boston, Suffolk County, Massachusetts, 02116, United States of America
Ice cream at Cafe with strawberry, mint and sorbet

Interestingly it returns a Ben & Jerry’s franchise in Boston, MA. Let’s try “Ben & Jerry’s. VT” and “Ben & Jerry’s NY” consecutively.

from geopy.geocoders import Nominatim
geolocator = Nominatim(user_agent="name")
address1 = "Ben & Jerry's, VT"
address2 = "Ben & Jerry's, NY"

location = geolocator.geocode(address1)
print(location.address)

location = geolocator.geocode(address2)
print(location.address)
Ben & Jerry's, Cherry Street, Church Street Marketplace, Burlington, Chittenden County, Vermont, 05401, United States of America
Ben & Jerry's, Lomb Memorial Drive, Genesee Junction, Henrietta Town, Monroe County, New York, 14624-4721, United States of America

We get 2 seemingly random stores again. The one in Burlington seems pretty cool on Google Maps.

So, this is good enough on Nominatim’s part. Without even completing the address we are getting accurate results. Let’s try some other places from the world and see if non-US data is as accurate:

from geopy.geocoders import Nominatim
geolocator = Nominatim(user_agent="name")
address1 = "Moulin Rouge"
address2 = "Noma"

location = geolocator.geocode(address1)
print(location.address)

location = geolocator.geocode(address2)
print(location.address)
Le Moulin Rouge, Boulevard de Clichy, Quartier des Grandes-Carrières, Paris, Île-de-France, France métropolitaine, 75018, France
Noma, Refshalevej, Margretheholm, Christianshavn, København, Københavns Kommune, Region Hovedstaden, 1440, Danmark

Okay, pretty impressive. It even recognizes Denmark’s award winning Michelin Restaurant and shows up the address for only typing Noma. Let’s try to run “latitude”, “longitude” and “raw” properties for Noma as well.

from geopy.geocoders import Nominatim
geolocator = Nominatim(user_agent="name")
address = "Noma"
location = geolocator.geocode(address)

print((location.latitude, location.longitude))

(55.6829314, 12.6104818)
print(location.raw)
{'place_id': 62456056, 'licence': 'Data © OpenStreetMap contributors, ODbL 1.0. https://osm.org/copyright', 'osm_type': 'node', 'osm_id': 5416925514, 'boundingbox': ['55.6828814', '55.6829814', '12.6104318', '12.6105318'], 'lat': '55.6829314', 'lon': '12.6104818', 'display_name': 'Noma, Refshalevej, Margretheholm, Christianshavn, København, Københavns Kommune, Region Hovedstaden, 1440, Danmark', 'class': 'amenity', 'type': 'restaurant', 'importance': 0.46778439681543516, 'icon': 'https://nominatim.openstreetmap.org/images/mapicons/food_restaurant.p.20.png'}

It makes more sense to print this one with Python’s pretty print library:

import pprint
pprint.pprint(location.raw)
{'boundingbox': ['55.6828814', '55.6829814', '12.6104318', '12.6105318'],
'class': 'amenity',
'display_name': 'Noma, Refshalevej, Margretheholm, Christianshavn, København, '
'Københavns Kommune, Region Hovedstaden, 1440, Danmark',
'icon': 'https://nominatim.openstreetmap.org/images/mapicons/food_restaurant.p.20.png',
'importance': 0.46778439681543516,
'lat': '55.6829314',
'licence': 'Data © OpenStreetMap contributors, ODbL 1.0. '
'https://osm.org/copyright',
'lon': '12.6104818',
'osm_id': 5416925514,
'osm_type': 'node',
'place_id': 62456056,
'type': 'restaurant'}

In raw data from geopy we can see bunch of useful information such as type (shown as restaurant here), latitude, longitude, class (shown as amenity) and importance.

Distance calculations with Geopy

You can also calculate the distance between two locations or points using geopy.distance module.

Let’s see the distance between a Department Store with Tesla Supercharger near Orly Airport and Paris’ finest ice-cream shop Berthillon. Geopy has been very successful at reverse geocoding place names to coordinates, let’s see if it can identify exact locations with little information again such as name.

from geopy import distance
import geopy

location1 = geolocator.geocode("Belle Épine, Thiais")
location2 = geolocator.geocode("Berthillon")
a=(location1.latitude, location1.longitude)
b=(location2.latitude, location2.longitude)

print(distance.distance(a, b).miles)

“Belle Epine” wasn’t being picked up by Geopy by itself so adding the district’s name was a good idea. It’s probably always a good idea to be as explicit as possible with addresses anyways. 

6.630156139356959

The distance can be expressed in many differeny measurement units other than miles. Let’s take a look at what help() function returns on geopy.distance.distance:

help(geopy.distance.distance)
 |  feet
|
| ft
|
| kilometers
|
| km
|
| m
|
| meters
|
| mi
|
| miles
|
| nautical
|
| nm

Great, there are tons of options! Let’s try km, since miles won’t make a lot of sense in France.

 

from geopy import distance
import geopy

location1 = geolocator.geocode("Belle Épine, Thiais")
location2 = geolocator.geocode("Berthillon")
a=(location1.latitude, location1.longitude)
b=(location2.latitude, location2.longitude)

print(distance.distance(a, b).km)
10.670202001937287

Sentiment Analysis in Python (w/Textblob & NLTK)

Estimated Time

10 mins

Skill Level

Intermediate

Exercises

na

Content Sections

Course Provider

Provided by HolyPython.com

Used Where?

  • Analysis of text, books, messages, posts, poems, media, news, social media…
  • Writing-support tools (auto-correction, auto-completion, spell check)
  • Sentiment analysis 
  • Financial investment decision making (through analysis of market sentiment)
  • Policy making (analysis of public reaction on specific matters)
  • Voice applications
  • Understanding of human emotions by computers
  • Search Engines
  • Business Development and Business Inteligence
  • Video Games and Virtual Reality
  • Increasingly useful and common Voice Assistants (Google Assistant, Cortana, Siri, Alexa, Athena, Mycroft, Aido, Cubic, Hound, Databot, AIVC, Butleroy, Robin, Lyra, Maluuba, Dragon, Lucida, Bixby, Vlingo, Silvia to name a few.)

How to install pip from Anaconda

First it makes sense to have pip installed (if you don’t have it already) before proceeding to add textblob to your Python library.

conda install pip

 

conda install pip (already installed)

To check if you have any of the needed libraries installed (pip, nltk, textblob) you can also try executing this command in Python:

It will list all the Python modules installed then you can scroll and check if you have the ones you need.

 

help("modules")

How to install textblob from Anaconda

 

You can install textblob from Anaconda Command Prompt. It’s simple as typing the command below:

pip install textblob

Once installed you can start importing textblob in Python using your favorite software such as Spyder, Jupyter Notebook, Pycharm, Vim, Atom, Sublime or Python in Command Prompt.

How to install Corpora Data

After you install textblob, finally, you need to make sure you have corpora files for sentiment analysis.

This can be done in numerous ways, here is a convenient one:

GUI method (NLTK Downloader)

You can type the code below in Python and execute it:

You don’t have to install nltk separately because it comes with Anaconda3 installation.

import nltk
nltk.download()

A download window will pop up.

Here you can see all the specialized corpora files that are available for installation.

NLTK Download Module for Corpora Data

Winerror 10061 (Server denies connection)

Before getting too complicated on this issue with sockets and proxies. Let’s understand why it happens and the most likely underlying cause.

CHANGE THIS
That suggests the remote machine has received your connection request, and send back a refusal (a RST packet). I don’t think this is a case of the remote machine simply not having a process listening on that port (but i could be wrong!).

That sounds like a firewall problem. It could be a firewall on the remote machine, or a filter in the network in between, or, perhaps on your local machine – are you running any kind of security software locally?

So, are you using a firewall or a security software? Anything that might obstruct the connection more than usual? For instance if you’re using a VPN connection such as NordVPN or ExpressVPN their ad blocking security system can cause this error and even if you disconnect from the VPN while the app is still running in the background connection error will occur.

The Solution:

Simply troubleshoot any extra security layer you might have in use and temporarily try disabling or terminating it.

Alternative Corpora Installation (Command-line method)

Alternatively you can install specific Corporas by executing either of the commands below:

This method will not solve Server rejection error since it’s a rather global problem regarding your computer environment. Also, NLTK Downloader really offers a nice visual experience. But just in case someone might prefer the command method here they are from the textblob official webpage:

$ python -m textblob.download_corpora

Lite corpora version: (might not cover all the needed files)

$ python -m textblob.download_corpora lite

Introduction to Sentiment Analysis and NLP

Sentiment likely comes from French word sentir which means to feel. Sentiment can be many abstract things that relate to emotions, feelings, thoughts, opinions and senses.

In computer science, sentiment analysis lives in the sweet spot where natural language processing (NLP) is carried out as a means for machines to make sense of human languages which usually involves, partially or fully; emotions, feelings, bias, conclusions, objectivity and opinions.

There are many benefits of bridging communication between humans and computers but sentiment analysis can also shed light on human to human communications as it contributes to processing capability both in terms of power, technique and efficiency.

The field is relatively new and definitely has wind in its sails since the processing capabilities keep increasing and amazing NLP opportunities continue being discovered. One can say it’s only the beginning in sentiment analysis and natural language processing.

So, it’s safe to say as Machine Learning models get more training, data keeps accumulating and more sophisticated AI modeling techniques keep coming up, the difference between computer and human communication is also getting blurry. Benefits of this technology already became enormous and will only get bigger. Let’s hope there won’t be much malintent.

Let’s discover this fantastic technology in Python with a few code examples that can be useful:

TextBlob Basics

Let’s explore some of the basic functions in textblob.

First we need to create a textblob object:

str = '''SHOULD WE COLONISE SPACE?

        In conclusion, I return to Einstein. If we find a planet in the 
        Alpha Centauri system, its image, captured by a camera travelling 
        at a fifth of light speed, will be slightly distorted due to the 
        effects of special relativity. It would be the first time a 
        spacecraft has flown fast enough to see such effects. In fact, 
        Einstein’s theory is central to the whole mission. Without it 
        we would have neither lasers nor the ability to perform the 
        calculations necessary for guidance, imaging and data transmission 
        over twenty-five trillion miles at a fifth of light speed. We can 
        see a pathway between that sixteen-year-old boy dreaming of riding 
        on a light beam and our own dream, which we are planning to turn 
        into a reality, of riding our own light beam to the stars. We are 
        standing at the threshold of a new era. Human colonisation on other 
        planets is no longer science fiction. It can be science fact. The 
        human race has existed as a separate species for about two million 
        years. Civilisation began about 10,000 years ago, and the rate of 
        development has been steadily increasing. If humanity is to continue 
        for another million years, our future lies in boldly going where no one 
        else has gone before. I hope for the best. I have to. We have no other 
        option. The era of civilian space travel is coming. What do you think 
        it means to us? I look forward to space travel. I would be one of the 
        first to buy a ticket. I expect that within the next hundred years we 
        will be able to travel anywhere in the solar system, except maybe the 
        outer planets. But travel to the stars will take a bit longer. I reckon 
        in 500 years, we will have visited some of the nearby stars. It won’t be 
        like Star Trek . We won’t be able to travel at warp speed. So a round trip 
        will take at least ten years and probably much longer.
        
        From the book: Brief Answers to the Big Questions – Stephen Hawking
'''

    
    
blob = textblob.TextBlob(str)

.words (list of words)

.words property will return all the words from the text in a list.

a = blob.words
print(a)
['SHOULD', 'WE', 'COLONISE', 'SPACE', 'In', 'conclusion', 'I', 'return', 'to', 'Einstein', 'If', 'we', 'find', 'a', 'planet', 'in', 'the', 'Alpha', 'Centauri', 'system', 'its', 'image', 'captured', 'by', 'a', 'camera', 'travelling', 'at', 'a', 'fifth', 'of', 'light', 'speed', 'will', 'be', 'slightly', 'distorted', 'due', 'to', 'the', 'effects', 'of', 'special', 'relativity', 'It', 'would', 'be', 'the', 'first', 'time', 'a', 'spacecraft', 'has', 'flown', 'fast', 'enough', 'to', 'see', 'such', 'effects', 'In', 'fact', 'Einstein', '’', 's', 'theory', 'is', 'central', 'to', 'the', 'whole', 'mission', 'Without', 'it', 'we', 'would', 'have', 'neither', 'lasers', 'nor', 'the', 'ability', 'to', 'perform', 'the', 'calculations', 'necessary', 'for', 'guidance', 'imaging', 'and', 'data', 'transmission', 'over', 'twenty-five', 'trillion', 'miles', 'at', 'a', 'fifth', 'of', 'light', 'speed', 'We', 'can', 'see', 'a', 'pathway', 'between', 'that', 'sixteen-year-old', 'boy', 'dreaming', 'of', 'riding', 'on', 'a', 'light', 'beam', 'and', 'our', 'own', 'dream', 'which', 'we', 'are', 'planning', 'to', 'turn', 'into', 'a', 'reality', 'of', 'riding', 'our', 'own', 'light', 'beam', 'to', 'the', 'stars', 'We', 'are', 'standing', 'at', 'the', 'threshold', 'of', 'a', 'new', 'era', 'Human', 'colonisation', 'on', 'other', 'planets', 'is', 'no', 'longer', 'science', 'fiction', 'It', 'can', 'be', 'science', 'fact', 'The', 'human', 'race', 'has', 'existed', 'as', 'a', 'separate', 'species', 'for', 'about', 'two', 'million', 'years', 'Civilisation', 'began', 'about', '10,000', 'years', 'ago', 'and', 'the', 'rate', 'of', 'development', 'has', 'been', 'steadily', 'increasing', 'If', 'humanity', 'is', 'to', 'continue', 'for', 'another', 'million', 'years', 'our', 'future', 'lies', 'in', 'boldly', 'going', 'where', 'no', 'one', 'else', 'has', 'gone', 'before', 'I', 'hope', 'for', 'the', 'best', 'I', 'have', 'to', 'We', 'have', 'no', 'other', 'option', 'The', 'era', 'of', 'civilian', 'space', 'travel', 'is', 'coming', 'What', 'do', 'you', 'think', 'it', 'means', 'to', 'us', 'I', 'look', 'forward', 'to', 'space', 'travel', 'I', 'would', 'be', 'one', 'of', 'the', 'first', 'to', 'buy', 'a', 'ticket', 'I', 'expect', 'that', 'within', 'the', 'next', 'hundred', 'years', 'we', 'will', 'be', 'able', 'to', 'travel', 'anywhere', 'in', 'the', 'solar', 'system', 'except', 'maybe', 'the', 'outer', 'planets', 'But', 'travel', 'to', 'the', 'stars', 'will', 'take', 'a', 'bit', 'longer', 'I', 'reckon', 'in', '500', 'years', 'we', 'will', 'have', 'visited', 'some', 'of', 'the', 'nearby', 'stars', 'It', 'won', '’', 't', 'be', 'like', 'Star', 'Trek', 'We', 'won', '’', 't', 'be', 'able', 'to', 'travel', 'at', 'warp', 'speed', 'So', 'a', 'round', 'trip', 'will', 'take', 'at', 'least', 'ten', 'years', 'and', 'probably', 'much', 'longer', 'From', 'the', 'book', 'Brief', 'Answers', 'to', 'the', 'Big', 'Questions', '–', 'Stephen', 'Hawking']

.sentences (list of sentences)

.sentences property will return all the sentences from the text in a list.

a = blob.sentences
print(a)
       [Sentence("SHOULD WE COLONISE SPACE?"), 
Sentence("In conclusion, I return to Einstein."), Sentence("If we find a planet in the
Alpha Centauri system, its image, captured by a camera travelling
at a fifth of light speed, will be slightly distorted due to the
effects of special relativity."), Sentence("It would be the first time a
spacecraft has flown fast enough to see such effects."), Sentence("In fact,
Einstein’s theory is central to the whole mission."), Sentence("Without it
we would have neither lasers nor the ability to perform the
calculations necessary for guidance, imaging and data transmission
over twenty-five trillion miles at a fifth of light speed."), Sentence("We can
see a pathway between that sixteen-year-old boy dreaming of riding
on a light beam and our own dream, which we are planning to turn
into a reality, of riding our own light beam to the stars."), Sentence("We are
standing at the threshold of a new era."), Sentence("Human colonisation on other
planets is no longer science fiction."), Sentence("It can be science fact."), Sentence("The
human race has existed as a separate species for about two million
years."), Sentence("Civilisation began about 10,000 years ago, and the rate of
development has been steadily increasing."), Sentence("If humanity is to continue
for another million years, our future lies in boldly going where no one
else has gone before."), Sentence("I hope for the best."), Sentence("I have to."), Sentence("We have no other
option."), Sentence("The era of civilian space travel is coming."), Sentence("What do you think
it means to us?"), Sentence("I look forward to space travel."), Sentence("I would be one of the
first to buy a ticket."), Sentence("I expect that within the next hundred years we
will be able to travel anywhere in the solar system, except maybe the
outer planets."), Sentence("But travel to the stars will take a bit longer."), Sentence("I reckon
in 500 years, we will have visited some of the nearby stars."), Sentence("It won’t be
like Star Trek ."), Sentence("We won’t be able to travel at warp speed."), Sentence("So a round trip
will take at least ten years and probably much longer."),
Sentence("From the book: Brief Answers to the Big Questions – Stephen Hawking")]

.noun_phrases (list of phrases)

.noun_phrases property will return all the noun phrases from the text in a list.

a = blob.noun_phrases
print(a)
['should we colonise space', 'einstein', 'alpha centauri', 'light speed', 'special relativity', 'such effects', 'einstein', '’ s theory', 'whole mission', 'data transmission', 'twenty-five trillion miles', 'light speed', 'sixteen-year-old boy', 'light beam', 'own dream', 'own light beam', 'new era', 'human colonisation', 'science fiction', 'science fact', 'human race', 'separate species', 'civilisation', 'civilian space travel', 'space travel', 'solar system', 'outer planets', 'nearby stars', '’ t', 'trek', '’ t', 'warp speed', 'round trip', 'brief answers', 'stephen hawking']

.ngrams(n=4) (n consecutive words)

.ngrams property will return successive words in a tuple. You can adjust the succession amount by assigning a different value to n parameter.

a = blob.ngrams(n=4)
print(a)
[WordList(['SHOULD', 'WE', 'COLONISE']), WordList(['WE', 'COLONISE', 'SPACE']), WordList(['COLONISE', 'SPACE', 'In']), WordList(['SPACE', 'In', 'conclusion']), WordList(['In', 'conclusion', 'I']), WordList(['conclusion', 'I', 'return']), WordList(['I', 'return', 'to']), WordList(['return', 'to', 'Einstein']), WordList(['to', 'Einstein', 'If']), WordList(['Einstein', 'If', 'we']), WordList(['If', 'we', 'find']), WordList(['we', 'find', 'a']), WordList(['find', 'a', 'planet']), WordList(['a', 'planet', 'in']), WordList(['planet', 'in', 'the']), WordList(['in', 'the', 'Alpha']), WordList(['the', 'Alpha', 'Centauri']), WordList(['Alpha', 'Centauri', 'system']), WordList(['Centauri', 'system', 'its']), WordList(['system', 'its', 'image']), WordList(['its', 'image', 'captured']), WordList(['image', 'captured', 'by']), WordList(['captured', 'by', 'a']), WordList(['by', 'a', 'camera']), WordList(['a', 'camera', 'travelling']), WordList(['camera', 'travelling', 'at']), WordList(['travelling', 'at', 'a']), WordList(['at', 'a', 'fifth']), WordList(['a', 'fifth', 'of']), WordList(['fifth', 'of', 'light']), WordList(['of', 'light', 'speed']), WordList(['light', 'speed', 'will']), WordList(['speed', 'will', 'be']), WordList(['will', 'be', 'slightly']), WordList(['be', 'slightly', 'distorted']), WordList(['slightly', 'distorted', 'due']), WordList(['distorted', 'due', 'to']), WordList(['due', 'to', 'the']), WordList(['to', 'the', 'effects']), WordList(['the', 'effects', 'of']), WordList(['effects', 'of', 'special']), WordList(['of', 'special', 'relativity']), WordList(['special', 'relativity', 'It']), WordList(['relativity', 'It', 'would']), WordList(['It', 'would', 'be']), WordList(['would', 'be', 'the']), WordList(['be', 'the', 'first']), WordList(['the', 'first', 'time']), WordList(['first', 'time', 'a']), WordList(['time', 'a', 'spacecraft']), WordList(['a', 'spacecraft', 'has']), WordList(['spacecraft', 'has', 'flown']), WordList(['has', 'flown', 'fast']), WordList(['flown', 'fast', 'enough']), WordList(['fast', 'enough', 'to']), WordList(['enough', 'to', 'see']), WordList(['to', 'see', 'such']), WordList(['see', 'such', 'effects']), WordList(['such', 'effects', 'In']), WordList(['effects', 'In', 'fact']), WordList(['In', 'fact', 'Einstein']), WordList(['fact', 'Einstein', '’']), WordList(['Einstein', '’', 's']), WordList(['’', 's', 'theory']), WordList(['s', 'theory', 'is']), WordList(['theory', 'is', 'central']), WordList(['is', 'central', 'to']), WordList(['central', 'to', 'the']), WordList(['to', 'the', 'whole']), WordList(['the', 'whole', 'mission']), WordList(['whole', 'mission', 'Without']), WordList(['mission', 'Without', 'it']), WordList(['Without', 'it', 'we']), WordList(['it', 'we', 'would']), WordList(['we', 'would', 'have']), WordList(['would', 'have', 'neither']), WordList(['have', 'neither', 'lasers']), WordList(['neither', 'lasers', 'nor']), WordList(['lasers', 'nor', 'the']), WordList(['nor', 'the', 'ability']), WordList(['the', 'ability', 'to']), WordList(['ability', 'to', 'perform']), WordList(['to', 'perform', 'the']), WordList(['perform', 'the', 'calculations']), WordList(['the', 'calculations', 'necessary']), WordList(['calculations', 'necessary', 'for']), WordList(['necessary', 'for', 'guidance']), WordList(['for', 'guidance', 'imaging']), WordList(['guidance', 'imaging', 'and']), WordList(['imaging', 'and', 'data']), WordList(['and', 'data', 'transmission']), WordList(['data', 'transmission', 'over']), WordList(['transmission', 'over', 'twenty-five']), WordList(['over', 'twenty-five', 'trillion']), WordList(['twenty-five', 'trillion', 'miles']), WordList(['trillion', 'miles', 'at']), WordList(['miles', 'at', 'a']), WordList(['at', 'a', 'fifth']), WordList(['a', 'fifth', 'of']), WordList(['fifth', 'of', 'light']), WordList(['of', 'light', 'speed']), WordList(['light', 'speed', 'We']), WordList(['speed', 'We', 'can']), WordList(['We', 'can', 'see']), WordList(['can', 'see', 'a']), WordList(['see', 'a', 'pathway']), WordList(['a', 'pathway', 'between']), WordList(['pathway', 'between', 'that']), WordList(['between', 'that', 'sixteen-year-old']), WordList(['that', 'sixteen-year-old', 'boy']), WordList(['sixteen-year-old', 'boy', 'dreaming']), WordList(['boy', 'dreaming', 'of']), WordList(['dreaming', 'of', 'riding']), WordList(['of', 'riding', 'on']), WordList(['riding', 'on', 'a']), WordList(['on', 'a', 'light']), WordList(['a', 'light', 'beam']), WordList(['light', 'beam', 'and']), WordList(['beam', 'and', 'our']), WordList(['and', 'our', 'own']), WordList(['our', 'own', 'dream']), WordList(['own', 'dream', 'which']), WordList(['dream', 'which', 'we']), WordList(['which', 'we', 'are']), WordList(['we', 'are', 'planning']), WordList(['are', 'planning', 'to']), WordList(['planning', 'to', 'turn']), WordList(['to', 'turn', 'into']), WordList(['turn', 'into', 'a']), WordList(['into', 'a', 'reality']), WordList(['a', 'reality', 'of']), WordList(['reality', 'of', 'riding']), WordList(['of', 'riding', 'our']), WordList(['riding', 'our', 'own']), WordList(['our', 'own', 'light']), WordList(['own', 'light', 'beam']), WordList(['light', 'beam', 'to']), WordList(['beam', 'to', 'the']), WordList(['to', 'the', 'stars']), WordList(['the', 'stars', 'We']), WordList(['stars', 'We', 'are']), WordList(['We', 'are', 'standing']), WordList(['are', 'standing', 'at']), WordList(['standing', 'at', 'the']), WordList(['at', 'the', 'threshold']), WordList(['the', 'threshold', 'of']), WordList(['threshold', 'of', 'a']), WordList(['of', 'a', 'new']), WordList(['a', 'new', 'era']), WordList(['new', 'era', 'Human']), WordList(['era', 'Human', 'colonisation']), WordList(['Human', 'colonisation', 'on']), WordList(['colonisation', 'on', 'other']), WordList(['on', 'other', 'planets']), WordList(['other', 'planets', 'is']), WordList(['planets', 'is', 'no']), WordList(['is', 'no', 'longer']), WordList(['no', 'longer', 'science']), WordList(['longer', 'science', 'fiction']), WordList(['science', 'fiction', 'It']), WordList(['fiction', 'It', 'can']), WordList(['It', 'can', 'be']), WordList(['can', 'be', 'science']), WordList(['be', 'science', 'fact']), WordList(['science', 'fact', 'The']), WordList(['fact', 'The', 'human']), WordList(['The', 'human', 'race']), WordList(['human', 'race', 'has']), WordList(['race', 'has', 'existed']), WordList(['has', 'existed', 'as']), WordList(['existed', 'as', 'a']), WordList(['as', 'a', 'separate']), WordList(['a', 'separate', 'species']), WordList(['separate', 'species', 'for']), WordList(['species', 'for', 'about']), WordList(['for', 'about', 'two']), WordList(['about', 'two', 'million']), WordList(['two', 'million', 'years']), WordList(['million', 'years', 'Civilisation']), WordList(['years', 'Civilisation', 'began']), WordList(['Civilisation', 'began', 'about']), WordList(['began', 'about', '10,000']), WordList(['about', '10,000', 'years']), WordList(['10,000', 'years', 'ago']), WordList(['years', 'ago', 'and']), WordList(['ago', 'and', 'the']), WordList(['and', 'the', 'rate']), WordList(['the', 'rate', 'of']), WordList(['rate', 'of', 'development']), WordList(['of', 'development', 'has']), WordList(['development', 'has', 'been']), WordList(['has', 'been', 'steadily']), WordList(['been', 'steadily', 'increasing']), WordList(['steadily', 'increasing', 'If']), WordList(['increasing', 'If', 'humanity']), WordList(['If', 'humanity', 'is']), WordList(['humanity', 'is', 'to']), WordList(['is', 'to', 'continue']), WordList(['to', 'continue', 'for']), WordList(['continue', 'for', 'another']), WordList(['for', 'another', 'million']), WordList(['another', 'million', 'years']), WordList(['million', 'years', 'our']), WordList(['years', 'our', 'future']), WordList(['our', 'future', 'lies']), WordList(['future', 'lies', 'in']), WordList(['lies', 'in', 'boldly']), WordList(['in', 'boldly', 'going']), WordList(['boldly', 'going', 'where']), WordList(['going', 'where', 'no']), WordList(['where', 'no', 'one']), WordList(['no', 'one', 'else']), WordList(['one', 'else', 'has']), WordList(['else', 'has', 'gone']), WordList(['has', 'gone', 'before']), WordList(['gone', 'before', 'I']), WordList(['before', 'I', 'hope']), WordList(['I', 'hope', 'for']), WordList(['hope', 'for', 'the']), WordList(['for', 'the', 'best']), WordList(['the', 'best', 'I']), WordList(['best', 'I', 'have']), WordList(['I', 'have', 'to']), WordList(['have', 'to', 'We']), WordList(['to', 'We', 'have']), WordList(['We', 'have', 'no']), WordList(['have', 'no', 'other']), WordList(['no', 'other', 'option']), WordList(['other', 'option', 'The']), WordList(['option', 'The', 'era']), WordList(['The', 'era', 'of']), WordList(['era', 'of', 'civilian']), WordList(['of', 'civilian', 'space']), WordList(['civilian', 'space', 'travel']), WordList(['space', 'travel', 'is']), WordList(['travel', 'is', 'coming']), WordList(['is', 'coming', 'What']), WordList(['coming', 'What', 'do']), WordList(['What', 'do', 'you']), WordList(['do', 'you', 'think']), WordList(['you', 'think', 'it']), WordList(['think', 'it', 'means']), WordList(['it', 'means', 'to']), WordList(['means', 'to', 'us']), WordList(['to', 'us', 'I']), WordList(['us', 'I', 'look']), WordList(['I', 'look', 'forward']), WordList(['look', 'forward', 'to']), WordList(['forward', 'to', 'space']), WordList(['to', 'space', 'travel']), WordList(['space', 'travel', 'I']), WordList(['travel', 'I', 'would']), WordList(['I', 'would', 'be']), WordList(['would', 'be', 'one']), WordList(['be', 'one', 'of']), WordList(['one', 'of', 'the']), WordList(['of', 'the', 'first']), WordList(['the', 'first', 'to']), WordList(['first', 'to', 'buy']), WordList(['to', 'buy', 'a']), WordList(['buy', 'a', 'ticket']), WordList(['a', 'ticket', 'I']), WordList(['ticket', 'I', 'expect']), WordList(['I', 'expect', 'that']), WordList(['expect', 'that', 'within']), WordList(['that', 'within', 'the']), WordList(['within', 'the', 'next']), WordList(['the', 'next', 'hundred']), WordList(['next', 'hundred', 'years']), WordList(['hundred', 'years', 'we']), WordList(['years', 'we', 'will']), WordList(['we', 'will', 'be']), WordList(['will', 'be', 'able']), WordList(['be', 'able', 'to']), WordList(['able', 'to', 'travel']), WordList(['to', 'travel', 'anywhere']), WordList(['travel', 'anywhere', 'in']), WordList(['anywhere', 'in', 'the']), WordList(['in', 'the', 'solar']), WordList(['the', 'solar', 'system']), WordList(['solar', 'system', 'except']), WordList(['system', 'except', 'maybe']), WordList(['except', 'maybe', 'the']), WordList(['maybe', 'the', 'outer']), WordList(['the', 'outer', 'planets']), WordList(['outer', 'planets', 'But']), WordList(['planets', 'But', 'travel']), WordList(['But', 'travel', 'to']), WordList(['travel', 'to', 'the']), WordList(['to', 'the', 'stars']), WordList(['the', 'stars', 'will']), WordList(['stars', 'will', 'take']), WordList(['will', 'take', 'a']), WordList(['take', 'a', 'bit']), WordList(['a', 'bit', 'longer']), WordList(['bit', 'longer', 'I']), WordList(['longer', 'I', 'reckon']), WordList(['I', 'reckon', 'in']), WordList(['reckon', 'in', '500']), WordList(['in', '500', 'years']), WordList(['500', 'years', 'we']), WordList(['years', 'we', 'will']), WordList(['we', 'will', 'have']), WordList(['will', 'have', 'visited']), WordList(['have', 'visited', 'some']), WordList(['visited', 'some', 'of']), WordList(['some', 'of', 'the']), WordList(['of', 'the', 'nearby']), WordList(['the', 'nearby', 'stars']), WordList(['nearby', 'stars', 'It']), WordList(['stars', 'It', 'won']), WordList(['It', 'won', '’']), WordList(['won', '’', 't']), WordList(['’', 't', 'be']), WordList(['t', 'be', 'like']), WordList(['be', 'like', 'Star']), WordList(['like', 'Star', 'Trek']), WordList(['Star', 'Trek', 'We']), WordList(['Trek', 'We', 'won']), WordList(['We', 'won', '’']), WordList(['won', '’', 't']), WordList(['’', 't', 'be']), WordList(['t', 'be', 'able']), WordList(['be', 'able', 'to']), WordList(['able', 'to', 'travel']), WordList(['to', 'travel', 'at']), WordList(['travel', 'at', 'warp']), WordList(['at', 'warp', 'speed']), WordList(['warp', 'speed', 'So']), WordList(['speed', 'So', 'a']), WordList(['So', 'a', 'round']), WordList(['a', 'round', 'trip']), WordList(['round', 'trip', 'will']), WordList(['trip', 'will', 'take']), WordList(['will', 'take', 'at']), WordList(['take', 'at', 'least']), WordList(['at', 'least', 'ten']), WordList(['least', 'ten', 'years']), WordList(['ten', 'years', 'and']), WordList(['years', 'and', 'probably']), WordList(['and', 'probably', 'much']), WordList(['probably', 'much', 'longer']), WordList(['much', 'longer', 'From']), WordList(['longer', 'From', 'the']), WordList(['From', 'the', 'book']), WordList(['the', 'book', 'Brief']), WordList(['book', 'Brief', 'Answers']), WordList(['Brief', 'Answers', 'to']), WordList(['Answers', 'to', 'the']), WordList(['to', 'the', 'Big']), WordList(['the', 'Big', 'Questions']), WordList(['Big', 'Questions', '–']), WordList(['Questions', '–', 'Stephen']), WordList(['–', 'Stephen', 'Hawking'])]

.tags (Part-of-speech tags)

.tags property is very useful and it will provide part-of-speech properties.

You can see a full list of part-of-speech tags at this page from University of Pennsylvania.

a = blob.tags
print(a)
[('SHOULD', 'MD'), ('WE', 'NNP'), ('COLONISE', 'NNP'), ('SPACE', 'NNP'), ('In', 'IN'), ('conclusion', 'NN'), ('I', 'PRP'), ('return', 'VBP'), ('to', 'TO'), ('Einstein', 'NNP'), ('If', 'IN'), ('we', 'PRP'), ('find', 'VBP'), ('a', 'DT'), ('planet', 'NN'), ('in', 'IN'), ('the', 'DT'), ('Alpha', 'NNP'), ('Centauri', 'NNP'), ('system', 'NN'), ('its', 'PRP$'), ('image', 'NN'), ('captured', 'VBN'), ('by', 'IN'), ('a', 'DT'), ('camera', 'NN'), ('travelling', 'NN'), ('at', 'IN'), ('a', 'DT'), ('fifth', 'NN'), ('of', 'IN'), ('light', 'JJ'), ('speed', 'NN'), ('will', 'MD'), ('be', 'VB'), ('slightly', 'RB'), ('distorted', 'VBN'), ('due', 'JJ'), ('to', 'TO'), ('the', 'DT'), ('effects', 'NNS'), ('of', 'IN'), ('special', 'JJ'), ('relativity', 'NN'), ('It', 'PRP'), ('would', 'MD'), ('be', 'VB'), ('the', 'DT'), ('first', 'JJ'), ('time', 'NN'), ('a', 'DT'), ('spacecraft', 'NN'), ('has', 'VBZ'), ('flown', 'VBN'), ('fast', 'RB'), ('enough', 'JJ'), ('to', 'TO'), ('see', 'VB'), ('such', 'JJ'), ('effects', 'NNS'), ('In', 'IN'), ('fact', 'NN'), ('Einstein', 'NNP'), ('’', 'NNP'), ('s', 'NN'), ('theory', 'NN'), ('is', 'VBZ'), ('central', 'JJ'), ('to', 'TO'), ('the', 'DT'), ('whole', 'JJ'), ('mission', 'NN'), ('Without', 'IN'), ('it', 'PRP'), ('we', 'PRP'), ('would', 'MD'), ('have', 'VB'), ('neither', 'CC'), ('lasers', 'NNS'), ('nor', 'CC'), ('the', 'DT'), ('ability', 'NN'), ('to', 'TO'), ('perform', 'VB'), ('the', 'DT'), ('calculations', 'NNS'), ('necessary', 'JJ'), ('for', 'IN'), ('guidance', 'NN'), ('imaging', 'VBG'), ('and', 'CC'), ('data', 'NNS'), ('transmission', 'NN'), ('over', 'IN'), ('twenty-five', 'JJ'), ('trillion', 'CD'), ('miles', 'NNS'), ('at', 'IN'), ('a', 'DT'), ('fifth', 'NN'), ('of', 'IN'), ('light', 'JJ'), ('speed', 'NN'), ('We', 'PRP'), ('can', 'MD'), ('see', 'VB'), ('a', 'DT'), ('pathway', 'NN'), ('between', 'IN'), ('that', 'DT'), ('sixteen-year-old', 'JJ'), ('boy', 'NN'), ('dreaming', 'NN'), ('of', 'IN'), ('riding', 'VBG'), ('on', 'IN'), ('a', 'DT'), ('light', 'JJ'), ('beam', 'NN'), ('and', 'CC'), ('our', 'PRP$'), ('own', 'JJ'), ('dream', 'NN'), ('which', 'WDT'), ('we', 'PRP'), ('are', 'VBP'), ('planning', 'VBG'), ('to', 'TO'), ('turn', 'VB'), ('into', 'IN'), ('a', 'DT'), ('reality', 'NN'), ('of', 'IN'), ('riding', 'VBG'), ('our', 'PRP$'), ('own', 'JJ'), ('light', 'NN'), ('beam', 'NN'), ('to', 'TO'), ('the', 'DT'), ('stars', 'NNS'), ('We', 'PRP'), ('are', 'VBP'), ('standing', 'VBG'), ('at', 'IN'), ('the', 'DT'), ('threshold', 'NN'), ('of', 'IN'), ('a', 'DT'), ('new', 'JJ'), ('era', 'NN'), ('Human', 'NNP'), ('colonisation', 'NN'), ('on', 'IN'), ('other', 'JJ'), ('planets', 'NNS'), ('is', 'VBZ'), ('no', 'DT'), ('longer', 'JJR'), ('science', 'NN'), ('fiction', 'NN'), ('It', 'PRP'), ('can', 'MD'), ('be', 'VB'), ('science', 'JJ'), ('fact', 'NN'), ('The', 'DT'), ('human', 'JJ'), ('race', 'NN'), ('has', 'VBZ'), ('existed', 'VBN'), ('as', 'IN'), ('a', 'DT'), ('separate', 'JJ'), ('species', 'NNS'), ('for', 'IN'), ('about', 'IN'), ('two', 'CD'), ('million', 'CD'), ('years', 'NNS'), ('Civilisation', 'NN'), ('began', 'VBD'), ('about', 'IN'), ('10,000', 'CD'), ('years', 'NNS'), ('ago', 'RB'), ('and', 'CC'), ('the', 'DT'), ('rate', 'NN'), ('of', 'IN'), ('development', 'NN'), ('has', 'VBZ'), ('been', 'VBN'), ('steadily', 'RB'), ('increasing', 'VBG'), ('If', 'IN'), ('humanity', 'NN'), ('is', 'VBZ'), ('to', 'TO'), ('continue', 'VB'), ('for', 'IN'), ('another', 'DT'), ('million', 'CD'), ('years', 'NNS'), ('our', 'PRP$'), ('future', 'NN'), ('lies', 'VBZ'), ('in', 'IN'), ('boldly', 'RB'), ('going', 'VBG'), ('where', 'WRB'), ('no', 'DT'), ('one', 'NN'), ('else', 'RB'), ('has', 'VBZ'), ('gone', 'VBN'), ('before', 'RB'), ('I', 'PRP'), ('hope', 'VBP'), ('for', 'IN'), ('the', 'DT'), ('best', 'JJS'), ('I', 'PRP'), ('have', 'VBP'), ('to', 'TO'), ('We', 'PRP'), ('have', 'VBP'), ('no', 'DT'), ('other', 'JJ'), ('option', 'NN'), ('The', 'DT'), ('era', 'NN'), ('of', 'IN'), ('civilian', 'JJ'), ('space', 'NN'), ('travel', 'NN'), ('is', 'VBZ'), ('coming', 'VBG'), ('What', 'WP'), ('do', 'VBP'), ('you', 'PRP'), ('think', 'VB'), ('it', 'PRP'), ('means', 'VBZ'), ('to', 'TO'), ('us', 'PRP'), ('I', 'PRP'), ('look', 'VBP'), ('forward', 'RB'), ('to', 'TO'), ('space', 'NN'), ('travel', 'NN'), ('I', 'PRP'), ('would', 'MD'), ('be', 'VB'), ('one', 'CD'), ('of', 'IN'), ('the', 'DT'), ('first', 'JJ'), ('to', 'TO'), ('buy', 'VB'), ('a', 'DT'), ('ticket', 'NN'), ('I', 'PRP'), ('expect', 'VBP'), ('that', 'IN'), ('within', 'IN'), ('the', 'DT'), ('next', 'JJ'), ('hundred', 'CD'), ('years', 'NNS'), ('we', 'PRP'), ('will', 'MD'), ('be', 'VB'), ('able', 'JJ'), ('to', 'TO'), ('travel', 'VB'), ('anywhere', 'RB'), ('in', 'IN'), ('the', 'DT'), ('solar', 'JJ'), ('system', 'NN'), ('except', 'IN'), ('maybe', 'RB'), ('the', 'DT'), ('outer', 'NN'), ('planets', 'NNS'), ('But', 'CC'), ('travel', 'NN'), ('to', 'TO'), ('the', 'DT'), ('stars', 'NNS'), ('will', 'MD'), ('take', 'VB'), ('a', 'DT'), ('bit', 'NN'), ('longer', 'RBR'), ('I', 'PRP'), ('reckon', 'VBP'), ('in', 'IN'), ('500', 'CD'), ('years', 'NNS'), ('we', 'PRP'), ('will', 'MD'), ('have', 'VB'), ('visited', 'VBN'), ('some', 'DT'), ('of', 'IN'), ('the', 'DT'), ('nearby', 'JJ'), ('stars', 'NNS'), ('It', 'PRP'), ('won', 'VBD'), ('’', 'NNP'), ('t', 'RB'), ('be', 'VB'), ('like', 'IN'), ('Star', 'NNP'), ('Trek', 'NNP'), ('We', 'PRP'), ('won', 'VBD'), ('’', 'NNP'), ('t', 'NN'), ('be', 'VB'), ('able', 'JJ'), ('to', 'TO'), ('travel', 'VB'), ('at', 'IN'), ('warp', 'JJ'), ('speed', 'NN'), ('So', 'RB'), ('a', 'DT'), ('round', 'NN'), ('trip', 'NN'), ('will', 'MD'), ('take', 'VB'), ('at', 'IN'), ('least', 'JJS'), ('ten', 'CD'), ('years', 'NNS'), ('and', 'CC'), ('probably', 'RB'), ('much', 'RB'), ('longer', 'RBR'), ('From', 'IN'), ('the', 'DT'), ('book', 'NN'), ('Brief', 'JJ'), ('Answers', 'NNS'), ('to', 'TO'), ('the', 'DT'), ('Big', 'NNP'), ('Questions', 'NNP'), ('–', 'NNP'), ('Stephen', 'NNP'), ('Hawking', 'NNP')]

.definitions (list of definitions)

.definitions property can be applied to words and it will return the definition of the word. But, first you’ll need to import the Word module from textblob.

from textblob import Word
a = Word("prolific")
print(a.definitions)
['intellectually productive', 'bearing in abundance especially offspring']

.spellcheck (Spell check with confidence level)

.spellcheck will return the correct spelling of a word with confidence value.

from textblob import Word
a = Word("profitible")
print(a.spellcheck)
[('profitable', 1.0)]

.correct (Autocorrection)

.correct will check a word and correct it if it’s written wrong with the highest confidence word option.

from textblob import Word
a = Word("profitible")
print(a.correct)
profitable

.word_count(["word"]) (frequency of a word)

.word_count() will return the frequency of a word.

print(blob.word_counts["space"])
3

Polarity & Subjectivity

Sentiment analysis can be carried out with these properties of textblob:

.sentiment (sentiment analysis)

.sentiment will return 2 values in a tuple:

Polarity: Takes a value between -1 and +1. -1 suggests a very negative language and +1 suggests a very positive language. This part of the analysis is the heart of sentiment analysis and can be supported, advanced or elaborated further.

Subjectivity: Takes a value between 0 and +1. 0 suggests maximum objectivity possible and +1 suggests a very subjective text.

a = blob.sentiment
    
print(a)
Sentiment(polarity=0.1725525664811379, subjectivity=0.44312306740878166)

.sentiment.subjectivity (only subjectivity)

a = blob.sentiment.subjectivity
    
print(a)
0.44312306740878166

.sentiment.polarity (only polarity)

a = blob.sentiment.polarity
    
print(a)
0.1725525664811379