背景:
win10, anaconda 4.8.3, python3.8.3 matplotlib画图, 小程序运行时,图像中坐标轴“负号”显示不全,(我这里)表现为“方框”,图片见下面。 关闭图像后, 有的情况下会有Glyph8722 warning提示,有的没有。
办法
这里的错误提示,只给出Glyph 8722 missing from current font …,不容易联系到和坐标轴的负号显示相关。比如下面代码段中#subplot(233) 这个图的代码画图后,会有这个提示。 有的情况下,负号显示不正常,但是没有warning 给出。这种情况下,目标比较明确,较为容易。比如代码中,屏蔽了#subplot(233) 这个图的代码段后,就是这种情况。 两种情况下,都可以通过更改 axes.unicode_minus 更正。 plt.rcParams[‘axes.unicode_minus’]=False, 或 mpl.rcParams[‘axes.unicode_minus’]=False plt 为 matplotlib.pyplot 的简写; mpl 为 matplotlib的简写。 例子见下面。
图片和例子
import matplotlib. pyplot as plt
import numpy as np
fig, ax= plt. subplots( 2 , 3 )
colors= [ "#8dd3c7" , "#ffffb3" , "#bebada" ]
ax[ 0 , 0 ] . bar( [ 1 , 2 , 3 ] , [ 0.6 , 0.2 , 0.8 ] , color= colors, width= 0.5 , hatch= '///' , align= 'center' )
ax[ 0 , 0 ] . errorbar( [ 1 , 2 , 3 ] , [ 0.6 , 0.2 , 0.8 ] , yerr= 0.1 , capsize= 0 , ecolor= "#377eb8" , fmt= 'o:' )
ax[ 0 , 0 ] . set_ylim( 0 , 1.0 )
ax[ 0 , 1 ] . errorbar( [ 1 , 2 , 3 ] , [ 20 , 30 , 36 ] , xerr= 2 , ecolor= "#4daf4a" , elinewidth= 2 , fmt= 's' , label= 'ETN' )
ax[ 0 , 1 ] . legend( loc= 3 , fancybox= True , shadow= True , fontsize= 10 , borderaxespad= 0.4 )
ax[ 0 , 1 ] . set_ylim( 10 , 40 )
ax[ 0 , 1 ] . set_xlim( - 2 , 6 )
ax[ 0 , 2 ] . grid( ls= ":" , lw= 1 , color= 'grey' , alpha= 0.5 )
x3= np. arange( 1 , 10 , 0.5 )
y3= np. cos( x3)
ax[ 0 , 2 ] . stem( x3, y3, basefmt= 'r-' , linefmt= 'b-' , markerfmt= 'bo' , label= 'liftesignal' , use_line_collection= True )
ax[ 0 , 2 ] . legend( loc= 2 , fontsize= 8 , frameon= False , borderpad= 0.0 , borderaxespad= 0.6 )
ax[ 0 , 2 ] . set_xlim( 0 , 11 )
ax[ 0 , 2 ] . set_ylim( - 1.1 , 1.1 )
plt. show( )