【问题标题】:Decrease index-ticks frequency降低指数报价频率
【发布时间】:2020-09-03 16:19:32
【问题描述】:

我有这个样本数据:

import matplotlib.pyplot as plt
from matplotlib.patches import Patch
import seaborn as sns
import pandas as pd

df = pd.DataFrame({'AAAAAAAAAAAAAAAAAAAA': np.random.choice([False,True], 100000),
                   'BBBBBBBBBBBBBBBBBBBB': np.random.choice([False,True], 100000),
                   'CCCCCCCCCCCCCCCCCCCC': np.random.choice([False,True], 100000)},
                  index= np.random.choice([202006,202006, 202006,202005,202005,202005,202004,202004,202003], 100000)).sort_index(ascending=False)

有了这个情节:

fig, ax = plt.subplots(figsize=(5, 6))
cmap = sns.mpl_palette("Set2", 2)
sns.heatmap(data=df, cmap=cmap, cbar=False)
plt.xticks(rotation=90, fontsize=10)
plt.yticks(rotation=0, fontsize=10)

legend_handles = [Patch(color=cmap[True], label='Missing Value'),  # red
                  Patch(color=cmap[False], label='Non Missing Value')]  # green
plt.legend(handles=legend_handles, ncol=2, bbox_to_anchor=[0.5, 1.02], loc='lower center', fontsize=8, handlelength=.8)
plt.tight_layout()
plt.show()

由于变量名称的长度而发生重叠(我无法更改它们,因为它们在我的真实情节中提供了丰富的信息)。所以,我需要减少 y 刻度的频率,它可能是每个值两个刻度(当月份变化时),或者只是? 消除您在上图中看到的重叠。这个图的 y-ticks 需要清楚地显示下个月的开始和结束时间(202006 表示 2020 年 6 月),因为有了我拥有的真实数据,我可以看到整整一个月的数据是否丢失(或更多月)适用于任何变量。

我发现的所有可能的适应性解决方案都基于当刻度来自列时:Change tick frequencyadding space between ticks labelsincrease spacing between ticks 等。但我仍在为任何适应而苦苦挣扎。

有什么建议吗?

注意:您不能增加/减少图形的大小。

【问题讨论】:

    标签: python pandas matplotlib seaborn


    【解决方案1】:

    创建您的 DataFrame 并稍作修正,即设置数字 元素作为变量(n):

    n = 100000
    df = pd.DataFrame({'AAAAAAAAAAAAAAAAAAAA': np.random.choice([False,True], n),
                       'BBBBBBBBBBBBBBBBBBBB': np.random.choice([False,True], n),
                       'CCCCCCCCCCCCCCCCCCCC': np.random.choice([False,True], n)},
        index = np.random.choice([202006,202006, 202006,202005,202005,202005,
            202004,202004,202003], n)).sort_index(ascending=False)
    

    然后运行您的绘图代码并进行另外 2 处更正,即:

    • 设置yLabelNo = 10y个标签的数量),
    • yticklabels=n // yLabelNo 传递给sns.heatmap

    所以代码是:

        yLabelNo = 10
        fig, ax = plt.subplots(figsize=(5, 6))
        cmap = sns.mpl_palette("Set2", 2)
        sns.heatmap(data=df, cmap=cmap, cbar=False, yticklabels=n // yLabelNo)
        plt.xticks(rotation=90, fontsize=10)
        plt.yticks(rotation=0, fontsize=10)
        legend_handles = [Patch(color=cmap[True], label='Missing Value'),  # red
                          Patch(color=cmap[False], label='Non Missing Value')]  # green
        plt.legend(handles=legend_handles, ncol=2, bbox_to_anchor=[0.5, 1.02],
            loc='lower center', fontsize=8, handlelength=.8)
        plt.tight_layout()
        plt.show()
    

    结果是:

    如果您愿意,可以尝试使用 yLabelNo 的其他(可能更小)值。

    【讨论】:

    • 由于数据框无法更改(您认为我是否应该指定可能?)我使用了您的解决方案并提供了一个小解决方法:yticklabels=len(df)//yLabelNo
    • 是的,在这种情况下,不需要 n
    猜你喜欢
    • 2019-11-13
    • 1970-01-01
    • 2013-02-02
    • 1970-01-01
    • 1970-01-01
    • 2021-04-09
    • 2014-02-06
    • 2023-04-01
    • 1970-01-01
    相关资源
    最近更新 更多