0

I have a function that produces a matplotlib map. I then want to overlay a seaborn heat map on top of this map, and have both maps exactly the same size on top of each other, while being able to see the details of both maps. Is it possible? Please see my code below.

def draw_map():
    fig=plt.figure()
    fig.set_size_inches(14.5, 8.8)
    ax=fig.add_subplot(1,1,1)
    
    #Map Outline & Centre Line
    plt.plot([0,0],[0,88], color="black")
    plt.plot([0,145],[88,88], color="black")
    plt.plot([145,145],[88,0], color="black")
    plt.plot([145,0],[0,0], color="black")

    ly97 = [39,49] 
    lx97 = [72.5,72.5]
    plt.plot(lx97,ly97,color="black")
    
    seaborn.heatmap(data)
    plt.ylim(0, 88)
    plt.xlim(0, 145)
                    
    #Display Map
    plt.show()
    

For some reason the seaborn heatmap appears tiny in comparison to the matplotlib map. The data in the seaborn heatmap contains values between 0 and 1 only, if this helps. Thanks in advance.

0

1 Answer 1

2

When drawing an MxN array as a heatmap, seaborn creates it with an x-dimension from 0 to N-1 and a y-dimension from 0 to M-1. There doesn't seem to be a way to provide your own dimensions. As seaborn calls matplotlib's pcolormesh() to draw the heatmap, you can call it directly. pcolormesh() does accept parameters to set the x and y dimensions.

The example below uses the standard "object-oriented" interface for pyplot. Alpha and green lines are used, to get some more contrast between the lines and the heatmap with seaborn's default colormap.

import matplotlib.pyplot as plt
import seaborn as sns
import numpy as np

fig, ax = plt.subplots(figsize=(14.5, 8.8))

# Map Outline & Centre Line
ax.plot([0, 0], [0, 88], color="lime", lw=3)
ax.plot([0, 145], [88, 88], color="lime", lw=3)
ax.plot([145, 145], [88, 0], color="lime", lw=3)
ax.plot([145, 0], [0, 0], color="lime", lw=3)

ly97 = [39, 49]
lx97 = [72.5, 72.5]
ax.plot(lx97, ly97, color="lime", lw=3)

M = 20
N = 30
data = np.random.rand(M, N)
# sns.heatmap(data)
ax.pcolormesh(np.linspace(0, 145, N+1), np.linspace(0, 88, M+1), data, alpha=0.4,
              cmap=sns.color_palette("rocket", as_cmap=True))
# ax.set_ylim(0, 88)
# ax.set_xlim(0, 145)

plt.show()

example plot

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

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.