简体   繁体   English

如何在同一图中绘制轮廓线f和我的图

[英]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? 我有一个图显示了contourf图,另一个图显示了我之前绘制的图,我想将两个图都绘制在同一图上,我该怎么办? Here is the code of my contourf plot: 这是我的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()

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. 不要使用pylab。 And if you absolutely must use it, use it within its namespace, and don't do from pylab import * . 而且,如果您绝对必须使用它,请在其名称空间中使用它,并且不要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. 它使代码非常草率-例如,linspace和meshgrid实际上来自numpy,但是使用pylab时很难说出来。
  2. For complicated plotting, don't even use pyplot. 对于复杂的绘图,甚至不要使用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()

等高线图

Notice that I only used pyplot to create the figure and axes, and show them. 请注意,我仅使用pyplot创建图形和轴,并显示它们。 The actual plotting is done using the AxesSubplot object. 实际绘制是使用AxesSubplot对象完成的。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM