【发布时间】: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 frequency、adding space between ticks labels、increase spacing between ticks 等。但我仍在为任何适应而苦苦挣扎。
有什么建议吗?
注意:您不能增加/减少图形的大小。
【问题讨论】:
标签: python pandas matplotlib seaborn