Package: electionGraphs
Election Result GraphsWithElectionResultGraphsyou can create either four different graph types or all graphs as one chart for displaying the result of a election based on a csv-inputfile.
As input acsv-fileneeds to be provided. In the initialization the data will be processed aspandasdataframeand graphs fromplotlycan be exported as image-files.pip install electionGraphsFeaturesdata from the csv-file will be processed and more data points will be calculated (initialization)a.getGraph()function can create different types of graphsa bar graph showing the results in relative votes can be displayeda bar graph showing a comparision of relative votes to last voting perioda pie chart shows the seat allocation of the parliamenta stacked bar chart shows possible coalitions.createOnePager()creates all graph options and combines them into one chartSeat Allocation (Parliament)seats of a parliament will be allocated based on relative voting resultsa threshold is implemented, so that a party needs to reach a certain relative voting result in order to be considered in the seat allocation (this feature can be adjusted by parameters)a list of parties can be excluded from the seat allocation (e.g. a "other" category)for the seat allocation the relative results (excluding parties that are defined by theexcludePartiesparameters or by thepercentageLimit) will be multiplied byparliamentSeats, the result will be floored. Not allocated seats will then be allocated each party ordered by the highest modulus (decimal rest from the division) until all seats are allocatedCoalitionscoalitions will be calculated based on total seats. The political spectrum will be considered as a thresholdfor every possible coaltion the distance based on the delivered information ofcolumnSpectrumwill be calculated. Reaches the overall distance thethresholdPoliticalDistancethe coalition will not be included in the final graphDisclaimerThis tool is loosly based on german elections. Some rulesets may differ from the reality (e.g. not considering first vote, second vote). This is just a hobby project for practicing data processing and data visualization.Example GraphOne Pager with multiple different graphsSingle chart of bar graphVotingGraphsFor the initialization the mandatory parameters must be given.fromelectionGraphsimportelectionGraphs# example for instantiating the classeg=electionGraphs.ElectionGraphs("exampleData.csv","YEAR","VOTINGS","PARTY_SHORT","PARTY_SPEC","PARTY_COLOR",)After initialization different aspects of logic and styling can be configured by setting the optional parameters:votingGraphs.titleMain="My other Title"votingGraphs.colors["background"]="#000000"votingGraphs.fontsize["titleMain"]=58mandatory parameters/attributescsvFile=(str)csv file to read in --For example of the structure of csv see example filecolumnYear=(str)name of column in csv in which the year is recordedcolumnVotings=(str)name of column in csv in which the absolute number of votings per party is recordedcolumnParty=(str)name of column in csv in which party name is recordedcolumnSpectrum=(str)name of column in csv in which the political orientation of the party is recordedcolumnColor=(str)name of column in csv in which the color code of the party is recordedoptional parameters/attributescolors=(dict{str:str})hex-color for different elements of the graphs. The value must be astrof hex-color (e.g."#F2EAD3") with following keys:backgrounddiagramtitlesubtitleyaxisxaxisgridvaluesthresholdexcludeParties=(list[str], default = ["Sonstiges", "Sonstige", "Other"])list of party names which will not be included in the seat allocationfilenameBarCompare=(str, default =  "barDifference.png")filename of export graph. Must be a .png filefilenameBarResult=(str, default =  "barResult.png")filename of export graph. Must be a .png filefilenameBarCoalitions=(str, default =  "barCoalition.png")filename of export graph. Must be a .png filefilenameOnePager=(str, default =  "ElectionResults")filename of export graph. Must be a .png filefilenamePieParliament=(str, default =  "pieParliament.png")filename of export graph. Must be a .png filefontfamily=(str, default = "Futura")font family of all graphsfontsize=(dict{str:int})font sizes of different elements. Each key represent a text type with following options:titlesubtitlevaluesyaxisxaxisoutputfolder=(str, default = "output")folder in which all output images will be savedparliamentSeats=(int, default = 120)number of seats in parliament. This parameter needs be defined at the initialization, otherwise some graphs will not display the seats correctlypercentageLimit=(int, default = 5)threshold at which a party will not be considered when calculting seat distributionthresholdPolitcalDistance=(int, default = 300)defines a threshold for displaying possible coalitions. When the politcal distance in a mathmatical possible coalition is too high, it can be filteredtitleBarCoalitions=(str, default="Koalitionen")adjusts title of bar graph with possible coalitionstitleBarCompare=(str, default="Veränderung der Wählerstimmen")adjusts title of bar graph with comparision to last votingtitleBarResult=(str, default="Wählerstimmen")adjusts title of bar graph with resultstitleMain=(str, default="Wahlergebnisse")adjusts main title of one pagertitlePieParliament=(str, default="Sitzverteilung")adjusts  title of parliament graphseperator=(str, default = ";")type of seperator in csvsubtitleBarCoalitions=(str, default="Anzahl Sitze für mögliche Koalitionen")adjusts a subtitle of corresponding graphsubtitleBarCompare=(str, default="Prozentpunkte im Vergleich zur letzten Wahl")adjusts a subtitle of corresponding graphsubtitleBarResult=(str, default="Anteil der Wählerstimmen in Prozent")adjusts a subtitle of corresponding graphsubtitlePieParliament=(str, default="Anzahl Sitze im Parlament")adjusts a subtitle of corresponding graphfunction: getGraphCreates a png-file of a specific chart type for the provided data# example for creating a graphelectionGraphs.getGraph(2021,type="BAR_DIFFERENCE")mandatory parameters/attributesyear=(int)year of datatype=(str, specific options)type of graph:"BAR_RESULT"= bar graph with basic results"BAR_COMPARE"= bar graph with comparison to last voting year"PIE_PARLIAMENT"= pie chart for seat distribution in a parliament"BAR_COALITIONS"= stacked bar chart with possible coalitionsoptional parameters/attributesMany of the optional parameters/attributes of the main classVotingGraphswill be applied to the charts (e.g. titles, colors, fonts, etc.)function: createOnePagerCreates a png-file with all four chart types arranged# example for creating a one pagerelectionGraphs.createOnePager()optional parameters/attributesyear=(int)year of data, if left empty all possible years will be processed and for each year a file will be createdMany of the optional parameters/attributes of the main classVotingGraphswill be applied to the charts (e.g. titles, colors, fonts, etc.)Dependenciespandasplotlykaleidopillow