0

I am drawing multiple horizontal and vertical lines using ax.hlines() and ax.vlines() respectively. I want to assign values to these lines using the array P and the order of assignment is presented in the expected output.

import matplotlib as mpl
import matplotlib.pyplot as plt
from matplotlib.patches import Rectangle
import numpy as np

fig,ax = plt.subplots(1)
n=3
for i in range(0,n):
    for j in range(0,n):
        rect = mpl.patches.Rectangle((200+200*i,200+200*j),10*n, 10*n, linewidth=1, edgecolor='black', facecolor='black')
        ax.add_patch(rect)
        ax.hlines(200+200*i+5*n, 200, 200*n, zorder=0)
        ax.vlines(200+200*j+5*n, 200, 200*n, zorder=0)

ax.set_xlim(left = 0, right = 220*n)
ax.set_ylim(bottom = 0, top = 220*n)
plt.show()

#########################################
P=np.array([[1.9],
       [4.9],
       [6.1],
       [8.2],
       [1.8],
       [5.8],
       [9.7],
       [7.3],
       [8.9],
       [2.5],
       [9.9],
       [0.7]])

#########################################

The current output is

enter image description here

The expected output is

enter image description here

9
  • Can you explain the values in array P? Commented Jul 14, 2022 at 3:33
  • The array P is mentioned at the bottom of the code. Commented Jul 14, 2022 at 3:51
  • I mean the colormap. Commented Jul 14, 2022 at 4:14
  • The colormap in the expected output is for illustration only. Based on the array P, it will be in the range 0-10 since the min is 0.7 and max is 9.9 Commented Jul 14, 2022 at 5:15
  • You are expecting a square because in your code every edge should be 200. But in your example.jpg, they seems to be not a square. As@Davide_sd said, it's because of overlapping. My code follows your order as you labeled from P[0]-P[11], and solves the overlapping. Commented Jul 15, 2022 at 2:55

2 Answers 2

1

Values bar is added following @Davide_sd.

I'm not sure if this sovles your problem.

import matplotlib as mpl
import matplotlib.pyplot as plt
from matplotlib.patches import Rectangle
import numpy as np
from matplotlib.colors import Normalize
from matplotlib import cm


fig,ax = plt.subplots(1)
n=3

P=np.array([[1.9],
       [4.9],
       [6.1],
       [8.2],
       [1.8],
       [5.8],
       [9.7],
       [7.3],
       [8.9],
       [2.5],
       [9.9],
       [0.7]])

color = cm.get_cmap('Blues')
norm = Normalize(vmin=0, vmax=10)
color_list = []
for i in range(len(P)):
    color_list.append(color(P[i]/10))
print(color_list)
id = 0
for j in range(0, n):
    for k in range(n-1):
        ax.hlines(200+200*(n-j-1)+5*n, 200*(k+1)+5*n, 200*(k+2)+5*n, zorder=0, colors=color_list[id])
        id += 1

    for i in range(0, n):
        rect = mpl.patches.Rectangle((200+200*i, 200+200*j), 10*n, 10*n, linewidth=1, edgecolor='black', facecolor='black')
        ax.add_patch(rect)
        if j < n-1:
            ax.vlines(200+200*i+5*n, 200*(n-1-j)+5*n, 200*(n-j)+5*n, zorder=0, colors=color_list[id])
            id += 1

cb = fig.colorbar(cm.ScalarMappable(cmap=color, norm=norm))
cb.set_label("Values")
ax.set_xlim(left = 0, right = 220*n)
ax.set_ylim(bottom = 0, top = 220*n)
plt.show()

And the ouput is like:enter image description here

![enter image description here

Sign up to request clarification or add additional context in comments.

7 Comments

The number of squares is fine now but as I increase n say from 3 to 4, I see 3 vertical lines not appearing at the bottom. I am looking for a generic code which works for many n. For n=4, the shape of P should be (24,1) which means 24 different values for 24 different lines (horizontal and vertical combined).
Got it, I will update later.
Try to change "if j <2" to "if j < n-1",I suppose it will work.
There's problem with n=4. It makes multiple lines at a time at some places. Instead I want it to take 1 line at a time and pick values according to P.
Ofcourse you are right. I will update.
|
1

You need to use a colormap, Normalize and ScalarMappable in order to create a colorbar.

Here is the procedure:

import matplotlib as mpl
import matplotlib.pyplot as plt
from matplotlib.patches import Rectangle
import matplotlib.cm as cm
from matplotlib.colors import Normalize
import numpy as np

#########################################
P=np.array([[1.9],
       [4.9],
       [6.1],
       [8.2],
       [1.8],
       [5.8],
       [9.7],
       [7.3],
       [8.9],
       [2.5],
       [9.9],
       [0.7]])

#########################################

# normalize the values. Values between 0 and 10 will be
# normalized to values from 0 and 1.
norm = Normalize(vmin=0, vmax=10)
Pnorm = norm(P)
# choose an appropriate colormap
cmap = cm.Blues

fig,ax = plt.subplots(1)
n=3
k = 0
for i in range(0,n):
    for j in range(0,n):
        rect = mpl.patches.Rectangle((200+200*i,200+200*j),10*n, 10*n, linewidth=1, edgecolor='black', facecolor='black')
        ax.add_patch(rect)
        # extract the color from the colormap
        ax.hlines(200+200*i+5*n, 200, 200*n, zorder=0, color=cmap(Pnorm[k]))
        ax.vlines(200+200*j+5*n, 200, 200*n, zorder=0, color=cmap(Pnorm[k]))
        k += 1

cb = fig.colorbar(ScalarMappable(cmap=cmap, norm=norm))
cb.set_label("Values")
ax.set_xlim(left = 0, right = 220*n)
ax.set_ylim(bottom = 0, top = 220*n)
plt.show()

There is a problem with the way you are currently plotting lines, as they are overlapping. You need to fix it!

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.