如何在同一图中绘制contourf和我的图形

时间:2021-01-11 15:00:06

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:

我有一个显示contourf图的图,另一个显示我之前制作的图,我想在同一个图上绘制我应该怎么做?这是我的contourf图的代码:

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

1 个解决方案

#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. 不要使用pylab。如果你绝对必须使用它,在它的命名空间中使用它,而不是从pylab import *。它使代码非常粗糙 - 例如,linspace和meshgrid实际上来自numpy,但是当你使用pylab时很难说。

  3. 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:
  4. 对于复杂的绘图,甚至不使用pyplot。而是使用直接对象绘图界面。例如,要在等高线图上绘制正常图(例如您想要这样做),您可以执行以下操作:

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

如何在同一图中绘制contourf和我的图形

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

请注意,我只使用pyplot来创建图形和轴,并显示它们。使用AxesSubplot对象完成实际绘图。

#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. 不要使用pylab。如果你绝对必须使用它,在它的命名空间中使用它,而不是从pylab import *。它使代码非常粗糙 - 例如,linspace和meshgrid实际上来自numpy,但是当你使用pylab时很难说。

  3. 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:
  4. 对于复杂的绘图,甚至不使用pyplot。而是使用直接对象绘图界面。例如,要在等高线图上绘制正常图(例如您想要这样做),您可以执行以下操作:

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

如何在同一图中绘制contourf和我的图形

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

请注意,我只使用pyplot来创建图形和轴,并显示它们。使用AxesSubplot对象完成实际绘图。