Building visualizations with Tweet counts from the Twitter API v2 in Python
Tags: twitter, python, tutorial
Published: 2021-07-08 23:27:17

In this post, we will learn how to build visualizations using the [Tweet counts](https://developer.twitter.com/en/docs/twitter-api/tweets/counts/introduction) 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](https://developer.twitter.com/en/docs/twitter-api/tweets/counts/quick-start/recent-tweet-counts), any developer can get the counts (volume) of Tweets from the last 7 days

* Using [full-archive Tweet counts](https://developer.twitter.com/en/docs/twitter-api/tweets/counts/quick-start/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](https://github.com/twitterdev/getting-started-with-the-twitter-api-v2-for-academic-research/blob/main/modules/4-getting-your-keys-and-token.md) for obtaining a bearer token.

For this demo, we will be using the [Twarc](https://twarc-project.readthedocs.io/en/latest/twarc2/) library in Python to connect to the Twitter API v2 and the [plotly](https://plotly.com/python/) library for building visualizations.

To install plotly, run the following in your terminal

```bash
pip3 install plotly
```

To install twarc, run the following in your terminal

```bash
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.

```python
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

```python
# 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](https://developer-staging.twitter.com/en/docs/twitter-api/tweets/counts/integrate/build-a-query)

```python
# 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

```python
# 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:

```json
    [
      {
        "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

```python
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)

```python
# 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

```python
# 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

```python
fig.show()
```

to see the visualization, which will look like:

![Bar Graph](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/9tnyinuz7rf2x96ng2o0.png)

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.

```python
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](https://twitter.com/suhemparack)