简体   繁体   English

将 408x408 numpy 阵列可视化为热图

[英]Visualize a 408x408 numpy array as a heatmap

Hello I want to visualize the sandbox game map.您好我想可视化沙盒游戏 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.我从 API 收集了数据,现在我想创建一个热图类型的可视化,其中颜色根据土地被出售的次数而变化。 I'm looking for a Python tool / GUI that will let me visualize a 408x408 numpy array.我正在寻找一个 Python 工具/GUI,它可以让我可视化 408x408 numpy 阵列。 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.我已经尝试过 seaborn 热图,但它看起来并不干净(见图),即使我尝试将 figsize 设置为 (200, 200) 也不足以满足我的需求。 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.更好的选择是有一个带滑块的大 map。

Perhaps it's possible to do what I want using Seaborn's heatmap, but I'm not very familiar with it.也许使用 Seaborn 的热图可以做我想做的事,但我对它不是很熟悉。

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)

Visualizing such large data with seaborn or Matplotlib will be difficult.用 seaborn 或 Matplotlib 可视化如此大的数据将很困难。

For that, we can use Plotly and the dash python library.为此,我们可以使用 Plotly 和破折号 python 库。 So, we can add a slider to view some portion of data at a time.因此,我们可以添加一个 slider 来一次查看部分数据。

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请参阅代码中的 output 图像: Output

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM