微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

Python,x轴标题与matplotlib中的刻度标签重叠

如何解决Python,x轴标题与matplotlib中的刻度标签重叠

我正在绘制图形,但是x轴标签在图形中不可见。

enter image description here

我试图通过添加

解决
String dirName = "dirName";
File file = new File("generatedQuestions" + File.separator + dirName);
if (file.mkdirs()) {
    String fileName = "fileName";
    try (PrintWriter writer = new PrintWriter(file + File.separator + fileName+".txt")) {
    }
    catch (FileNotFoundException x) {
        x.printstacktrace();
    }
}
else {
    System.out.println("'mkdirs()' Failed.");
}

相反,x标签将与股票代码标签重叠

enter image description here

如何进行调整以在绘图图中同时显示x轴标签和x行情标签


用于复制图形的完整代码

Path path = Paths.get("generatedQuestions",dirName);
try {
    Files.createDirectories(path);
    path = path.resolve(fileName + ".txt");
    PrintWriter writer = new PrintWriter(path.toFile());
}
catch (IOException x) {
    x.printstacktrace();
}

解决方法

您可以在matplotlib中使用“ Tight Layout”功能来解决该问题。

在绘制图形之前添加一行,其中h_pad将调整高度,w_pad将调整宽度。

# Adjust x-axis margins
plt.tight_layout(pad=0.4,w_pad=0.5,h_pad=5.0)

并删除此部分:

ax.xaxis.labelpad = -10 # Adjust x-axis label position

结果:

enter image description here

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。