Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Add a verticle line between matplotlib subplots

I have this subplots here Hello, I have made this subplots, here the images are in 4 columns, my idea is to compare them in a pair of two, that is, 1st column with 2nd and 3rd with 4th. But here it is bit confusing the way it looks right now. Is it possible to add a vertical line between 2nd and 3rd column somehow?? So that it seems like first two column are together and the other two are pairs? Is there any possible way to do it?

import matplotlib.pyplot as plt

indexes = [1000,1001]

indexes2 = [1010,1011]
columns = 4
rows = len(indexes)
f, axarr = plt.subplots(rows, columns,figsize=(10,10))
k = 0
for i in range(0, rows):
    axarr[i, 0].set_title(str(indexes[k])+"-Patch 1",fontsize=15)    
    axarr[i, 1].set_title(str(indexes[k])+"-Patch 2",fontsize=15)

    axarr[i, 2].set_title(str(indexes2[k])+"-Patch 1",fontsize=15)    
    axarr[i, 3].set_title(str(indexes2[k])+"-Patch 2",fontsize=15)

    k = k+1
    axarr[i, 0].set_xticks([])
    axarr[i, 1].set_xticks([])
    axarr[i, 0].set_yticks([])
    axarr[i, 1].set_yticks([])

    axarr[i, 2].set_xticks([])
    axarr[i, 3].set_xticks([])
    axarr[i, 2].set_yticks([])
    axarr[i, 3].set_yticks([])

plt.tight_layout()

I have added the code how I got the subplots up. If it is helpful in answering the question. Thank you for your time. :)

If the question is not clear, I need a line like this, I added in image editor in the following image. Image with line that I need

like image 535
Haramoz Avatar asked Aug 25 '26 00:08

Haramoz


1 Answers

Line

Adding a line is as easy as

line = plt.Line2D((.5,.5),(.1,.9), color="k", linewidth=3)
fig.add_artist(line)

enter image description here

import matplotlib.pyplot as plt
import numpy as np

a = np.random.rand(10,10,8)
columns = 4
rows = a.shape[2]//columns

fig, axarr = plt.subplots(rows, columns)
fig.subplots_adjust(left=0.1, right=0.9,  wspace=0.4)

for i, ax in enumerate(axarr.flat):
    img = a[:,:,i]
    ax.imshow(img)
    ax.set_title("-Patch {}".format(i))    

line = plt.Line2D((.5,.5),(.1,.9), color="k", linewidth=3)
fig.add_artist(line)

plt.show()

For a more sophisticated solution with lines, see Draw a separator or lines between subplots

Space

However, potentially you would rather adjust the spacing between the plots.

import matplotlib.pyplot as plt
from matplotlib.gridspec import GridSpec
import numpy as np

a = np.random.rand(10,10,8)
columns = 4
rows = a.shape[2]//columns


fig = plt.figure()
axarr1 = fig.subplots(2,2, gridspec_kw=dict(left=0.05, right=0.43, wspace=0.4))
axarr2 = fig.subplots(2,2, gridspec_kw=dict(left=0.57, right=0.95, wspace=0.4))


for i, ax in enumerate(axarr1.flat):
    img = a[:,:,i]
    ax.imshow(img)
    ax.set_title("-Patch {}".format(i)) 

for i, ax in enumerate(axarr2.flat):
    img = a[:,:,i+4]
    ax.imshow(img)
    ax.set_title("-Patch {}".format(i+4)) 


plt.show()

This allows to visually separate the two groups of subplots without having some black line in the plot.

enter image description here

like image 131
ImportanceOfBeingErnest Avatar answered Aug 27 '26 13:08

ImportanceOfBeingErnest



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!