python - 基于线的热图或二维线直方图

标签 python plot heatmap

我有一个合成数据集,其中包含 1000 个不同阶次的噪声多边形和正弦/余弦曲线,我可以使用 python seaborn 将其绘制为线。

enter image description here

由于我有很多重叠的线,我想绘制我的折线图的某种热图或直方图。
我尝试遍历列并聚合计数以使用 seaborn 的热图图,但是对于许多行,这需要很长时间。

导致我想要的下一个最好的结果是 hexbin 图(带有 seaborn 关节图)。

enter image description here

但它是运行时和粒度之间的折衷(显示的图形的网格大小为 750)。我找不到任何其他图形类型来解决我的问题。但我也不知道它到底叫什么。

我也试过将线 alpha 设置为 0.2。这会产生与我想要的类似的图表。但它不太精确(如果超过 5 条线在同一点重叠,我的透明度已经为零)。此外,它错过了热图的典型着色。

(没有实际意义的搜索词是:热图、二维线直方图、线直方图、密度图……)

有没有人知道包来绘制这个更高效和更高质量 或者知道如何使用流行的 python 绘图仪(即 matplotlib 系列:matplotlib、seaborn、bokeh)进行操作。不过我对任何包裹都很好。

最佳答案

我花了一段时间,但我终于使用 Datashader 解决了这个问题.如果使用笔记本,则可以将绘图嵌入交互式 Bokeh情节,看起来真的很不错。

无论如何,这是静态图像的代码,以防其他人需要类似的东西:

# coding: utf-8
import time

import numpy as np
from numpy.polynomial import polynomial
import pandas as pd

import matplotlib.pyplot as plt
import datashader as ds
import datashader.transfer_functions as tf


plt.style.use("seaborn-whitegrid")

def create_data():
    # ...

# Each column is one data sample
df = create_data()

# Following will append a nan-row and reshape the dataframe into two columns, with each sample stacked on top of each other
#   THIS IS CRUCIAL TO OPTIMIZE SPEED: https://github.com/bokeh/datashader/issues/286

# Append row with nan-values
df = df.append(pd.DataFrame([np.array([np.nan] * len(df.columns))], columns=df.columns, index=[np.nan]))

# Reshape
x, y = df.shape
arr = df.as_matrix().reshape((x * y, 1), order='F')
df_reshaped = pd.DataFrame(arr, columns=list('y'), index=np.tile(df.index.values, y))
df_reshaped = df_reshaped.reset_index()
df_reshaped.columns.values[0] = 'x'

# Plotting parameters
x_range = (min(df.index.values), max(df.index.values))
y_range = (df.min().min(), df.max().max())
w = 1000
h = 750
dpi = 150
cvs = ds.Canvas(x_range=x_range, y_range=y_range, plot_height=h, plot_width=w)

# Aggregate data
t0 = time.time()
aggs = cvs.line(df_reshaped, 'x', 'y', ds.count())
print("Time to aggregate line data: {}".format(time.time()-t0))

# One colored plot
t1 = time.time()
stacked_img = tf.Image(tf.shade(aggs, cmap=["darkblue", "darkblue"]))
print("Time to create stacked image: {}".format(time.time() - t1))

# Save
f0 = plt.figure(figsize=(w / dpi, h / dpi), dpi=dpi)
ax0 = f0.add_subplot(111)
ax0.imshow(stacked_img.to_pil())
ax0.grid(False)
f0.savefig("stacked.png", bbox_inches="tight", dpi=dpi)

# Heat map - This uses a equalized histogram (built-in default), there are other options, though.
t2 = time.time()
heatmap_img = tf.Image(tf.shade(aggs, cmap=plt.cm.Spectral_r))
print("Time to create stacked image: {}".format(time.time() - t2))

# Save
f1 = plt.figure(figsize=(w / dpi, h / dpi), dpi=dpi)
ax1 = f1.add_subplot(111)
ax1.imshow(heatmap_img.to_pil())
ax1.grid(False)
f1.savefig("heatmap.png", bbox_inches="tight", dpi=dpi)

使用以下运行时间(以秒为单位):
Time to aggregate line data: 0.7710442543029785
Time to create stacked image: 0.06000351905822754
Time to create stacked image: 0.05600309371948242

结果图:
Stacked lines

Heatmap

关于python - 基于线的热图或二维线直方图,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47175398/

相关文章:

python - Pandas:用组的模式填充 na

python - 改变matplotlib中散点的大小

python - 如何在 Seaborn 中轻松平滑热图绘图?

python - 在 Chef 服务器中,如何确定节点何时完全引导

python - 生成四元对

从R中的情节中删除 'y'标签

matlab - 当 x 轴是时间时,如何在绘图上绘制彩色矩形?

javascript - 是否有任何支持 Internet Explorer 8 的热图 javascript 库?

python - 使用seaborn绘制热图时出现类型错误

python - Django S3 Direct 上的 CORS 错误