How to plot contourf and my graph in the same figure - python

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

Without your data we can't see what the plot is supposed to look like, but I have some general recommendations.
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.
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()
Notice that I only used pyplot to create the figure and axes, and show them. The actual plotting is done using the AxesSubplot object.

Related

matplotlib fill in between step function

I need to plot a step function in matplotlib (because that's the correct interpretation of my data) and would like to fill between the x-axis and the step curve.
Something like fill_between, only that this one does not work with drawstyle.
See minimal example below:
import numpy as np
import matplotlib.pyplot as plt
np.random.seed(2)
x = np.arange(50)
y = np.random.rand(50)
fig = plt.figure()
ax = fig.add_subplot(111)
ax.plot(x, y, drawstyle='steps-post')
ax.fill_between(x, y, drawstyle='steps-post') # does not work
plt.show()
You can use ax.fill_between(x, y, step='post'). fill_between doesn't have the parameter of drawstyle.

How to keep the previous plot and not clear the figure after calling plt.show() without simply repeating the call?

import matplotlib.pyplot as plt
import numpy as np
x = np.linspace(0, 2 * np.pi, 100)
plt.plot(x, np.sin(x))
plt.show()
plt.plot(x, np.cos(x))
plt.show()
This example produces two consecutive plots in two windows, first the one on the left, then the one on the right:
As you can see, the sine disappeared and only the cosine shows up in the second window; the plot window has been cleared in between. However, this is what I want:
import matplotlib.pyplot as plt
import numpy as np
x = np.linspace(0, 2 * np.pi, 100)
plt.plot(x, np.sin(x))
plt.show()
plt.plot(x, np.sin(x))
plt.plot(x, np.cos(x))
plt.show()
How can I keep the previous plot so it shows up in the second plot, too? There has to be a better way other than simply repeating the previous call to plt.plot?
(A) Repeat previous calls to plt.plot: Just as mentioned in the question, it's possible to just repeat the previous calls which created the plots before adding the new plot. This might be the easiest solution for simple cases.
import matplotlib.pyplot as plt
import numpy as np
x = np.linspace(0, 2 * np.pi, 100)
plt.plot(x, np.sin(x))
plt.show()
plt.plot(x, np.sin(x))
plt.plot(x, np.cos(x))
plt.show()
(B) Use plt.waitforbuttonpress: If this is applicable in your case, you can use plt.waitforbuttonpress as an interrupt between plots which waits for a mouse click or key press until the next plot is added into the same window.
import matplotlib.pyplot as plt
import numpy as np
x = np.linspace(0, 2 * np.pi, 100)
for f in [np.sin, np.cos]:
plt.plot(x, f(x))
plt.waitforbuttonpress()
(C) Pickle the plot before plt.show: I don't know how well this works for complex plots, but it seems possible to pickle the plot before calling plt.show, only to immediately restore it afterwards. Then the additional plot can be added before the next call to plt.show.
import matplotlib.pyplot as plt
import numpy as np
import pickle
x = np.linspace(0, 2 * np.pi, 100)
ax = plt.gca()
for f in [np.sin, np.cos]:
plt.plot(x, f(x))
temp = pickle.dumps(ax)
plt.show()
ax = pickle.loads(temp)
(D) Manipulating figure manager and canvas: Essentially, the figure fig gets "loaded into" a newly created figure for each loop iteration, then updated with new plots.
import matplotlib.pyplot as plt
import numpy as np
x = np.linspace(0, 2 * np.pi, 100)
fig = plt.figure()
plt.close()
for f in [np.sin, np.cos]:
plt.figure()
fm = plt.get_current_fig_manager()
fm.canvas.figure = fig
fig.canvas = fm.canvas
plt.plot(x, f(x))
plt.show()
plt.close()

How to control axis limits in mplot3d in pythons matplotlib

in mplot3d how do I change my axis limits such gets cut off the at the limites. When using ax.set_xlim3d() the graph continuous out the plot.
Consider the graph generated by:
import numpy as np; import matplotlib.pyplot as plt;
from mpl_toolkits import mplot3d
def func(x, y):
return x ** 2 + 0.5*y ** 3
x = np.linspace(-6, 6, 30)
y = np.linspace(-6, 6, 30)
X, Y =np. meshgrid(x, y)
Z = func(X, Y)
plt.clf()
fig = plt.figure(1)
ax = fig.add_subplot(111, projection='3d')
ax.plot_surface(X, Y, Z, rstride=2, cstride=1,
cmap='viridis', edgecolor='none')
Say I want to cut my z = 50, so the last part disappears. ax.set_zlim3d(-100,50) doesn't do the trick. I have a lot of code written in this form so I prefer not to use the method described here mplot3D fill_between extends over axis limits where the core code of the plots are totally different than mine. TI figure there must be a way to fix my problem my adding a line of code to my existing code.

Superimpose independent plots in python

I want to work with only one figure, with multiples, different and modifiable plots, whithout the subplots formalism.
Is there a way to superimpose two differents plots, in the same way as text boxes, i.e anywhere on the figure ?
Here a "gimp made" example :
Thanks !
You can use figure.add_axes to place an axes at an arbitrary location.
fig = plt.figure()
fig.add_axes([0.1,0.2,0.3,0.4])
places an axes at x=0.1, y=0.2, width=0.3, height=0.4 in figure coordinates.
from mpl_toolkits.mplot3d import Axes3D
import matplotlib.pyplot as plt
import numpy as np
fig = plt.figure()
ax = fig.add_axes([0.4,0.1,0.5,0.6], projection='3d')
X, Y = np.meshgrid(np.arange(-5, 5, 0.25), np.arange(-5, 5, 0.25))
Z = np.sin(np.sqrt(X**2 + Y**2))
surf = ax.plot_surface(X, Y, Z, cmap="plasma")
ax = fig.add_axes([0.3,0.4,0.3,.4])
plt.plot([1,2,3])
plt.show()

Python : How to plot 3d graphs using Python?

I am using matplotlib for doing this
from mpl_toolkits.mplot3d import Axes3D
import numpy as np
import matplotlib
import matplotlib.pyplot as plt
fig = plt.figure()
ax = Axes3D(fig)
x = [6,3,6,9,12,24]
y = [3,5,78,12,23,56]
ax.plot(x, y, zs=0, zdir='z', label='zs=0, zdir=z')
plt.show()
Now this builds a graph that is horizontal in the 3d space. How do I make the graph vertical so that it faces the user?
What I want to do is build multiple such vertical graphs that are separated by some distance and are facing the user.
bp's answer might work fine, but there's a much simpler way.
Your current graph is 'flat' on the z-axis, which is why it's horizontal. You want it to be vertical, which means that you want it to be 'flat' on the y-axis. This involves the tiniest modification to your code:
from mpl_toolkits.mplot3d import Axes3D
import numpy as np
import matplotlib
import matplotlib.pyplot as plt
fig = plt.figure()
ax = Axes3D(fig)
x = [6,3,6,9,12,24]
y = [3,5,78,12,23,56]
# put 0s on the y-axis, and put the y axis on the z-axis
ax.plot(xs=x, ys=[0]*len(x), zs=y, zdir='z', label='ys=0, zdir=z')
plt.show()
Then you can easily have multiple such graphs by using different values for the ys parameter (for example, ys=[2]*len(x) instead would put the graph slightly behind).
Mayavi, in particular the mlab module, provides powerful 3D plotting that will work on large and or complex data, and should be easy to use on numpy arrays.
You can set the view angle of the 3d plot with the view_init() function. The example below is for version 1.1 of matplotlib.
from mpl_toolkits.mplot3d import axes3d
import numpy as np
import matplotlib
import matplotlib.pyplot as plt
fig = plt.figure()
ax = fig.add_subplot(111, projection='3d')
x = [6,3,6,9,12,24]
y = [3,5,78,12,23,56]
ax.plot(x, y, zs=0, zdir='z', label='zs=0, zdir=z')
ax.view_init(90, -90)
plt.show()
According to the documentation you want to use the ax.plot_surface(x,y,z) method. More information and chart types here.
The following should work:
x = [1,2,3]
y = [4,5,6]
z = [7,8,9]
data = zip(x,y,z)
#map data on the plane
X, Y = numpy.meshgrid(arange(0, max(x), 1), arange(0, max(y), 1))
Z = numpy.zeros((len(Y), len(X)), 'Float32')
for x_,y_,z_ in data:
Z[x_, y_] = z_ #this should work, but only because x and y are integers
#and arange was done with a step of 1, starting from 0
fig = p.figure()
ax = p3.Axes3D(fig)
ax.plot_surface(X, Y, Z)

Categories

Resources