Verbesserungen: Visualierungen und Achsenbeschriftung an Template angepasst

This commit is contained in:
2025-05-18 17:20:21 +02:00
parent 0b0dda1dc2
commit d80a293278

View File

@ -4,10 +4,12 @@ import os
# Terminal leeren # Terminal leeren
os.system('cls' if os.name == 'nt' else 'clear') os.system('cls' if os.name == 'nt' else 'clear')
from ci_template import plotly_template
from datetime import datetime from datetime import datetime
import bibtexparser import bibtexparser
import pandas as pd import pandas as pd
import numpy as np import numpy as np
from scipy.stats import pearsonr
import subprocess import subprocess
from slugify import slugify from slugify import slugify
@ -27,6 +29,8 @@ import matplotlib.pyplot as plt
# Debugging and Output # Debugging and Output
from tabulate import tabulate from tabulate import tabulate
theme = "light" # "light" oder "dark"
# Name der BibTeX-Datei für Exportzwecke # Name der BibTeX-Datei für Exportzwecke
bib_filename = "Suchergebnisse.bib" bib_filename = "Suchergebnisse.bib"
@ -83,19 +87,11 @@ bib_path = os.path.join("Research", "Charité - Universitätsmedizin Berlin", "S
with open(bib_path, encoding='utf-8') as bibtex_file: with open(bib_path, encoding='utf-8') as bibtex_file:
bib_database = bibtexparser.load(bibtex_file) bib_database = bibtexparser.load(bibtex_file)
# Farben definieren # Template aktivieren und Farben/Styles setzen
colors = { plotly_template.set_theme(theme)
"background": "#003366", # Hintergrundfarbe from ci_template.plotly_template import get_colors, get_plot_styles, get_standard_layout
"text": "#333333", # Textfarbe colors = get_colors()
"accent": "#663300", # Akzentfarbe plot_styles = get_plot_styles()
"primaryLine": "#660066", # Bildungswirkfaktor
"secondaryLine": "#cc6600", # Bildungswirkindikator
"depthArea": "#006666", # Kompetenzmessunsicherheit
"brightArea": "#66CCCC", # Kompetenzentwicklungsunsicherheit
"positiveHighlight": "#336600", # Positive Hervorhebung
"negativeHighlight": "#990000", # Negative Hervorhebung
"white": "#ffffff" # Weiß
}
# Aktuelles Datum # Aktuelles Datum
current_date = datetime.now().strftime("%Y-%m-%d") current_date = datetime.now().strftime("%Y-%m-%d")
@ -239,8 +235,6 @@ def interpret_correlation(x_term, y_term, correlation, min_corr, max_corr):
else: else:
return "Negativ verbunden" return "Negativ verbunden"
from scipy.stats import pearsonr
def visualize_bivariate_correlation(df, x_terms, y_terms, title, x_label, y_label, export_flag): def visualize_bivariate_correlation(df, x_terms, y_terms, title, x_label, y_label, export_flag):
""" """
Visualisiert bivariate Korrelationen und zeigt Interpretationen sowie Signifikanz im Tooltip an. Visualisiert bivariate Korrelationen und zeigt Interpretationen sowie Signifikanz im Tooltip an.
@ -342,10 +336,9 @@ def visualize_bivariate_correlation(df, x_terms, y_terms, title, x_label, y_labe
color_continuous_scale=color_scale # Dynamische Farbskala color_continuous_scale=color_scale # Dynamische Farbskala
) )
# Layout anpassen
fig.update_traces( fig.update_traces(
marker=dict( marker=dict(
line=dict(width=1) line=dict(width=1, color=colors['background'])
), ),
hovertemplate=( hovertemplate=(
'<b>%{customdata[0]}</b><br>' '<b>%{customdata[0]}</b><br>'
@ -356,13 +349,25 @@ def visualize_bivariate_correlation(df, x_terms, y_terms, title, x_label, y_labe
customdata=correlation_df[['x_term', 'p_value', 'significance']].to_numpy() customdata=correlation_df[['x_term', 'p_value', 'significance']].to_numpy()
) )
# Standardlayout verwenden und ggf. ergänzen, Margin dynamisch für Responsivität
fig.update_layout( fig.update_layout(
plot_bgcolor=colors['background'], get_standard_layout(
paper_bgcolor=colors['background'], title=f'{title} (n={len(correlation_df)}, Stand: {current_date}) | Quelle: {bib_filename.replace(".bib", "")}',
font=dict(color=colors['white']), x_title=x_label,
coloraxis_colorbar=dict(title='Korrelationswert'), # Legende y_title=y_label
),
xaxis=dict(
tickangle=-45,
automargin=True
), # Einheitlicher Winkel für X-Achsenbeschriftungen
yaxis=dict(
automargin=True
),
coloraxis_colorbar=dict(title='Korrelationswert'),
autosize=True, autosize=True,
margin=dict(l=0, r=0, t=40, b=40) margin=dict(l=0, r=0, t=60, b=60),
height=None,
width=None
) )
export_and_transfer_figure( export_and_transfer_figure(
fig, fig,
@ -454,7 +459,6 @@ def visualize_indizes_vs_indizes(export_flag):
#====================================== #======================================
# Farben für die Cluster
cluster_colors = { cluster_colors = {
"0": colors['primaryLine'], # Cluster 0 "0": colors['primaryLine'], # Cluster 0
"1": colors['secondaryLine'], # Cluster 1 "1": colors['secondaryLine'], # Cluster 1
@ -554,21 +558,21 @@ for cluster, label in cluster_labels.items():
plot_title += f" | Quelle: {bib_filename.replace('.bib', '')}" plot_title += f" | Quelle: {bib_filename.replace('.bib', '')}"
fig_cluster = px.scatter_3d( fig_cluster = px.scatter_3d(
df, df,
x='X_Dimension', # X-Achse: Deduktive Dimension (Suchbegriffe) x='X_Dimension',
y='Y_Dimension', # Y-Achse: Deduktive Dimension (Kategorien) y='Y_Dimension',
z='Z_Dimension', # Z-Achse: Deduktive Dimension (Forschungsfragen) z='Z_Dimension',
color='Cluster_Label', # Cluster-Beschreibungen mit Umbrüchen color='Cluster_Label',
size='Point_Size', # Dynamische Punktgröße basierend auf Wertigkeit size='Point_Size',
size_max=100, # Maximale Punktgröße anpassen für bessere Sichtbarkeit size_max=100,
color_discrete_sequence=list(cluster_colors.values()), # Farben für Cluster-Beschreibungen color_discrete_sequence=list(cluster_colors.values()),
hover_data={ hover_data={
'Cluster_Label': True, # Statische Cluster-Beschreibungen mit Umbrüchen 'Cluster_Label': True,
'X_Dimension': True, # Deduktive Dimension: Suchbegriffe 'X_Dimension': True,
'Y_Dimension': True, # Deduktive Dimension: Kategorien 'Y_Dimension': True,
'Z_Dimension': True, # Deduktive Dimension: Forschungsfragen 'Z_Dimension': True,
'Point_Size': True # Dynamische Punktgröße 'Point_Size': True
}, },
title=plot_title, # Dynamische Überschrift mit Silhouette-Score und Quelle title=plot_title,
labels={ labels={
'X_Dimension': 'Suchbegriffe', 'X_Dimension': 'Suchbegriffe',
'Y_Dimension': 'Kategorien', 'Y_Dimension': 'Kategorien',
@ -578,62 +582,23 @@ fig_cluster = px.scatter_3d(
} }
) )
# Layout anpassen # Layout mit Standardlayout und individuellen Ergänzungen
fig_cluster.update_layout( layout_cluster = get_standard_layout(
scene=dict( title=plot_title,
xaxis=dict( x_title='Suchbegriffe',
title='Suchbegriffe', # Titel der X-Achse y_title='Kategorien'
showbackground=True, # Hintergrund anzeigen )
backgroundcolor=colors['background'], # Hintergrundfarbe
gridcolor=colors['white'], # Gitterlinienfarbe fig_cluster.update_layout(
zerolinecolor=colors['white'], # Null-Linienfarbe **layout_cluster
showline=True, # Achsenlinie anzeigen
tickcolor=colors['white'], # Tick-Farbe
titlefont=dict(size=12, color=colors['white']) # Titelstil der Achse
),
yaxis=dict(
title='Kategorien', # Titel der Y-Achse
showbackground=True,
backgroundcolor=colors['background'],
gridcolor=colors['white'],
zerolinecolor=colors['white'],
showline=True,
tickcolor=colors['white'],
titlefont=dict(size=12, color=colors['white'])
),
zaxis=dict(
title='Forschungsfragen', # Titel der Z-Achse
showbackground=True,
backgroundcolor=colors['background'],
gridcolor=colors['white'],
zerolinecolor=colors['white'],
showline=True,
tickcolor=colors['white'],
titlefont=dict(size=12, color=colors['white'])
)
),
plot_bgcolor=colors['background'], # Plot-Hintergrundfarbe
paper_bgcolor=colors['background'], # Papierhintergrundfarbe
font=dict(color=colors['white']), # Schriftfarbe
showlegend=True, # Legende anzeigen
legend=dict(
title=dict(text="Cluster-Beschreibung", font=dict(size=12, color=colors['white'])),
font=dict(size=10, color=colors['white']),
bgcolor=colors['background'], # Hintergrund der Legende
bordercolor=colors['white'], # Rahmenfarbe der Legende
borderwidth=1 # Rahmenbreite der Legende
),
template="plotly_white" # Plotly-Template
) )
# Plot anzeigen und ggf. exportieren
export_and_transfer_figure( export_and_transfer_figure(
fig_cluster, fig_cluster,
"clusteranalyse_kmeans_deduktiv", "clusteranalyse_kmeans_deduktiv",
export_fig_clusteranalyse export_fig_clusteranalyse
) )
# Berechnung der Korrelationen und Erstellung der Übersicht # Berechnung der Korrelationen und Erstellung der Übersicht
def analyze_correlation_quality(df, x_terms, y_terms): def analyze_correlation_quality(df, x_terms, y_terms):
@ -741,13 +706,23 @@ def plot_average_correlation_plotly(summary_df):
) )
fig.update_layout( fig.update_layout(
xaxis_tickangle=-45, get_standard_layout(
plot_bgcolor=colors['background'], title=f"Durchschnittliche Korrelationen pro Korrelationstyp (n={len(summary_df)}, Stand: {current_date}) | Quelle: {bib_filename.replace('.bib', '')}",
paper_bgcolor=colors['background'], x_title="Korrelationstyp",
font=dict(color=colors['white']), y_title="Durchschnittliche Korrelation"
),
xaxis=dict(
tickangle=-45,
automargin=True
),
yaxis=dict(
automargin=True
),
coloraxis_colorbar=dict(title="Korrelationswert"), coloraxis_colorbar=dict(title="Korrelationswert"),
autosize=True, autosize=True,
margin=dict(l=0, r=0, t=40, b=40) margin=dict(l=60, r=40, t=80, b=80),
height=None,
width=None
) )
export_and_transfer_figure( export_and_transfer_figure(
fig, fig,
@ -755,7 +730,6 @@ def plot_average_correlation_plotly(summary_df):
export_fig_summary_plot export_fig_summary_plot
) )
#============================ #============================
# Aufruf Alle möglichen bivariaten Korrelationen visualisieren # Aufruf Alle möglichen bivariaten Korrelationen visualisieren