def YingBian2_image(): # 读取数据表格 file_path = r'F:\Glaucoma detection\Yingbian_file' filenames = os.listdir(file_path) Index = 0 c = [] for filename in filenames: print(filename) if Index == 0: c = 'r' s = '2000' elif Index == 1: c = 'g' s = '3300' else: c = 'b' s = '6000' Index += 1 data = pd.read_csv(file_path + '/' + filename).values surf = data[:, :2] + data[:, 3:] fig = plt.figure(figsize=(10, 3.8)) # 设置整个图的背景颜色为黑色 fig.patch.set_facecolor('black') plt.scatter(surf[:, 0], surf[:, 1], c=np.linalg.norm(data[:, 3:], ord=2, axis=1), cmap='rainbow') plt.colorbar() plt.title('压力值' + s, fontsize=14) plt.rcParams['axes.unicode_minus'] = False # 保存散点图到文件 plt.savefig("F:/Glaucoma detection/Yingbian_image/scatter_" + s + ".png", dpi=1000) plt.show() 这个怎么讲背景全变为黑色
时间: 2024-01-23 11:01:52 浏览: 23
在这个函数中,你可以在 `fig = plt.figure(figsize=(10, 3.8))` 下面添加一行代码 `fig.patch.set_facecolor('black')`,来设置整个图的背景颜色为黑色,具体代码如下:
```
import matplotlib.pyplot as plt
import numpy as np
import os
import pandas as pd
def YingBian2_image():
# 读取数据表格
file_path = r'F:\Glaucoma detection\Yingbian_file'
filenames = os.listdir(file_path)
Index = 0
c = []
for filename in filenames:
print(filename)
if Index == 0:
c = 'r'
s = '2000'
elif Index == 1:
c = 'g'
s = '3300'
else:
c = 'b'
s = '6000'
Index += 1
data = pd.read_csv(file_path + '/' + filename).values
surf = data[:, :2] + data[:, 3:]
fig = plt.figure(figsize=(10, 3.8))
fig.patch.set_facecolor('black') # 设置整个图的背景颜色为黑色
plt.scatter(surf[:, 0], surf[:, 1], c=np.linalg.norm(data[:, 3:], ord=2, axis=1), cmap='rainbow')
plt.colorbar()
plt.title('压力值' + s, fontsize=14)
plt.rcParams['axes.unicode_minus'] = False
plt.savefig("F:/Glaucoma detection/Yingbian_image/scatter_" + s + ".png", dpi=1000)
plt.show()
```
注意该方法需要在 `plt.show()` 之前调用。