时间:2021-05-22
使用matplotlib.tri.CubicTriInterpolator.演示变化率计算:
完整实例:
from matplotlib.tri import ( Triangulation, UniformTriRefiner, CubicTriInterpolator)import matplotlib.pyplot as pltimport matplotlib.cm as cmimport numpy as np#-----------------------------------------------------------------------------# Electrical potential of a dipole#-----------------------------------------------------------------------------def dipole_potential(x, y): """ The electric dipole potential V """ r_sq = x**2 + y**2 theta = np.arctan2(y, x) z = np.cos(theta)/r_sq return (np.max(z) - z) / (np.max(z) - np.min(z))#-----------------------------------------------------------------------------# Creating a Triangulation#-----------------------------------------------------------------------------# First create the x and y coordinates of the points.n_angles = 30n_radii = 10min_radius = 0.2radii = np.linspace(min_radius, 0.95, n_radii)angles = np.linspace(0, 2 * np.pi, n_angles, endpoint=False)angles = np.repeat(angles[..., np.newaxis], n_radii, axis=1)angles[:, 1::2] += np.pi / n_anglesx = (radii*np.cos(angles)).flatten()y = (radii*np.sin(angles)).flatten()V = dipole_potential(x, y)# Create the Triangulation; no triangles specified so Delaunay triangulation# created.triang = Triangulation(x, y)# Mask off unwanted triangles.triang.set_mask(np.hypot(x[triang.triangles].mean(axis=1), y[triang.triangles].mean(axis=1)) < min_radius)#-----------------------------------------------------------------------------# Refine data - interpolates the electrical potential V#-----------------------------------------------------------------------------refiner = UniformTriRefiner(triang)tri_refi, z_test_refi = refiner.refine_field(V, subdiv=3)#-----------------------------------------------------------------------------# Computes the electrical field (Ex, Ey) as gradient of electrical potential#-----------------------------------------------------------------------------tci = CubicTriInterpolator(triang, -V)# Gradient requested here at the mesh nodes but could be anywhere else:(Ex, Ey) = tci.gradient(triang.x, triang.y)E_norm = np.sqrt(Ex**2 + Ey**2)#-----------------------------------------------------------------------------# Plot the triangulation, the potential iso-contours and the vector field#-----------------------------------------------------------------------------fig, ax = plt.subplots()ax.set_aspect('equal')# Enforce the margins, and enlarge them to give room for the vectors.ax.use_sticky_edges = Falseax.margins(0.07)ax.triplot(triang, color='0.8')levels = np.arange(0., 1., 0.01)cmap = cm.get_cmap(name='hot', lut=None)ax.tricontour(tri_refi, z_test_refi, levels=levels, cmap=cmap, linewidths=[2.0, 1.0, 1.0, 1.0])# Plots direction of the electrical vector fieldax.quiver(triang.x, triang.y, Ex/E_norm, Ey/E_norm, units='xy', scale=10., zorder=3, color='blue', width=0.007, headwidth=3., headlength=4.)ax.set_title('Gradient plot: an electrical dipole')plt.show()总结
以上就是本文关于python+matplotlib演示电偶极子实例代码的全部内容,希望对大家有所帮助。感兴趣的朋友可以继续参阅本站其他相关专题,如有不足之处,欢迎留言指出。感谢朋友们对本站的支持!
声明:本页内容来源网络,仅供用户参考;我单位不保证亦不表示资料全面及准确无误,也不保证亦不表示这些资料为最新信息,如因任何原因,本网内容或者用户因倚赖本网内容造成任何损失或损害,我单位将不会负任何法律责任。如涉及版权问题,请提交至online#300.cn邮箱联系删除。
本文分享的实例主要实现的是Python+matplotlib绘制一个有阴影和没有阴影的3D条形图,具体如下。首先看看演示效果:完整代码如下:importnump
本文是从matplotlib官网上摘录下来的一个实例,实现的功能是Python+matplotlib绘制自定义饼图作为散点图的标记,具体如下。首先看下演示效果实
例1使用Python+matplotlib绘图进行可视化,在图形中创建轴域并设置轴域的位置和大小,同时演示设置坐标轴标签和图例位置的用法。参考代码:运行结果:例
本文所示代码主要是通过Python+matplotlib实现作图,并且在图中添加表格的功能,具体如下。代码importmatplotlib.pyplotaspl
Python+matplotlib进行鼠标交互,实现动态标注,数据可视化显示,鼠标划过时画一条竖线并使用标签来显示当前值。Python3.6.5,代码示例:im