Mahmoud Ayman
Mahmoud Ayman

Reputation: 197

How to plot contourf and my graph in the same figure

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()

Upvotes: 0

Views: 2958

Answers (1)

Ajean
Ajean

Reputation: 5659

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.

Upvotes: 1

Related Questions