为何Matplotlib生成的图表未显示标题与X轴标签?
Matplotlib散点图标题与X轴标签不显示的解决办法
你运行以下代码生成散点图时,图表的标题和X轴标签无法正常显示:
normalDistribution = np.random.normal(loc = 0.0, scale = 1.0, size = totalPoints) rows = 4 columns = 5 fig = plt.figure(figsize =(24, 24)) plt.subplots_adjust(wspace=0.3, hspace=0.4) # to adjust the spacing between subplots plt.subplot(rows, columns,1) plt.title = "Normal Dist" plt.xlabel = "Sequence #" plt.scatter(range(0, len(normalDistribution)), normalDistribution, c='green')
问题原因
你错误地把plt.title和plt.xlabel当成变量直接赋值字符串了,实际上它们是Matplotlib的函数,必须通过函数调用的方式传入文本参数才能生效。
修正后的代码
把赋值语句改成函数调用即可:
normalDistribution = np.random.normal(loc = 0.0, scale = 1.0, size = totalPoints) rows = 4 columns = 5 fig = plt.figure(figsize =(24, 24)) plt.subplots_adjust(wspace=0.3, hspace=0.4) # 调整子图间距 plt.subplot(rows, columns,1) plt.title("Normal Dist") # 改为函数调用 plt.xlabel("Sequence #") # 改为函数调用 plt.scatter(range(0, len(normalDistribution)), normalDistribution, c='green')
修正后,图表的标题和X轴标签就能正常显示了。
内容的提问来源于stack exchange,提问作者nicomp
相关产品推荐
相关产品推荐

