matplotlib 将标题添加到subplot列中suppitle下面

irtuqstp  于 2023-05-18  发布在  其他
关注(0)|答案(2)|浏览(150)

有没有一种简单的方法可以添加到我的原始代码中,以便我可以在子图的两列中添加另一个标题?例如像下面图片中所示的粉红色区域中的某处。
有人建议我看看这个post的解决方案,但我想看看是否有一个方法不使用for循环

我的代码:

import cv2
import numpy as np
import matplotlib.pyplot as plt

path = 'R:\\Temp\\xx\\'
path1 = 'R:\\Temp\\xx\\'

def Hue(im_file):
    im = cv2.imread(im_file)
    im = cv2.cvtColor(im, cv2.COLOR_BGR2HSV_FULL) # Get Hue value= range[0,360]
    im1 = im[776, 402]
    Hue = im1[0]
    return Hue

def Saturation(im_file):
    im = cv2.imread(im_file)
    im = cv2.cvtColor(im, cv2.COLOR_BGR2HSV_FULL) #return Saturation value = range[0,255]
    im1 = im[776, 402]
    Saturation = im1[1]
    return Saturation

def Value(im_file):
    im = cv2.imread(im_file)
    im = cv2.cvtColor(im, cv2.COLOR_BGR2HSV_FULL) #return Value(Brightness) value = range[0,255]
    im1 = im[776, 402]
    Value = im1[2]
    return Value   

def BlueComponent(im_file):
    im = cv2.imread(im_file) #return blue value
    im1 = im[776, 402]
    b = im1[0]
    return b

def GreenComponent(im_file):
    im = cv2.imread(im_file) #return green value 
    im1 = im[776, 402]
    g = im1[1]
    return g

def RedComponent(im_file): #return red value 
    im = cv2.imread(im_file)
    im1 = im[776, 402]
    r = im1[2]
    return r

myHueList = []
mySaturationList = []
myValueList = []
myBlueList = []
myGreenList = []
myRedList = []
myList = []
num_images = 99 # number of images

dotPos = 0
for i in range(1770, 1869): # loop to auto-generate image names and run prior function 
    image_name = path + 'Cropped_Aligned_IMG_' + str(i) + '.png' # for loop runs from image number 1770 to 1868
    myHueList.append(Hue(image_name))
    mySaturationList.append(Saturation(image_name))
    myValueList.append(Value(image_name))
    myBlueList.append(BlueComponent(image_name))
    myGreenList.append(GreenComponent(image_name))
    myRedList.append(RedComponent(image_name))
    myList.append(dotPos)
    dotPos = dotPos + 0.5

print(myBlueList)
print(myGreenList)
print(myRedList)
print(myHueList)
print(mySaturationList)
print(myValueList)
print(myList)


for k in range(1770,1869):
    a = 'Cropped_Aligned_IMG_' + str(k)
    image_name = path + a + '.png'
    img_file = cv2.imread(image_name)

x = myList
y = myBlueList
y1 = myGreenList
y2 = myRedList
y3 = myHueList
y4 = mySaturationList
y5 = myValueList

plt.axes([0.1, 0.1, 1, 1])
plt.suptitle('BGR & HSV Color Decimal Code Against Function of Time(Hours)', fontsize=14, fontweight='bold')
plt.subplot(3,2,1)
plt.plot(x, y, 'b.-')
plt.title('Blue Component Color Decimal Code')
plt.xlabel('Time(Hours)')
plt.ylabel('Colour Code')

plt.subplot(3,2,3)
plt.plot(x, y1, 'g.-')
plt.title('Green Component Color Decimal Code')
plt.xlabel('Time(Hours)')
plt.ylabel('Colour Code')

plt.subplot(3,2,5)
plt.plot(x, y2, 'r.-')
plt.title('Red Component Color Decimal Code')
plt.xlabel('Time(Hours)')
plt.ylabel('Colour Code')

plt.subplot(3,2,2)
plt.plot(x, y3, 'b.-')
plt.title('Hue Component HSV Color Decimal Code')
plt.xlabel('Time(Hours)')
plt.ylabel('Colour Code')

plt.subplot(3,2,4)
plt.plot(x, y4, 'g.-')
plt.title('Saturation Component HSV Color Decimal Code')
plt.xlabel('Time(Hours)')
plt.ylabel('Colour Code')

plt.subplot(3,2,6)
plt.plot(x, y5, 'r.-')
plt.title('Value Component HSV Color Decimal Code')
plt.xlabel('Time(Hours)')
plt.ylabel('Colour Code')
plt.subplots_adjust(hspace = 0.5)
plt.show()
yfwxisqw

yfwxisqw1#

我可以想象在顶部创建一个空行的子图,每个子图都有自己的标题,将作为一个列标题。

import matplotlib.pyplot as plt
import numpy as np
plt.rcParams["figure.figsize"] = 6,8
colors = plt.rcParams["axes.prop_cycle"].by_key()["color"]

x = np.linspace(-3,3)
y = np.random.randn(len(x),6)

fig, axes = plt.subplots(ncols=2, nrows=3+1, gridspec_kw={"height_ratios":[0.02,1,1,1]})
fig.suptitle('Some long super title for the complete figure', 
             fontsize=14, fontweight='bold')
for i, ax in enumerate(axes.flatten()[2:]):
    ax.plot(x,y[:,i], color=colors[i%6])
    ax.set_title("Title {}".format(i+1))

for i, ax in enumerate(axes.flatten()[:2]):
    ax.axis("off")
    ax.set_title("Columntitle {}".format(i+1), fontweight='bold')

fig.subplots_adjust(hspace=0.5, bottom=0.1)
plt.show()

注意,我在这里使用循环是为了简化事情。当然,如果出于任何原因需要,您也可以为每个子图单独键入命令。

ubby3x7f

ubby3x7f2#

对于遇到这个问题的其他人,我们可以简单地使用plt.figtext()直接在需要的地方添加文本。
在这种情况下,它将是类似于:

plt.figtext(0.33, 0.8, 'BGR')
plt.figtext(0.67, 0.8, 'HSV')

我猜的是y值(0.8),所以我们需要用x,y坐标进行定位。figtext()也可以使用字体dict来处理格式化。

相关问题