---
author: tam kien duong
output-file: twitter-noise.html
execute:
echo: false
jupyter:
jupytext:
formats: 'ipynb,qmd:quarto'
text_representation:
extension: .qmd
format_name: quarto
format_version: '1.0'
jupytext_version: 1.14.1
kernelspec:
display_name: Python 3 (ipykernel)
language: python
name: python3
---
# Twitter : bruit ou information ?
{{< include /_includes/draft.qmd >}}
```{python}
import pandas as pd
import matplotlib.pyplot as plt
import matplotlib as mpt
import numpy as np
```
```{python}
tweets = (
pd
.read_parquet('datasets/tweets.par' )
)
```
```{python}
figsize = (15 ,10 )
```
```{python}
def timeline(df, ax):
scatter = ax.scatter(
x= df.local_time.dt.date,
y= df.local_time.dt.hour* 60 + df.local_time.dt.minute,
s= np.interp(df.retweet_count, (0 , tweets.retweet_count.max ()), (5 , 1000 )),
#label=df.user_screen_name.tolist(),
alpha= 0.5 ,
c= df.user_screen_name.cat.codes,
cmap= 'tab10'
)
plt.ylabel('heure' )
ax.set_ylim(0 , 24 * 60 )
legend1 = ax.legend(scatter.legend_elements()[0 ], df.user_screen_name.unique(), loc= "lower left" , title= "comptes twitter" )
ax.add_artist(legend1)
handles, labels = scatter.legend_elements(prop= "sizes" , alpha= 0.6 )
#legend2 = ax.legend(handles, labels, loc="lower right", title="retweets")
return scatter
def show_timeline(account):
fig, ax = plt.subplots()
fig.set_size_inches(* figsize)
#fig.set_dpi(600)
timeline(tweets.query(f'user_screen_name.isin(@account)' ), ax)
ax.yaxis.set_major_locator(mpt.ticker.MultipleLocator(4 * 60 ))
ax.yaxis.set_major_formatter(lambda x,pos: ' {:0.0f} ' .format (x/ 60 ))
ax.yaxis.set_minor_locator(mpt.ticker.MultipleLocator(60 ))
plt.grid(axis= 'y' )
plt.tight_layout()
plt.show()
```
## Vue d'ensemble
```{python}
#| layout-ncol: 1
fig, ax = plt.subplots()
fig.set_size_inches(* figsize)
timeline(tweets, ax)
ax.yaxis.set_major_locator(mpt.ticker.MultipleLocator(4 * 60 ))
ax.yaxis.set_major_formatter(lambda x,pos: ' {:0.0f} ' .format (x/ 60 ))
ax.yaxis.set_minor_locator(mpt.ticker.MultipleLocator(60 ))
plt.grid(axis= 'y' )
plt.tight_layout()
plt.savefig('graphics/timeline-raw.png' );
plt.show()
```
## Le Monde
```{python}
#| layout-ncol: 1
show_timeline(['lemondefr' ])
```
## Mediapart
```{python}
#| layout-ncol: 1
show_timeline(['Mediapart' ])
```
```{python}
#| layout-ncol: 1
show_timeline(['MediapartBlogs' ])
```
## Brut et Loopsider
```{python}
#| layout-ncol: 2
#| fig-cap:
#| - Brut
#| - Loopsider
show_timeline(['brutofficiel' ])
show_timeline(['Loopsidernews' ])
```
{{< include /_includes/checkmeout.qmd >}}