So erstellen Sie eine Heatmap aus einem ordentlichen/langen PolardatenrahmenPython

Python-Programme
Anonymous
 So erstellen Sie eine Heatmap aus einem ordentlichen/langen Polardatenrahmen

Post by Anonymous »

Ich muss eine Heatmap auf Basis eines ordentlichen/langen pl.DataFrame erstellen. Betrachten Sie das folgende Beispiel, in dem ich pandas und plotly verwendet habe, um eine Heatmap zu erstellen.

Code: Select all

import plotly.express as px
import polars as pl

tidy_df_pl = pl.DataFrame(
{
"x": [10, 10, 10, 20, 20, 20, 30, 30, 30],
"y": [3, 4, 5, 3, 4, 5, 3, 4, 5],
"value": [5, 8, 2, 4, 10, 14, 10, 8, 9],
}
)

print(tidy_df_pl)

shape: (9, 3)
┌─────┬─────┬───────┐
│ x   ┆ y   ┆ value │
│ --- ┆ --- ┆ ---   │
│ i64 ┆ i64 ┆ i64   │
╞═════╪═════╪═══════╡
│ 10  ┆ 3   ┆ 5     │
│ 10  ┆ 4   ┆ 8     │
│ 10  ┆ 5   ┆ 2     │
│ 20  ┆ 3   ┆ 4     │
│ 20  ┆ 4   ┆ 10    │
│ 20  ┆ 5   ┆ 14    │
│ 30  ┆ 3   ┆ 10    │
│ 30  ┆ 4   ┆ 8     │
│ 30  ┆ 5   ┆ 9     │
└─────┴─────┴───────┘
Umwandeln in einen breiten pd.DataFrame:

Code: Select all

pivot_df_pd = (
tidy_df_pl.pivot(index="x", on="y", values="value").to_pandas().set_index("x")
)
print(pivot_df_pd)

3   4   5
x
10   5   8   2
20   4  10  14
30  10   8   9
Erstellen der Heatmap mit plotly.

Code: Select all

fig = px.imshow(pivot_df_pd)
fig.show()
Image

Das scheint alles etwas umständlich zu sein. Ich suche nur nach Polaren.
Wie kann ich diese Heatmap direkt aus Polaren erstellen, ohne eine dritte Bibliothek zu verwenden?

Quick Reply

Change Text Case: 
   
  • Similar Topics
    Replies
    Views
    Last post