In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-02-28 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)06/01 Report--
This article mainly introduces how to draw contours and three-dimensional images based on python and Matplotlib, which has a certain reference value, interested friends can refer to, I hope you can learn a lot after reading this article, the following let Xiaobian take you to understand.
The function drawn in this example is:
1. Grid point
takes a look at the drawing of grid points before drawing a curve. For example, to draw a 3x3 grid, you need nine coordinate points:
(0pr 2)-(1pr 2)-(2pr 2)
(0pr 1)-(1jr 1)-(2pr 1)
(0) (0)-(1) (1)-(2)
represents its x-axis and y-axis coordinates separately:
# x axis: [[0,1,2], [0,1,2], [0,1,2]] # y axis: [[0,0,0], [1,1,1], [2,2,2]]
can use np.meshgrid () in numpy to generate grid points:
Import numpy as npimport matplotlib.pyplot as plt# 10x10x = np.linspace (- 1.5,1.5, num=10) y = np.linspace (- 1.5,1.5, num=10) # generate gridX, Y = np.meshgrid (x, y) plt.plot (X, Y, marker='.', linestyle='') plt.grid (True) plt.show ()
two。 Contour line
The coordinate position of the data points needed by to draw the contours (XMague y) and the height z of the coordinates z, which is calculated by bringing the coordinate points (XMague y) into the function f (XMago y) f (XMagy) f (XPY). In matplotlib, you can use plt.contour () to draw:
Import numpy as npimport matplotlib.pyplot as pltx = np.linspace (- 1.5,1.5, num=100) y = np.linspace (- 1.5,1.5, num=100) X, Y = np.meshgrid (x, y) f = X* np.exp (- X, y) fig = plt.figure () plt.xlim (- 1.5,1.5) plt.ylim (- 1.5,1.5) # drawax = plt.contour (X, Y, f, levels=10 Cmap=plt.cm.cool) # add labelplt.clabel (ax, inline=True) # plt.savefig ('img1.png') plt.show ()
# add colorplt.contourf (X, Y, f, levels=10, cmap=plt.cm.cool) # plt.colorbar () # plt.savefig ('img1.png') plt.show ()
For more api parameters of , please refer to the official documentation.
3. Three-dimensional image
The 3D image of the function needs the same data as the contours, that is, the coordinate position (x, y) and the height z of the coordinates. In matplotlib, you can use mpl_toolkits.mplot3d to draw:
Import numpy as npimport matplotlib.pyplot as pltfrom mpl_toolkits.mplot3d import Axes3Dx = np.linspace (- 1.5,1.5, num=100) y = np.linspace (- 1.5,1.5, num=100) X, Y = np.meshgrid (x, y) f = X* np.exp (- X, y) fig = plt.figure () ax = Axes3D (fig) plt.xlim (- 1.5,1.5) plt.ylim (- 1.5,1.5) ax.plot_surface (X, Y, f Cmap=plt.cm.cool) # plt.savefig ('img1.png') plt.show ()
Thank you for reading this article carefully. I hope the article "how to draw contours and 3D images of functions based on python and Matplotlib" shared by the editor will be helpful to you. At the same time, I also hope you will support us and pay attention to the industry information channel. More related knowledge is waiting for you to learn!
Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.
Views: 0
*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.