Now with prettier charts

This commit is contained in:
rnsrk 2023-01-03 22:21:46 +01:00
parent 0b13673bcc
commit f0d4eadf28
2 changed files with 50 additions and 11 deletions

3
.gitignore vendored
View file

@ -3,4 +3,5 @@ plots
instance instance
__pycache__ __pycache__
hedonodon_clientcred.secret hedonodon_clientcred.secret
hedonodon_usercred.secret hedonodon_usercred.secret
.fleet

58
Main.py
View file

@ -1,13 +1,15 @@
from CRUDManager import CRUDManager from CRUDManager import CRUDManager
from datetime import datetime from datetime import datetime, date
from DbSetup import init_db from DbSetup import init_db
import locale import locale
from MastodonAccountManager import MastodonAccountManager from MastodonAccountManager import MastodonAccountManager
import matplotlib.pyplot as plt import matplotlib.pyplot as plt
import matplotlib.dates as mdates
from matplotlib.ticker import MultipleLocator
import numpy as np
from TootCrawler import TootCrawler from TootCrawler import TootCrawler
from sqlalchemy.sql import desc, select
locale.setlocale(locale.LC_TIME, "de_DE.UTF-8") locale.setlocale(locale.LC_TIME, "en_EN.UTF-8")
init_db() init_db()
mastodonAccountManager = MastodonAccountManager() mastodonAccountManager = MastodonAccountManager()
@ -26,13 +28,22 @@ crudManager = CRUDManager()
lastTootId = crudManager.getLastToot() lastTootId = crudManager.getLastToot()
tootsDataframe = tootCrawler.buildTootsDataframe(lastTootId) tootsDataframe = tootCrawler.buildTootsDataframe(lastTootId)
sentimentsYesterday = crudManager.calculateAggregates('sentiment', 'Count') sentimentsYesterday = crudManager.calculateAggregates('sentiment', 'Count')
colormap = {
'negative"': '#ff9999',
'neutral': '#ffcc99',
"positive": '#99ff99'
}
todaysColors = []
for sentiment in sentimentsYesterday['sentiment'].to_numpy():
todaysColors.append(colormap[sentiment])
compoundsYesterday = crudManager.calculateAggregates('compound', 'Avg') compoundsYesterday = crudManager.calculateAggregates('compound', 'Avg')
if not tootsDataframe.empty: if not tootsDataframe.empty:
crudManager.saveToDatabase(tootsDataframe, 'Toots', useIndex=False) crudManager.saveToDatabase(tootsDataframe, 'Toots', useIndex=False)
crudManager.saveToDatabase(dataframe=sentimentsYesterday, table='Sentiments', useIndex=True) crudManager.saveToDatabase(dataframe=sentimentsYesterday, table='Sentiments', useIndex=True)
crudManager.saveToDatabase(dataframe=compoundsYesterday, table='Compounds', useIndex=True) crudManager.saveToDatabase(dataframe=compoundsYesterday, table='Compounds', useIndex=True)
#print(sentimentsYesterday, 'sentimentsYesterday')
#print(compoundsYesterday, 'sentimentsYesterday')
else: else:
print('Nothing changed since last database insert!') print('Nothing changed since last database insert!')
@ -42,16 +53,43 @@ dataframe4LineChart = crudManager.loadFromDatabase('Compounds', 'date').drop('in
fig, axes = plt.subplots(nrows=2, ncols=1, figsize=(10,10)) fig, axes = plt.subplots(nrows=2, ncols=1, figsize=(10,10))
# Pie chart.
pieChartlabels = dataframe4PieChart.index.to_numpy() pieChartlabels = dataframe4PieChart.index.to_numpy()
pieChart = dataframe4PieChart.plot.pie(ax=axes[0], y='sentimentCount', ylabel="", labels=dataframe4PieChart['sentimentCount'], title=f'Moods of the toots on {TodayDate} of the local timeline on fedihum.org', colors = ['red', 'grey', 'green']) pieChart = dataframe4PieChart.plot.pie(
ax=axes[0],
y='sentimentCount',
ylabel="",
labels=dataframe4PieChart['sentimentCount'],
title=f'Moods of the toots on {TodayDate} of the local timeline on fedihum.org',
colors = todaysColors,
wedgeprops=dict(linewidth=3, edgecolor='w'),
startangle=90
)
axes[0].axis('equal')
centre_circle = plt.Circle((0,0),0.6,fc='white')
axes[0].add_patch(centre_circle)
chartBox = axes[0].get_position() chartBox = axes[0].get_position()
axes[0].set_position([chartBox.x0,chartBox.y0-0.2,chartBox.width,chartBox.height]) axes[0].set_position([chartBox.x0,chartBox.y0-0.2,chartBox.width,chartBox.height])
axes[0].legend(pieChartlabels,loc='upper right', bbox_to_anchor=(1.3, 0.9)) axes[0].legend(pieChartlabels,loc='upper right', bbox_to_anchor=(0.8, 0.9))
lineChart = dataframe4LineChart.plot.line(ax=axes[1], title='Compounds from max positive (1) to min neg (-1)')
axes[1].set_ylim([-1, 1])
# Line chart.
lineChart = dataframe4LineChart.plot.line(
ax=axes[1],
title='Compounds from max positive (1) to min negative (-1)'
)
axes[1].grid(True)
axes[1].set_xlim([date(2023, 1, 1), date(2023, 12, 31)])
axes[1].set_ylim([-1, 1])
axes[1].xaxis.set_major_locator(mdates.MonthLocator())
axes[1].xaxis.set_minor_locator(mdates.MonthLocator(bymonthday=15))
axes[1].xaxis.set_major_formatter(plt.NullFormatter())
axes[1].xaxis.set_minor_formatter(mdates.DateFormatter('%h'))
axes[1].tick_params(which='minor', length=0)
plotFileUrl = f'./plots/{TodayDate}.png' plotFileUrl = f'./plots/{TodayDate}.png'
plt.show()
plt.savefig(plotFileUrl) plt.savefig(plotFileUrl)
media = mastodonInstance.media_post(plotFileUrl, mime_type="image/png", description=f"Sentiment analysis of local timeline on fedihum.org, showing the moods of the toots on, and the compounds up to {TodayDate}.") media = mastodonInstance.media_post(plotFileUrl, mime_type="image/png", description=f"Sentiment analysis of local timeline on fedihum.org, showing the moods of the toots on, and the compounds up to {TodayDate}.")
mastodonInstance.status_post(f'The moods of the toots on and up to {TodayDate}.', media_ids=media, language='en') mastodonInstance.status_post(f'The moods of the toots on and up to {TodayDate}.', media_ids=media, language='en')