Building visualizations with Tweet counts from the Twitter API v2 in Python

In this post, we will learn how to build visualizations using the Tweet counts functionality in the Twitter API v2

Tweet counts gives you the volume of Tweets that matches a Twitter search query. So if you are interested in seeing how a search term is trending in terms of Tweet volume over a period of time, you can get this information using the Twitter API v2.

Currently, the Twitter API v2 supports this functionality in 2 ways:

  • Using recent Tweet counts, any developer can get the counts (volume) of Tweets from the last 7 days

  • Using full-archive Tweet counts, academic researchers can get the counts of Tweets from the full-archive of public Tweets

In order to follow this tutorial, you need an approved developer account. Once you have a developer account, you will need a bearer token to connect to the Twitter API v2 to get the Tweet counts for a search term. Follow these instructions for obtaining a bearer token.

For this demo, we will be using the Twarc library in Python to connect to the Twitter API v2 and the plotly library for building visualizations.

To install plotly, run the following in your terminal

pip3 install plotly

To install twarc, run the following in your terminal

pip3 install twarc

Once you have these libraries installed, import them. To display the JSON response from the Twitter API for Tweet counts, we will be using the json library.

import plotly.graph_objects as go
from twarc import Twarc2
import json

Next, setup the Twarc client (that you will use to get Tweet counts from the Twitter API v2) with your bearer token

# Replace with your own bearer token
client = Twarc2(
    bearer_token="REPLACE_ME")

Then, specify the query that you want Tweet counts for. Learn more about building queries here

# Replace the query below with your own query
query = "#TwitterAPI"

Next, use the counts_recent method in twarc to get the Tweet counts for the search query for the last 7 days. The default aggregation of the data is at an hourly level. If you want data aggregated at day-level, specify that using the granualrity parameter

# Get the recent Tweet counts using Twarc for your query by day
search_results = client.counts_recent(query=query, granularity='day')

for page in search_results:
    # Get the data object from the Tweet counts response which contains the daily Tweet count
    data = page['data']
    break

print(json.dumps(data, indent=2))

The response will look like:

[
      {
        "end": "2021-07-02T00:00:00.000Z",
        "start": "2021-07-01T22:57:15.000Z",
        "tweet_count": 1
      },
      {
        "end": "2021-07-03T00:00:00.000Z",
        "start": "2021-07-02T00:00:00.000Z",
        "tweet_count": 36
      },
      {
        "end": "2021-07-04T00:00:00.000Z",
        "start": "2021-07-03T00:00:00.000Z",
        "tweet_count": 9
      },
      {
        "end": "2021-07-05T00:00:00.000Z",
        "start": "2021-07-04T00:00:00.000Z",
        "tweet_count": 4
      },
      {
        "end": "2021-07-06T00:00:00.000Z",
        "start": "2021-07-05T00:00:00.000Z",
        "tweet_count": 5
      },
      {
        "end": "2021-07-07T00:00:00.000Z",
        "start": "2021-07-06T00:00:00.000Z",
        "tweet_count": 12
      },
      {
        "end": "2021-07-08T00:00:00.000Z",
        "start": "2021-07-07T00:00:00.000Z",
        "tweet_count": 7
      },
      {
        "end": "2021-07-08T22:57:15.000Z",
        "start": "2021-07-08T00:00:00.000Z",
        "tweet_count": 10
      }
    ]

To display the Tweet counts in a bar graph, we will get the start date in a list called day to display on the x-axis and the corresponding tweet_counts in a list called tweet_counts to display on the y-axis

day = []
tweet_counts = []

for d in data:
    # Add the start date to display on x-axis
    day.append(d['start'][:10])
    # Add the daily Tweet counts to display on the y-axis
    tweet_counts.append(d['tweet_count'])

Next, we will create a Figure to display the bar chart (using the graph_objects in plotly)

# Build a bar chart
fig = go.Figure(data=[go.Bar(x=day, y=tweet_counts)])

Then, we will add appropriate titles for the x and y axes

# Add the titles
fig.update_layout(xaxis_title="Time Period", yaxis_title="Tweet Counts",
                  title_text='Tweets by day for {}'.format(query))

Note: When running the code on your local machine (outside of this notebook), you will also have to write

fig.show()

to see the visualization, which will look like:

Below is the complete code sample that you can plug and play on your local machine. Just add your bearer token and make sure you have the libraries installed.

import plotly.graph_objects as go
from twarc import Twarc2
import json

# Replace with your own bearer token
client = Twarc2(bearer_token="XXXXX")

# Replace the query below with your own query
query = "#TwitterAPI"

# Get the recent Tweet counts using Twarc for your query by day
search_results = client.counts_recent(query=query, granularity='day')

for page in search_results:
    # Get the data object from the Tweet counts response which contains the daily Tweet count
    data = page['data']
    break
print(json.dumps(data, indent=2))

day = []
tweet_counts = []

for d in data:
    # Add the start date to display on x-axis
    day.append(d['start'][:10])
    # Add the daily Tweet counts to display on the y-axis
    tweet_counts.append(d['tweet_count'])

# Build a bar chart
fig = go.Figure(data=[go.Bar(x=day, y=tweet_counts)])

# Add the titles
fig.update_layout(xaxis_title="Time Period", yaxis_title="Tweet Counts",
                  title_text='Tweets by day for {}'.format(query))

fig.show()

In addition to bar charts, you can also build line charts etc. using Plotly. Try different queries to see the different results.

Got feedback? Reach out on Twitter @suhemparack

18