<jupyter_start><jupyter_text>Erster Versuch: Visualisierung 'Kreisradar' Benötigte Bibliotheken:<jupyter_code>import pandas as pd
import numpy as np
import dash
import plotly
import pickle
# Einlesen der Daten 
data = pd.read_pickle('../Data/data.pkl')
data<jupyter_output><empty_output><jupyter_text>Wir ersetzen die NaNs durch 0, damit wir Rechenoperationen an der Tabelle durchführen können.<jupyter_code>data = data.fillna(0)
data<jupyter_output><empty_output><jupyter_text>Wie in der Excel Datai angegeben, brauchen wir noch die Summen pro Spalte bzw. das Gesamtvolumen pro SDG über alle Verwendungszwecke.<jupyter_code>sums = data.iloc[: ,1:188].apply(sum, axis = 0) # Anwendung der Funktion sum auf alle Spalten bis auf Verwendungszweck
sums_df = pd. DataFrame(sums).T # Überführung in ein DataFrame (optional)<jupyter_output><empty_output><jupyter_text>Da wir keine Unterpunkte, sondern nur SDG1 bis SDG17 benötigen, filtern wir diese raus.<jupyter_code>sums_df.columns
# Bildung einer Liste mit den SDG Namen, auch manuell möglich oder in List Comprehension
sdgs = []

for col in sums_df.columns:
    if 'SDG' in str(col):
        sdgs.append(col)
sdgs<jupyter_output><empty_output><jupyter_text>Nun können wir eine Subtabelle/ Array bilden, welche nur die Summen über die 17 SDGs enthält.<jupyter_code>sdg = sums_df[sdgs]
sdg_sums = (sdg.T).rename(columns={0: 'sum'})
sdg_sums<jupyter_output><empty_output><jupyter_text>Laut KfW soll der höchste Wert eines SDGs auf 100% abgebildet werden.<jupyter_code>def mapping(x):
    y = []
    for i in list(x):
        if i == x.max():
            i=100
        else:
            i=round(i/x.max()*100,2)
        y.append(i)
    return y
sdg_sums['mapping'] = mapping(sdg_sums['sum'])
sdg_sums
from PIL import Image
img = Image.open("C:/Users/frik/Pictures/kfw2.PNG")
import plotly.graph_objects as go

fig = go.Figure(go.Barpolar(dr=0.3,
    r=[4, 3.5 ,3.5, 3.5, 3.5, 3.5, 10, 10, 4.5, 3.5, 10, 3.5, 10, 3.5, 3.5, 3.5, 4],
    theta=[360/17*(x+1) for x in range(17)],
    width=[20, 20, 20, 20, 20, 20, 20, 20, 20, 20, 20, 20, 20, 20, 20, 20, 20],
    hoverinfo = 'text',
   
    marker_color=['rgb(229, 36, 59)',
'rgb(221, 166, 58)',
'rgb(76, 159, 56)',
'rgb(197, 25, 45)',
'rgb(255, 58, 33)',
'rgb(38, 189, 226)',
'rgb(252, 195, 11)',
'rgb(162, 25, 66)',
'rgb(253, 105, 37)',
'rgb(221, 19, 103)',
'rgb(253, 157, 36)',
'rgb(191, 139, 46)',
'rgb(63, 126, 68)',
'rgb(10, 151, 217)',
'rgb(86, 192, 43)',
'rgb(0, 104, 157)',
'rgb(25, 72, 106)'

],
    
    text=['No Poverty',
'Zero Hunger', 
'Good Health and Well-being',
'Quality Education', 
'Gender Equality', 
'Clean Water and Sanitation', 
'Affordable and Clean Energy', 
'Decent Work and Economic Growth', 
'Industry, Innovation and Infrastructure', 
'Reduced Inequality', 
'Sustainable Cities and Communities', 
'Responsible Consumption and Production', 
'Climate Action', 
'Life Below Water', 
'Life on Land', 
'Peace and Justice Strong Institutions', 
'Partnerships to achieve the Goal' ]
))

fig.update_layout(
    template=None,
    width=600, 
    height=600,
    title='KfW SDG contribution - new commitment volume year 2018',
    font_size=14,
    legend_font_size=14,
    hoverlabel_align = "right",
    #polar_radialaxis_ticksuffix='%',
    #polar_angularaxis_rotation=5,
    polar_angularaxis_direction="clockwise",
    polar = dict(
        radialaxis = dict(range=[0, 5], showticklabels=False, ticks='', color = 'white'),
        angularaxis = dict(showticklabels=False, ticks='', color = 'white'),
     
    )
)







#fig.add_shape(
#        dict(
##            type="circle",
#            fillcolor= "white",
#            layer ="above",
#             xref="paper", yref="paper",
#             x0= 0.25,
#             x1= 0.25,
#             y0=0.55,
#             y1 = 0.55,
#            line=dict(
#                color="white",
#                width=0.25
#            )
#))
                     
fig.add_layout_image(dict(source= img,
                         xref="paper", yref="paper",
                          layer="above",
                          yanchor="top",
                         sizex=0.45, sizey=0.45,
                          x= 0.28,
                          y=0.72,
                         ))
                                           
               
