簡體   English   中英

matplotlib 顯示不完整的刻度標簽

[英]matplotlib show incomplete tick labels

我需要使用 Matplotlib 來繪制帶有中文刻度標簽的頭圖。 但結果顯示不完整的刻度標簽如下。 我不知道為什么會這樣。 我嘗試更改其他中文字體但不起作用。 如何解決?

import numpy as np
import matplotlib.pyplot as plt
from pylab import mpl
mpl.rcParams['font.sans-serif'] = ['STZhongsong']    
mpl.rcParams['axes.unicode_minus'] = True          

def heatmap(data, row_labels, col_labels, ax=None,
            cbar_kw={}, cbarlabel="", **kwargs):

    if not ax:
        ax = plt.gca()

    im = ax.imshow(data, **kwargs)

    cbar = ax.figure.colorbar(im, ax=ax, **cbar_kw)
    cbar.ax.set_ylabel(cbarlabel, rotation=-90, va="bottom")

    ax.set_xticks(np.arange(data.shape[1]))
    ax.set_yticks(np.arange(data.shape[0]))

    ax.set_xticklabels(col_labels)
    ax.set_yticklabels(row_labels)

    ax.tick_params(top=True, bottom=False,
                   labeltop=True, labelbottom=False)

    plt.setp(ax.get_xticklabels(), rotation=-20, ha="right",
             rotation_mode="anchor")

    for edge, spine in ax.spines.items():
        spine.set_visible(False)

    ax.set_xticks(np.arange(data.shape[1]+1)-.5, minor=True)
    ax.set_yticks(np.arange(data.shape[0]+1)-.5, minor=True)
    ax.grid(which="minor", color="w", linestyle='-', linewidth=3)
    ax.tick_params(which="minor", bottom=False, left=False)

    return im, cbar

if __name__ == "__main__":
    x,y = list("你這是干什么啦?"),list("你要吃什么?")
    s = np.random.random([len(x), len(y)])
    fig, ax = plt.subplots()

    im, cbar = heatmap(s, x, y, ax=ax,
                       cmap="YlGn", cbarlabel="attention scores")

    fig.tight_layout()
    plt.savefig("test", dpi=300, bbox_inches = 'tight')
    plt.show()

在此處輸入圖像描述

 import numpy as np import matplotlib.pyplot as plt import matplotlib.font_manager as mfm import matplotlib.pyplot as plt def heatmap(data, row_labels, col_labels, ax=None, cbar_kw={}, cbarlabel="", **kwargs): if not ax: ax = plt.gca() im = ax.imshow(data, **kwargs) cbar = ax.figure.colorbar(im, ax=ax, **cbar_kw) cbar.ax.set_ylabel(cbarlabel, rotation=-90, va="bottom") ax.set_xticks(np.arange(data.shape[1])) ax.set_yticks(np.arange(data.shape[0])) font_path = "/Downloads/ZCOOLXiaoWei-Regular.ttf" prop = mfm.FontProperties(fname=font_path) ax.set_xticklabels(col_labels,fontproperties=prop,fontsize=50) ax.set_yticklabels(row_labels,fontproperties=prop,fontsize=50) ax.tick_params(top=True, bottom=False, labeltop=True, labelbottom=False) plt.setp(ax.get_xticklabels(), rotation=-20, ha="right", rotation_mode="anchor") for edge, spine in ax.spines.items(): spine.set_visible(False) ax.set_xticks(np.arange(data.shape[1]+1)-.5, minor=True) ax.set_yticks(np.arange(data.shape[0]+1)-.5, minor=True) ax.grid(which="minor", color="w", linestyle='-', linewidth=3) ax.tick_params(which="minor", bottom=False, left=False) return im, cbar if __name__ == "__main__": x,y = list("你這是干什么啦"),list("你這是干什么啦") s = np.random.random([len(x), len(y)]) fig, ax = plt.subplots(figsize=(10,10)) im, cbar = heatmap(s, x, y, ax=ax, cmap="YlGn", cbarlabel="attention scores") fig.tight_layout() plt.savefig("test", dpi=300, bbox_inches = 'tight') plt.show()

我對代碼做了一些改動,使用了 matplotlib.font_manager 來渲染中文字體。 在此處輸入圖像描述

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM