Home > Net >  Matplotlib - custom grid layering
Matplotlib - custom grid layering

Time:06-14

I have a requirement to display scatter plot as follows:

enter image description here

here is my code:

# Define Data

x = result.iloc[:,6] #R Score
y = result.iloc[:,11] #FM Score
 
# Create Figure

fig, ax = plt.subplots()
ax.set_ylabel('frequency & monetary score')
ax.set_xlabel('recency score')
ax.scatter(x, y,c=z, cmap='viridis', alpha=0.5)
ax.legend(*sc.legend_elements(), title="CLUSTER",loc='lower center', 
          bbox_to_anchor=(0.5, 1.05),ncol=5, fancybox=True, shadow=True)
ax.grid(True)
plt.show()

output: enter image description here

the problem is, i dont know how to create a custom layering on top of certain grid position (example Champions layers for x-axis:4-5 and y-axis:6-10)

is this possible using matplotlib?

thank you

CodePudding user response:

For those who stumbled same problem, i found the solution, using image grid

# Define Data

x = result.iloc[:,6] #R Score
y = result.iloc[:,11]/2 #FM Score
 
extent = (0,5, 0, 5)    
# Create Figure

fig, ax = plt.subplots()
ax.set_ylabel('frequency & monetary score')
ax.set_xlabel('recency score')
ax.scatter(x, y,c=z, cmap='viridis', alpha=0.5,zorder=2)
ax.legend(*sc.legend_elements(), title="CLUSTER",loc='lower center', 
          bbox_to_anchor=(0.5, 1.05),ncol=5, fancybox=True, shadow=True)
ax.grid(True)
layer_img=plt.imread("assets/rfm.JPG")
plt.imshow(layer_img,zorder=1, extent=extent)
plt.show()

result:

enter image description here

  • Related