Ebrin
Ebrin

Reputation: 209

Visualize a 408x408 numpy array as a heatmap

Hello I want to visualize the sandbox game map. I have collected the data from API and now I want to create a heatmap kind of visualization, where the color changes depending on how many times the land's been sold. I'm looking for a Python tool / GUI that will let me visualize a 408x408 numpy array. I've tried the seaborn heatmap, but it doesn't look clean (see image), even If I try to set figsize to (200, 200) it's not big enough for my needs. I want to have a visualization on potentially whole screen, where each land is big enough so that I can write something on it (potentially price). Better option would be to have a big map with sliders.

Perhaps it's possible to do what I want using Seaborn's heatmap, but I'm not very familiar with it.

Here's the code I used for visualization:

import matplotlib.pyplot as plt
import seaborn as sns
import numpy as np

arr = np.random.rand(408, 408)
x_labels = list(range(-204, 204))
y_labels = list(reversed(range(-204, 204)))

fig, ax = plt.subplots(figsize=(100, 100))
sns.heatmap(arr, square=True, xticklabels=x_labels, yticklabels=y_labels, ax=ax)
ax.tick_params(axis="both", labelsize=40)

Upvotes: 0

Views: 323

Answers (1)

Atri
Atri

Reputation: 26

Visualizing such large data with seaborn or Matplotlib will be difficult.

For that, we can use Plotly and the dash python library. So, we can add a slider to view some portion of data at a time.

I have used these two libraries.

import plotly.express as px
from dash import Dash, dcc, html, Input, Output
import numpy as np
import pandas as pd

#creating data
arr = np.random.rand(408, 408)
x_labels = list(range(-204, 204))
y_labels = list(reversed(range(-204, 204)))

#Converted to dataframe
df_data = pd.DataFrame(arr,index =y_labels, columns = [x_labels] )

app = Dash(__name__)

#How many items to show at a time
show_item_limit = 20

app.layout = html.Div([
    html.H4('Range'),
    dcc.Graph(id="graph"),
    html.P("Select range"),
    dcc.Slider(
        min = 0,
        max = 408-show_item_limit, 
        step = show_item_limit,
        value = 0,
        id= 'my-slider'
    ),
])


@app.callback(
    Output("graph", "figure"), 
    Input("my-slider", "value"))

def filter_heatmap(selected_value):
    # Selected value will be passed from Slider
    df = df_data # replace with your own data source
    #We can filter the data here
    filtered_df = df_data.iloc[selected_value:selected_value+show_item_limit,range(selected_value,selected_value+show_item_limit)]
    #Update using plotly 
    fig = px.imshow(filtered_df,
                text_auto=True, 
                labels=dict(x="X-range", y="y-range"),
                x = filtered_df.columns,
                y = filtered_df.index
                )
    return fig
app.run_server(debug=True)

See the output image: Output from code

Upvotes: 1

Related Questions