Issue
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
The expected output is
Solution
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()
Answered By - x pie
0 comments:
Post a Comment
Note: Only a member of this blog may post a comment.