#功图批量绘制 import os import numpy as np import pandas as pd from PIL import Image from matplotlib import pyplot as plt plt.figure(figsize=(4, 2), dpi=50, frameon=False) ax = plt.axes([0, 0, 1, 1]) grey = plt.get_cmap('Greys') seismic = plt.get_cmap('bwr') datapath = "G:/功图excel/0" conds = os.listdir(datapath) for cond in conds: data = pd.read_csv("G:/功图excel/0/" + cond) os.mkdir(r"G:/功图/0/" + cond[:-4]) # print(data) # 首先将pandas读取的数据转化为array data = np.array(data) # 然后转化为list形式 data = data.tolist() # print(data) n = 0 for i in data: if np.isnan(i).any(): # 检查数据是否包含 NaN 值 continue # 如果包含,则跳过该迭代 WY = i[0:200] ZH = i[200:400] # print(len(WY),len(ZH)) plt.plot(WY[0: 100], ZH[0: 100], color=seismic(5 / 5.0), lw=3) plt.plot(WY[100: 200], ZH[100: 200], color=seismic(0 / 5.0), lw=3) plt.xticks([]) plt.yticks([]) ax.spines['right'].set_visible(False) ax.spines['top'].set_visible(False) ax.spines['left'].set_visible(False) ax.spines['bottom'].set_visible(False) Y_ALL = [] Y_ALL.extend(list(map(float, ZH))) Y_MAX, Y_MIN = np.max(Y_ALL), np.min(Y_ALL) ax.set_ylim(np.min([0, Y_MIN - (Y_MAX - Y_MIN) * 0.1]), Y_MAX + (Y_MAX - Y_MIN) * 0.1) # plt.axis('off') plt.savefig("G:/功图/0/" + cond[:-4] + '/' + str(n), dpi=50) # plt.savefig(newpath + "GT/" + cond + "/" + data['IMGNAME'][i], dpi=50) plt.clf() # plt.show() n = n + 1,这段画图代码使用了 ax.spines 属性去掉边框,但是画出来的图怎么还有边框
时间: 2024-03-27 18:35:01 浏览: 106
Matplotlib.pyplot 三维绘图的实现示例
根据你提供的代码,使用 `ax.spines` 属性去掉边框是可以去掉边框的。但是,如果你还想要进一步去掉图像周围的空白边距,可以使用 `plt.tight_layout()` 函数,将图像周围的空白边距调整为最小值。示例代码如下:
```python
import os
import numpy as np
import pandas as pd
from PIL import Image
from matplotlib import pyplot as plt
plt.figure(figsize=(4, 2), dpi=50, frameon=False)
ax = plt.axes([0, 0, 1, 1])
grey = plt.get_cmap('Greys')
seismic = plt.get_cmap('bwr')
datapath = "G:/功图excel/0"
conds = os.listdir(datapath)
for cond in conds:
data = pd.read_csv("G:/功图excel/0/" + cond)
os.mkdir(r"G:/功图/0/" + cond[:-4])
data = np.array(data)
data = data.tolist()
n = 0
for i in data:
if np.isnan(i).any():
continue
WY = i[0:200]
ZH = i[200:400]
plt.plot(WY[0: 100], ZH[0: 100], color=seismic(5 / 5.0), lw=3)
plt.plot(WY[100: 200], ZH[100: 200], color=seismic(0 / 5.0), lw=3)
plt.xticks([])
plt.yticks([])
ax.spines['right'].set_visible(False)
ax.spines['top'].set_visible(False)
ax.spines['left'].set_visible(False)
ax.spines['bottom'].set_visible(False)
Y_ALL = []
Y_ALL.extend(list(map(float, ZH)))
Y_MAX, Y_MIN = np.max(Y_ALL), np.min(Y_ALL)
ax.set_ylim(np.min([0, Y_MIN - (Y_MAX - Y_MIN) * 0.1]), Y_MAX + (Y_MAX - Y_MIN) * 0.1)
plt.savefig("G:/功图/0/" + cond[:-4] + '/' + str(n), dpi=50)
plt.clf()
n = n + 1
# 调整图像周围的空白边距为最小值
plt.tight_layout()
```
在上面的代码中,我添加了 `plt.tight_layout()` 函数来调整图像周围的空白边距为最小值。
阅读全文