fig.show()



import plotly.graph_objects as go

labels = ['No Poverty',
'Zero Hunger', 
'Good Health and Well-being',
'Quality Education', 
'Gender Equality', 
'Clean Water and Sanitation', 
'Affordable and Clean Energy', 
'Decent Work and Economic Growth', 
'Industry, Innovation and Infrastructure', 
'Reduced Inequality', 
'Sustainable Cities and Communities', 
'Responsible Consumption and Production', 
'Climate Action', 
'Life Below Water', 
'Life on Land', 
'Peace and Justice Strong Institutions', 
'Partnerships to achieve the Goal']

values = sdg_sums['mapping']

# Use `hole` to create a donut-like pie chart
fig = go.Figure(data=[go.Pie(labels=labels, 
                             values=values, 
                             hole=.5, 
                             hoverinfo = 'text',
                             marker_colors=['rgb(229, 36, 59)',
'rgb(221, 166, 58)',
'rgb(76, 159, 56)',
'rgb(197, 25, 45)',
'rgb(255, 58, 33)',
'rgb(38, 189, 226)',
'rgb(252, 195, 11)',
'rgb(162, 25, 66)',
'rgb(253, 105, 37)',
'rgb(221, 19, 103)',
'rgb(253, 157, 36)',
'rgb(191, 139, 46)',
'rgb(63, 126, 68)',
'rgb(10, 151, 217)',
'rgb(86, 192, 43)',
'rgb(0, 104, 157)',
'rgb(25, 72, 106)'

])])

fig.update_traces(hoverinfo='label', textinfo='none')
fig.update(layout_title_text='KfW SDG contribution - new commitment volume year 2018',
           layout_showlegend=False)

fig.update_layout(
    font_size=16
    )

fig.show()<jupyter_output><empty_output><jupyter_text>Versuch: Circular Barplot<jupyter_code>import math
df = sdg_sums
colors = ['rgb(229, 36, 59)',
'rgb(221, 166, 58)',
'rgb(76, 159, 56)',
'rgb(197, 25, 45)',
'rgb(255, 58, 33)',
'rgb(38, 189, 226)',
'rgb(252, 195, 11)',
'rgb(162, 25, 66)',
'rgb(253, 105, 37)',
'rgb(221, 19, 103)',
'rgb(253, 157, 36)',
'rgb(191, 139, 46)',
'rgb(63, 126, 68)',
'rgb(10, 151, 217)',
'rgb(86, 192, 43)',
'rgb(0, 104, 157)',
'rgb(25, 72, 106)'

]

# Get incremental angle value
n = len(df) + 20
dtheta = 2*math.pi / n
theta = math.pi / 2

# Initialise
xcoord = []
ycoord = []


# This is for the white - circle in the middle
adjust =  20
fig = go.Figure()


for i in range(len(df)):
    
    a = df.mapping[i] + adjust
    x1 = adjust * math.cos(theta)
    y1 = adjust * math.sin(theta)
  
    x2 = a * math.cos(theta)
    y2 = a * math.sin(theta)
  
    xcoord.append(x1)
    xcoord.append(x2)
    xcoord.append(np.nan)
    ycoord.append(y1)
    ycoord.append(y2)
    ycoord.append(np.nan)
    #cols = [cols, df$Continent[ctr], df$Continent[ctr], NA)
  
    theta = theta + dtheta
  
    
    fig.add_trace(go.Scatter(x = [x1, x2],
                 y = [y1, y2],
                 mode = "lines", 
                 line = dict(width = 10, color = colors[i])
                                ))
    

up = np.array([x for x in xcoord if str(x) != 'nan']+[x for x in ycoord if str(x) != 'nan']).max()+10
down = np.array([x for x in xcoord if str(x) != 'nan']+[x for x in ycoord if str(x) != 'nan']).min()-10

fig.update_xaxes(range=[down, up], domain = [0, 1], title="", showgrid= False, zeroline=False, showticklabels = False)
fig.update_yaxes(range=[down, up], domain = [0, 1], title="", showgrid= False, zeroline=False, showticklabels = False)
fig.update_layout(showlegend=False, width=800, height=800, template=None)


fig.add_shape(
        dict(
            type="circle",
            x0= ( - adjust),
            y0= ( - adjust),
            x1= ( adjust),
            y1= ( adjust),
            fillcolor= "white",
            line=dict(
                color="black",
                width=1
            )
))




fig.update_shapes(dict(xref='x', yref='y'))

fig.show()<jupyter_output><empty_output>
{'fraction_non_alphanumeric': 0.15732596129680823, 'fraction_numerical': 0.07225433526011561, 'mean_word_length': 2.852371732817038, 'pattern_counts': {'":': 0, '<': 29, '<?xml version=': 0, '>': 29, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 5, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'chain_length': '7', 'dir': 'jupyter-structured-clean-dedup', 'id': '200774', 'license': 'MIT', 'n_tokens_mistral': 3415, 'n_tokens_neox': 2908, 'n_words': 746, 'path': 'Notebooks/Kreisradar Visualisierung.ipynb', 'repo_name': 'adessoAG/kfw-prototyp', 'size': '7958'}