脚本之家,脚本语言编程技术及教程分享平台!
分类导航

Python|VBS|Ruby|Lua|perl|VBA|Golang|PowerShell|Erlang|autoit|Dos|bat|

服务器之家 - 脚本之家 - Python - 完美解决matplotlib子图坐标轴重叠问题

完美解决matplotlib子图坐标轴重叠问题

2021-10-11 08:36自然卷卷、 Python

这篇文章主要介绍了完美解决matplotlib子图坐标轴重叠问题,具有很好的参考价值,希望对大家有所帮助。一起跟随小编过来看看吧

绘制matplotlib多个图形于一张图时往往会出现相邻图坐标轴重叠的现象

此时只须在汇完所有子图后加上以下代码即可

?
1
plt.tight_layout()

补充:解决matplotlib横坐标日期过长导致的重叠、无法完全显示问题(换行)

该方法较为繁杂,主要是将年月日和时间分开,通过使用plt.text根据坐标位置将年月日重新加上,前面是为了进行时间转化,

有用的代码为

?
1
> plt.text(zb[link],-1.2,year,ha='center')#需要根据自己的位置调整Y的坐标即调整-1

完整程序:

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
import matplotlib.pyplot as plt
import pandas as pd
import numpy as np
plt.rcParams['font.sans-serif']=['SimHei'] #用来正常显示中文标签
plt.rcParams['axes.unicode_minus']=False #用来正常显示负号
time1 =input("输入一个时间(时、分、秒以空格分割):")
year = input('请输入年份,格式如 2019-8-30 :')
n = input('请输入X轴分段数:')
old_time_x = []
time1List = time1.split(" ")
time1List = [int(x) for x in time1List]
'''时间转化'''
shi = time1List[0]
fen = time1List[1]
miao = time1List[2]
for x in range(eval(n)+1):
    time_x = '%02d:%02d:%02d'%(shi,fen,miao)
    old_time_x.append(time_x)
    miao += 60
    while miao >= 60:
        miao = miao - 60
        fen += 1
        while fen >= 60:
            fen = fen -60
            shi += 1
            while shi > 24:
                shi = shi - 24
print(old_time_x)
x = np.arange(0,10,1)
y = np.arange(0,10,1)
fig = plt.figure(figsize=(12,6))
plt.xticks(np.linspace(0,9,eval(n)),old_time_x,rotation=0)
zb = np.linspace(0,9,eval(n))
print(zb)
for link in range(len(zb)):
    plt.text(zb[link],-1.2,year,ha='center')#需要根据自己的位置调整Y的坐标即调整-1
plt.plot(x,y)

完美解决matplotlib子图坐标轴重叠问题

以上为个人经验,希望能给大家一个参考,也希望大家多多支持服务器之家。如有错误或未考虑完全的地方,望不吝赐教。

原文链接:https://blog.csdn.net/weixin_44778883/article/details/90635997

延伸 · 阅读

精彩推荐