【问题标题】:Is there a Python package for plotting a spike map是否有用于绘制尖峰图的 Python 包
【发布时间】:2021-03-12 12:34:45
【问题描述】:

尖峰图(如下图所示,使用D3.js 实现)是一种显示某个离散的、突然变化的现象(例如人数)的大小差异的方法。

是否有我可以使用的包(或我可以遵循的示例代码)在 Python 中创建类似于上面显示的地图的静态峰值图?例如Matplotlib

【问题讨论】:

    标签: python matplotlib maps geopandas


    【解决方案1】:

    您可以尝试使用 Ridge Plot。它不完全相同,但也许它可以为你工作。 seaborn 中的实现如下所示:

    import numpy as np
    import pandas as pd
    import seaborn as sns
    import matplotlib.pyplot as plt
    sns.set_theme(style="white", rc={"axes.facecolor": (0, 0, 0, 0)})
    
    # Create the data
    rs = np.random.RandomState(1979)
    x = rs.randn(500)
    g = np.tile(list("ABCDEFGHIJ"), 50)
    df = pd.DataFrame(dict(x=x, g=g))
    m = df.g.map(ord)
    df["x"] += m
    
    # Initialize the FacetGrid object
    pal = sns.cubehelix_palette(10, rot=-.25, light=.7)
    g = sns.FacetGrid(df, row="g", hue="g", aspect=15, height=.5, palette=pal)
    
    # Draw the densities in a few steps
    g.map(sns.kdeplot, "x",
          bw_adjust=.5, clip_on=False,
          fill=True, alpha=1, linewidth=1.5)
    g.map(sns.kdeplot, "x", clip_on=False, color="w", lw=2, bw_adjust=.5)
    g.map(plt.axhline, y=0, lw=2, clip_on=False)
    
    
    # Define and use a simple function to label the plot in axes coordinates
    def label(x, color, label):
        ax = plt.gca()
        ax.text(0, .2, label, fontweight="bold", color=color,
                ha="left", va="center", transform=ax.transAxes)
    
    
    g.map(label, "x")
    
    # Set the subplots to overlap
    g.fig.subplots_adjust(hspace=-.25)
    
    # Remove axes details that don't play well with overlap
    g.set_titles("")
    g.set(yticks=[])
    g.despine(bottom=True, left=True)
    
    plt.show()
    

    并创建以下图表

    【讨论】:

    • 有没有办法在地图上绘制这个?例如,如果我在问题的示例地图中使用same data
    猜你喜欢
    • 2021-01-07
    • 2010-09-15
    • 2010-09-19
    • 2017-11-14
    • 1970-01-01
    • 1970-01-01
    • 2011-07-24
    • 2011-03-18
    • 2010-10-29
    相关资源
    最近更新 更多