So richten Sie die Zeilen- und Spaltenbezeichnungen des Plotly -Nebenhandelsgitters in der Python -Dash -Anwendung ausPython

Python-Programme
Anonymous
 So richten Sie die Zeilen- und Spaltenbezeichnungen des Plotly -Nebenhandelsgitters in der Python -Dash -Anwendung aus

Post by Anonymous »

Ich versuche eine Dash -Anwendung zu erstellen, in der ein Gitter von Nebenhandlungen angezeigt wird, um den paarweisen Vergleich der Spalten eines Datenrahmens zu visualisieren. Oben und links in jeder Rasterreihe und der Spalte befinden sich die entsprechenden Variablen. Die Variablennamen können jedoch ziemlich lang sein, daher ist es einfach, sie falsch auszurichten. Ich habe es versucht, die Variablennamen zu taumeln, sie aber schließlich auf der Linie eingeschaltet, um sie zu übertreffen. Siehe das Bild unten. Ich habe auch meinen Code am Ende dieses Beitrags < /p> aufgenommen

Code: Select all

df = pd.DataFrame({
"AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA": ["1", "2", "3", "4"],
"BBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBB": ["2024-01-01", "2024-01-02", "2024-01-03", "2024-01-04"],
"CCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCC": ["cat", "dog", "cat", "mouse"],
"DDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDD": ["10.5", "20.3", "30.1", "40.2"],
'EEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEE': ['apple', 'apple', 'apple', 'banana']
})
Für diesen Datenrahmen würde ich gerne so etwas wie

Code: Select all

import dash
from dash import dcc, html
import pandas as pd
import plotly.express as px
import plotly.subplots as sp
import numpy as np
import plotly.graph_objects as go

# Sample DataFrame
df = pd.DataFrame({
"AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA": ["1", "2", "3", "4"],
"BBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBB": ["2024-01-01", "2024-01-02", "2024-01-03", "2024-01-04"],
"CCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCC": ["cat", "dog", "cat", "mouse"],
"DDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDD": ["10.5", "20.3", "30.1", "40.2"],
'EEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEE': ['apple', 'apple', 'apple', 'banana']
})

# Convert data types
def convert_dtypes(df):
for col in df.columns:
try:
df[col] = pd.to_numeric(df[col])  # Convert to int/float
except ValueError:
try:
df[col] = pd.to_datetime(df[col])  # Convert to datetime
except ValueError:
df[col] = df[col].astype("string")  # Keep as string
return df

df = convert_dtypes(df)
columns = df.columns
num_cols = len(columns)

# Dash App
app = dash.Dash(__name__)

app.layout = html.Div([
html.H1("Pairwise Column Plots"),
dcc.Graph(id='grid-plots')
])

@app.callback(
dash.Output('grid-plots', 'figure'),
dash.Input('grid-plots', 'id')  # Dummy input to trigger callback
)
def create_plot_grid(_):
fig = sp.make_subplots(rows = num_cols, cols = num_cols,
#subplot_titles = [f"{x} vs {y}"  for x in columns for y in columns],
shared_xaxes = False, shared_yaxes = False)

annotations = []  # Store subplot titles dynamically
# Add column labels (Top Labels)
for j, col_label in enumerate(columns):
annotations.append(
dict(
#text=f"{col_label}[/b]",  # Bold for emphasis[b]                text=f"{'
'.join(col_label[x:x+10] for x in range(0, len(col_label), 10))}[/b]",[b]                xref = "paper", yref = "paper",
x = (j) / num_cols,  # Center over the column
y = 1.02,  # Slightly above the top row
showarrow = False,
font = dict(size = 14, color = "black")
)
)
# Add row labels (Side Labels)
for i, row_label in enumerate(columns):
annotations.append(
dict(
#text = f"{row_label}[/b]",  # Bold for emphasis[b]                text=f"{'
'.join(row_label[x:x+10] for x in range(0, len(row_label), 10))}[/b]",
xref = "paper", yref = "paper",
x = -0.02,  # Slightly to the left of the row
y = (1 - (i + 0.5) / num_cols),  # Center next to the row
showarrow = False,
font = dict(size = 14, color = "black"),
textangle = -90  # Rotate text for vertical orientation
)
)

print(annotations)

for i, x_col in enumerate(columns):
for j, y_col in enumerate(columns):
dtype_x, dtype_y = df[x_col].dtype, df[y_col].dtype
row, col = i + 1, j + 1  # Adjust for 1-based indexing

# I only want to print the upper triangle of the grid
if j

Quick Reply

Change Text Case: 
   
  • Similar Topics
    Replies
    Views
    Last post
  • Fehlerberechnung mehrerer Def in Python Dash Plotly
    by Anonymous » » in Python
    0 Replies
    5 Views
    Last post by Anonymous
  • Plotly-Dash – OSError: [WinError 87] Der Parameter ist falsch
    by Guest » » in Python
    0 Replies
    11 Views
    Last post by Guest
  • Leeres Diagramm mit Plotly Dash in JupyterLab
    by Anonymous » » in Python
    0 Replies
    10 Views
    Last post by Anonymous
  • Dash Plotly x-Achse
    by Anonymous » » in Python
    0 Replies
    8 Views
    Last post by Anonymous
  • So richten Sie die Ansicht am Ende im Zeilen -Jetpack komponieren
    by Anonymous » » in Android
    0 Replies
    0 Views
    Last post by Anonymous