A Look at Trump and Clinton’s Tweets Using Tweepy – Part 3: Geo-Stalking Donald Trump

This post is part of a series:
Part 1 can be found here.
Part 2 can be found here.

Status.Place

As part of the Twitter API each status has a “place” object containing the country, city, name, and coordinates of a given location. I figured this could be used to discover where the candidates had been campaigning. Unfortunately, Clinton doesn’t attach any locations to her tweets. But Trump often does. I extracted the coordinate locations of his last 1500 tweets using the following script (with preliminary Tweepy work included in Post 1):

userID = 'realDonaldTrump'
followers = api.get_user(id=userID).followers_count
setsize = 1500
statuses = []
for status in tweepy.Cursor(api.user_timeline, id = userID).items(setsize):
    statuses.append(status)

left_pts = []
right_pts = []
center_pts = []
for status in statuses:
    if status.place != None:
        left_pts.append(status.place.bounding_box.coordinates[0][0])
        right_pts.append(status.place.bounding_box.coordinates[0][2])

Twitter doesn’t give a set of exact coordinates for each tweet (they might have this information, but it’s probably for the best that they don’t reveal it) but instead provides a bounding-box range of 4 points. Each tweet originated within the square range given. To give a single point of origination, I took the midpoint between two opposing vertices of the square. You can see me extract those as “left_pts” and “right_pts” in the script above. This code gives me the desired center_pts:

for i in range(0, len(left_pts)):
    center_pts.append([(left_pts[i][0] + right_pts[i][0])/2 , (left_pts[i][1] + right_pts[i][1])/2])

Of the 1500 tweets taken, only 111 of them had locations attached (dating back to 4/19/16). It’s likely that Trump only attaches a location when he wants to show his followers where he is. It might be more accurate to call this an analysis of where Trump wants us to know he’s been.

The next step is to convert this list into a data frame:

import pandas as pd
coord_frame = pd.DataFrame(center_pts)
coord_frame.columns = ['Long', 'Lat']

Mapping Coordinates

At this stage we’re ready to impose the scatterplot of these coordinates over a map of the United States. BeyondBeneath’s kaggle notebook on Geolocation visualization goes more in-depth on how to use Basemap to do so. For the code below, my lon/lat min/max variables create a box around the United States, and the scatter function plots our coordinates. Points on the scatterplot are more intense if Trump has tweeted from there multiple times. Let’s take a look:

import os
import matplotlib.pyplot as plt
import seaborn as sns
from mpl_toolkits.basemap import Basemap
from matplotlib.patches import Polygon
%matplotlib inline

lon_min, lon_max = -130,-55
lat_min, lat_max = 20,50

plt.figure(2, figsize=(12,6))

m = Basemap(projection='merc',
             llcrnrlat=lat_min,
             urcrnrlat=lat_max,
             llcrnrlon=lon_min,
             urcrnrlon=lon_max,
             lat_ts=35,
             resolution='i')

m.fillcontinents(color='#bfbfbf')
m.drawcountries(linewidth=0.2, color='w')
m.drawstates(linewidth=0.2, color='w')              

# Plot the data
mxy = m(coord_frame['Long'].tolist(), coord_frame['Lat'].tolist())
m.scatter(mxy[0], mxy[1], c='#ff3333', lw=0, alpha=0.5, zorder=10)

plt.show()

Trump Map 1.png

I was curious to see if Trump favors swing states (he does) and decided to add them to the map as well. According to Politico, the 11 swing states for this election are: Colorado, Florida, Iowa, Michigan, Nevada, New Hampshire, North Carolina, Ohio, Pennsylvania, Virginia and Wisconsin.

Basemap does not make it easy to color states separately. You’ll see in the following code segment that I’ve used the m.readshapefile() function. I pulled data from the Census Bureau to find the shape of each state. The lines for Michigan are unfortunately blotchy, as portions of the Great Lakes were included under what the bureau called “Michigan”.

lon_min, lon_max = -130,-55
lat_min, lat_max = 20,50

plt.figure(2, figsize=(12,6))

m = Basemap(projection='merc',
             llcrnrlat=lat_min,
             urcrnrlat=lat_max,
             llcrnrlon=lon_min,
             urcrnrlon=lon_max,
             lat_ts=35,
             resolution='i')

m.fillcontinents(color='#bfbfbf')
m.drawcountries(linewidth=0.2, color='w')
m.drawstates(linewidth=0.2, color='w')              

m.readshapefile('/Users/keithselover/Downloads/tl_2014_us_state/tl_2014_us_state', name='states', drawbounds=False)
ax = plt.gca()

state_names = []
for shape_dict in m.states_info:
    state_names.append(shape_dict['NAME'])

for st in ['Ohio', 'Florida','Nevada','Colorado','North Carolina','Virginia','Michigan','Pennsylvania','Iowa','New Hampshire','Wisconsin']:
    seg = m.states[state_names.index(st)]
    poly = Polygon(seg, facecolor='#a6a6a6',edgecolor='white')
    ax.add_patch(poly)

# Plot the data
mxy = m(coord_frame['Long'].tolist(), coord_frame['Lat'].tolist())
m.scatter(mxy[0], mxy[1], c='#ff3333', lw=0, alpha=0.5, zorder=10)
font = {'family' : 'serif',
'style' : 'italic',
'weight' : 'normal',
'size' : '8'}

matplotlib.rc('font', **font) # pass in the font dict as kwargs

plt.annotate('Filled in states indicate Swing States', xy=(.76, .02), xycoords='axes fraction')
plt.title('Where in the World is Donald Trump?')
plt.show()

Labelled Trump Map.png

Trump’s tweets do predominantly come from swing states. Some interesting exceptions are New York and Texas, the former being the headquarters of Trump’s campaign, and the later being a significant stalwart conservative state.

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s