Skip to content

Instantly share code, notes, and snippets.

@do-me
Created May 25, 2019 11:32
Show Gist options
  • Save do-me/3b50a3dec3584d618d9aa84f511ece63 to your computer and use it in GitHub Desktop.
Save do-me/3b50a3dec3584d618d9aa84f511ece63 to your computer and use it in GitHub Desktop.
# Scatterplots with seaborn
import pandas as pd
import os
pd.set_option('precision', 10) # working with csv data with high precision,
# meaning more characters after the comme
os.chdir("C:/Users/Dome/Desktop/nu/Wahl- und Strukturdaten/Tabellen/")
# avoid confusion by converting number seperators (comma to point)
ma = pd.read_csv("MASTERENG.csv",float_precision='round_trip') # high precision
import seaborn as sns
sns.set_style("darkgrid") # style set
# self defined scatterplot function one plot
def seascat(data,x,y,hue,path,docname): # data, columns, color, path, name
import seaborn as sns # useful module
sns.set_style("darkgrid") # set style
ax=sns.lmplot(data=data,x=x, y=y, hue=hue,fit_reg=False)
title = 'Area'
ax._legend.set_title(title) # generate scatterplot
# save plot
outp=docname+".png"
path=path
ax.savefig(path+outp) # resolution changable dpi=1000
import matplotlib.pyplot as plt
plt.close()
return("Done") # confirmation
###################### LOOP FUNCTION SCATTERPLOT ###########################
# get all column names by ma.columns.values and take them as list
colli=['Population 31.12.2015 Foreigners (%)',
'Population Density 31.12.2015 (Inhabitant per km2)',
'Population 31.12.2015 under 18 (%)',
'Population 31.12.2015 18-24 (%)',
'Population 31.12.2015 25-34 (%)',
'Population 31.12.2015 35-59 (%)',
'Population 31.12.2015 60-74 (%)',
'Population 31.12.2015 75 and more (%)',
'Flats Total 31.12.2015 (per 1000 Inhabitants)',
'Available Income Private Households 2014 (per Inhabitant)',
'GDP 2014 (per Inhabitant)',
'Cars 01.01.2016 (per 1000 Inhabitants)',
'Graduates School 2015 total without externals (per 1000 Inhabitants)',
'Graduates School 2015 without Hauptschulabschluss (%)',
'Graduates School 2015 with Hauptschulabschluss (%)',
'Graduates School 2015 with mittlerem Schulabschluss (%)',
'Graduates School 2015 with allgemeiner und Fachhochschulreife (matriculation standard) (%)',
'Childcare: Children 01.03.2016 (per 1000 Inhabitants)',
'Business Register 2014 Businesses total (per 1000 Inhabitants)',
'Business Register 2014 Craft Businesses (per 1000 Inhabitants)',
'State Benificiaries 31.12.2016 total (per 1000 Inhabitants)',
'State Benificiaries 31.12.2016 unable to work (nicht erwerbsfaehige Hilfebeduerftige) (%)',
'State Benificiaries 31.12.2016 Foreigners (%)',
'Unemployment Rate March 2017 total',
'Unemployment Rate March 2017 Men',
'Unemployment Rate March 2017 Women',
'Unemployment Rate March 2017 15 - under 20 Jahre',
'Unemployment Rate March 2017 55 - under 65 Jahre']
# for one party plot series would be
#count=0
#for i in colli:
# seascat(data=ma, x='afd17',y=i, hue="AreaBer",path=p,docname=str(count)+"afd_2017.png")
# count+=1
# user´s folder
p="C:/Users/Dome/Desktop/nu/Wahl- und Strukturdaten/Scatterplots/ALL/"
# for all parties (incl. selfindex17 and deltaindex) scatterplots
partei=[ 'linke17', 'gruenen17','spd17', 'fdp17','cducsu17', 'afd17', 'selfindex17', 'deltaindex']
# for all parties
for party in partei:
count=0
# for all structure columns
for i in colli:
seascat(data=ma, x=party ,y=i, hue="AreaBer",path=p,docname=str(count)+"_"+str(party)+".png") # hue colors the dots differently for East, West and Berlin seperately
count+=1
# all scatter plot automatically saved in user defined folder, indexed by number and party
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment