Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Show incomplete tick labels when using mixed chinese and english characters #15820

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
yaoxiaoyuan opened this issue Dec 3, 2019 · 2 comments
Closed

Comments

@yaoxiaoyuan
Copy link

yaoxiaoyuan commented Dec 3, 2019

Bug report

The figure shows incomplete tick labels as below when I use Matplotlib to draw headmaps with chinese tick labels. Note this only happens when using Chinese character and punctuation(or English alphabet) in tick labels at the same time.

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()

The above figure can be found here: https://i.stack.imgur.com/HwVp3.png

  • Operating system: windows 10
  • Matplotlib version: 3.0.3
  • Matplotlib backend: Qt5Agg
  • Python version: 3.7.3
@QuLogic
Copy link
Member

QuLogic commented Dec 3, 2019

Looks like #8609, fixed by #14705 in 3.2.0. Please try with the latest version.

@anntzer
Copy link
Contributor

anntzer commented Dec 9, 2019

I can confirm locally this was fixed by #14705. Feel free to request a reopen if not.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

4 participants