Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import psycopg2
- connection = psycopg2.connect("host=localhost port=5432 dbname=postgres user=kimishi password=********")
- connection.get_backend_pid()
- import pandas as pd
- import numpy as np
- import matplotlib.pyplot as plt
- import seaborn as sns
- import math
- from matplotlib.patches import Ellipse
- from scipy import stats
- df_pre = pd.read_sql(sql="SELECT t1.*,t2.artist_genres FROM tracks as t1 left join artists as t2 on t1.artist_id=t2.artist_id where valence>=0.5 and energy>=0.5;", con=connection, index_col='index' )
- df = df_pre[df_pre['artist_genres'].str.contains('pop')]
- def corrfunc(x, y, **kws):
- r, _ = stats.spearmanr(x, y)
- ax = plt.gca()
- ax.axis('off')
- ellcolor = plt.cm.RdBu(0.5*(r+1))
- txtcolor = 'black' if math.fabs(r) < 0.5 else 'white'
- ax.add_artist(Ellipse(xy=[.5, .5], width=math.sqrt(1+r), height=math.sqrt(1-r), angle=45,
- facecolor=ellcolor, edgecolor='none', transform=ax.transAxes))
- ax.text(.5, .5, '{:.0f}'.format(r*100), color=txtcolor, fontsize=28,
- horizontalalignment='center', verticalalignment='center', transform=ax.transAxes)
- d2 = pd.DataFrame({'valence':df['valence'], 'energy':df['energy'], 'acousticness':df['acousticness'], 'danceability':df['danceability'],
- 'instrumentalness':df['instrumentalness'], 'liveness':df['liveness'], 'loudness':df['loudness'] , 'speechiness':df['speechiness'] , 'tempo':df['tempo'] , 'popularity':df['popularity']},
- columns=['valence', 'energy', 'acousticness', 'danceability', 'instrumentalness', 'liveness', 'loudness','speechiness','tempo','popularity'])
- sns.set(font_scale=2)
- g = sns.PairGrid(d2)
- g = g.map_lower(sns.kdeplot, cmap='Blues_d')
- g = g.map_diag(sns.distplot, kde=False)
- g = g.map_upper(corrfunc)
- g.fig.subplots_adjust(wspace=0.05, hspace=0.05)
- for ax in g.axes.flatten():
- for t in ax.get_xticklabels():
- _ = t.set(rotation=40)
- g.savefig('/Users/kimishi/Desktop/fig1_1_1.png')
- df_pre = pd.read_sql(sql="SELECT t1.*,t2.artist_genres FROM tracks as t1 left join artists as t2 on t1.artist_id=t2.artist_id where valence<0.5 and energy>=0.5;", con=connection, index_col='index' )
- df = df_pre[df_pre['artist_genres'].str.contains('pop')]
- def corrfunc(x, y, **kws):
- r, _ = stats.spearmanr(x, y)
- ax = plt.gca()
- ax.axis('off')
- ellcolor = plt.cm.RdBu(0.5*(r+1))
- txtcolor = 'black' if math.fabs(r) < 0.5 else 'white'
- ax.add_artist(Ellipse(xy=[.5, .5], width=math.sqrt(1+r), height=math.sqrt(1-r), angle=45,
- facecolor=ellcolor, edgecolor='none', transform=ax.transAxes))
- ax.text(.5, .5, '{:.0f}'.format(r*100), color=txtcolor, fontsize=28,
- horizontalalignment='center', verticalalignment='center', transform=ax.transAxes)
- d3 = pd.DataFrame({'valence':df['valence'], 'energy':df['energy'], 'acousticness':df['acousticness'], 'danceability':df['danceability'],
- 'instrumentalness':df['instrumentalness'], 'liveness':df['liveness'], 'loudness':df['loudness'] , 'speechiness':df['speechiness'] , 'tempo':df['tempo'] , 'popularity':df['popularity']},
- columns=['valence', 'energy', 'acousticness', 'danceability', 'instrumentalness', 'liveness', 'loudness','speechiness','tempo','popularity'])
- sns.set(font_scale=2)
- g = sns.PairGrid(d3)
- g = g.map_lower(sns.kdeplot, cmap='Blues_d')
- g = g.map_diag(sns.distplot, kde=False)
- g = g.map_upper(corrfunc)
- g.fig.subplots_adjust(wspace=0.05, hspace=0.05)
- for ax in g.axes.flatten():
- for t in ax.get_xticklabels():
- _ = t.set(rotation=40)
- g.savefig('/Users/kimishi/Desktop/fig1_1_2.png')
- df_pre = pd.read_sql(sql="SELECT t1.*,t2.artist_genres FROM tracks as t1 left join artists as t2 on t1.artist_id=t2.artist_id where valence<0.5 and energy<0.5;", con=connection, index_col='index' )
- df = df_pre[df_pre['artist_genres'].str.contains('pop')]
- def corrfunc(x, y, **kws):
- r, _ = stats.spearmanr(x, y)
- ax = plt.gca()
- ax.axis('off')
- ellcolor = plt.cm.RdBu(0.5*(r+1))
- txtcolor = 'black' if math.fabs(r) < 0.5 else 'white'
- ax.add_artist(Ellipse(xy=[.5, .5], width=math.sqrt(1+r), height=math.sqrt(1-r), angle=45,
- facecolor=ellcolor, edgecolor='none', transform=ax.transAxes))
- ax.text(.5, .5, '{:.0f}'.format(r*100), color=txtcolor, fontsize=28,
- horizontalalignment='center', verticalalignment='center', transform=ax.transAxes)
- d4 = pd.DataFrame({'valence':df['valence'], 'energy':df['energy'], 'acousticness':df['acousticness'], 'danceability':df['danceability'],
- 'instrumentalness':df['instrumentalness'], 'liveness':df['liveness'], 'loudness':df['loudness'] , 'speechiness':df['speechiness'] , 'tempo':df['tempo'] , 'popularity':df['popularity']},
- columns=['valence', 'energy', 'acousticness', 'danceability', 'instrumentalness', 'liveness', 'loudness','speechiness','tempo','popularity'])
- sns.set(font_scale=2)
- g = sns.PairGrid(d4)
- g = g.map_lower(sns.kdeplot, cmap='Blues_d')
- g = g.map_diag(sns.distplot, kde=False)
- g = g.map_upper(corrfunc)
- g.fig.subplots_adjust(wspace=0.05, hspace=0.05)
- for ax in g.axes.flatten():
- for t in ax.get_xticklabels():
- _ = t.set(rotation=40)
- g.savefig('/Users/kimishi/Desktop/fig1_1_3.png')
- df_pre = pd.read_sql(sql="SELECT t1.*,t2.artist_genres FROM tracks as t1 left join artists as t2 on t1.artist_id=t2.artist_id where valence>=0.5 and energy<0.5;", con=connection, index_col='index' )
- df = df_pre[df_pre['artist_genres'].str.contains('pop')]
- def corrfunc(x, y, **kws):
- r, _ = stats.spearmanr(x, y)
- ax = plt.gca()
- ax.axis('off')
- ellcolor = plt.cm.RdBu(0.5*(r+1))
- txtcolor = 'black' if math.fabs(r) < 0.5 else 'white'
- ax.add_artist(Ellipse(xy=[.5, .5], width=math.sqrt(1+r), height=math.sqrt(1-r), angle=45,
- facecolor=ellcolor, edgecolor='none', transform=ax.transAxes))
- ax.text(.5, .5, '{:.0f}'.format(r*100), color=txtcolor, fontsize=28,
- horizontalalignment='center', verticalalignment='center', transform=ax.transAxes)
- d5 = pd.DataFrame({'valence':df['valence'], 'energy':df['energy'], 'acousticness':df['acousticness'], 'danceability':df['danceability'],
- 'instrumentalness':df['instrumentalness'], 'liveness':df['liveness'], 'loudness':df['loudness'] , 'speechiness':df['speechiness'] , 'tempo':df['tempo'] , 'popularity':df['popularity']},
- columns=['valence', 'energy', 'acousticness', 'danceability', 'instrumentalness', 'liveness', 'loudness','speechiness','tempo','popularity'])
- sns.set(font_scale=2)
- g = sns.PairGrid(d5)
- g = g.map_lower(sns.kdeplot, cmap='Blues_d')
- g = g.map_diag(sns.distplot, kde=False)
- g = g.map_upper(corrfunc)
- g.fig.subplots_adjust(wspace=0.05, hspace=0.05)
- for ax in g.axes.flatten():
- for t in ax.get_xticklabels():
- _ = t.set(rotation=40)
- g.savefig('/Users/kimishi/Desktop/fig1_1_4.png')
Add Comment
Please, Sign In to add comment