0

I have a figure showing the contourf plot and another showing a plot i've made earlier and I want to plot both on the same figure what should I do? Here is the code of my contourf plot:

import pylab as pl
from pylab import *
import xlrd
import math
import itertools
from matplotlib import collections as mc
import matplotlib.pyplot as plt
import copy as dc
import pyexcel
from pyexcel.ext import xlsx
import decimal

x_list = linspace(0, 99, 100)
y_list = linspace(0, 99, 100)
X, Y = meshgrid(x_list, y_list, indexing='xy')

Z = [[0 for x in range(len(x_list))] for x in range(len(y_list))]
for each_axes in range(len(Z)):
    for each_point in range(len(Z[each_axes])):
        Z[len(Z)-1-each_axes][each_point] = power_at_each_point(each_point, each_axes)

figure()
CP2 = contourf(X, Y, Z, cmap=plt.get_cmap('Reds'))
colorbar(CP2)
title('Coverage Plot')
xlabel('x (m)')
ylabel('y (m)')
show()

This is the code of my previously plotted plot:

lc = mc.LineCollection(lines, linewidths=3)
fig, ax = pl.subplots()
ax.add_collection(lc)
ax.autoscale()
ax.margins(0.05)

#The code blow is just for drawing the final plot of the building.
Nodes = xlrd.open_workbook(Node_file_location)
sheet = Nodes.sheet_by_index(0)
Node_Order_Counter = range(1, sheet.nrows + 1)
In_Node_Order_Counter = 0
for counter in range(len(Node_Positions_Ascending)):
    plt.plot(Node_Positions_Ascending[counter][0],     Node_Positions_Ascending[counter][1], marker='o', color='r',
             markersize=6)
    pl.text(Node_Positions_Ascending[counter][0],     Node_Positions_Ascending[counter][1],
            str(Node_Order_Counter[In_Node_Order_Counter]),
            color="black", fontsize=15)
    In_Node_Order_Counter += 1
#Plotting the different node positions on our plot & numbering them
pl.show()
Mahmoud Ayman
  • 197
  • 1
  • 13
  • To have a plot and a contourf plot on the same figure you should use subplots (see the [suplots demo](http://matplotlib.org/examples/pylab_examples/subplots_demo.html)). – Flabetvibes May 09 '15 at 16:00
  • Note that you should include imports in your code so we don't have to guess what 'pl', 'mc', etc. are. – Ajean May 09 '15 at 17:47
  • @Baptiste but I don't want to put them as subplots beside each other I want to plot the to plots on top of each other. – Mahmoud Ayman May 09 '15 at 18:05
  • @Ajean sorry I edited my question to include my imports. – Mahmoud Ayman May 09 '15 at 18:06
  • Oops... It was not clear to me. Then if you want to have a plot and a contourf plot on the same axes, Ajean's answer is the one you are looking for. – Flabetvibes May 09 '15 at 21:02

1 Answers1

1

Without your data we can't see what the plot is supposed to look like, but I have some general recommendations.

  1. Don't use pylab. And if you absolutely must use it, use it within its namespace, and don't do from pylab import *. It makes for very sloppy code - for example, linspace and meshgrid are actually from numpy, but it's hard to tell that when you use pylab.
  2. For complicated plotting, don't even use pyplot. Instead, use the direct object plotting interface. For example, to make a normal plot on top of a contour plot, (such as you want to do) you could do the following:
import numpy as np
import matplotlib.pyplot as plt

fig, ax = plt.subplots()

x = np.linspace(1, 5, 20)
y = np.linspace(2, 5, 20)
z = x[:,np.newaxis] * (y[np.newaxis,:])**2

xx, yy = np.meshgrid(x, y)

ax.contourf(xx, yy, z, cmap='Reds')
ax.plot(x, 0.2*y**2)

plt.show()

contour and line plot together

Notice that I only used pyplot to create the figure and axes, and show them. The actual plotting is done using the AxesSubplot object.

Ajean
  • 5,528
  • 14
  • 46
  • 69
  • That's exactly what i wanted but can I ask you a performance tip question? What if I have a 100mx100m large space that I calculate the power at every 1 meter in that space to get the contourf plot of that space at the end. My code runs in 25 mins which is unsatisfactory so do you know a tip that I can do to reduce the run time? – Mahmoud Ayman May 10 '15 at 10:44
  • 1
    There are plenty of ways to speed up code but it's highly dependent on what operations you are doing. Off the top of my head, I would say 1) vectorize your operations as much as possible to avoid loops, and 2) possibly lower your grid resolution (only calculate every 2m rather than 1m) – Ajean May 10 '15 at 14:27
  • I can start by doing that thank you @Ajean for your help,appreciate it! – Mahmoud Ayman May 11 '15 at 10:20