我正在尝试使用tri.UniformTriRefiner()
来平滑matplotlib
中的tricontour()
图。使用优化器会更改插值图中的z
值,并且色条不正确。
如何使用精磨机而不破坏颜色栏图例上的值?图形显示如下(顶部颜色条是正确的,而底部颜色条已被平滑是错误的)
为方便起见,我在下面的代码中包含了实际数据。
import numpy as np
import matplotlib.pyplot as plt
import matplotlib.tri as tri
# The data used for the plots is down below the code.
x,y = lon, lat
fig, axs = plt.subplots(2,1, figsize=(8, 20))
# Top plot.
axs[0].tricontour(x, y, z, levels=20, colors='k', linewidths=[0.5])
cntr1 = axs[0].tricontourf(x, y, z, levels=20, cmap="RdBu_r")
fig.colorbar(cntr1, ax=axs[0])
axs[0].plot(x, y, 'ko', ms=3)
# Bottom plot.
triang = tri.Triangulation(x, y)
refiner = tri.UniformTriRefiner(triang)
tri_refi, z_test_refi = refiner.refine_field(z, subdiv=4)
axs[1].tricontour(tri_refi, z_test_refi, levels=20, colors='k', linewidths=[0.5])
cntr2 = axs[1].tricontourf(tri_refi, z_test_refi, levels=20, cmap="RdBu_r")
fig.colorbar(cntr2, ax=axs[1])
axs[1].plot(x, y, 'ko', ms=3)
# Data.
lat = [33.59536838, 33.59531911, 33.59520088, 33.59536027, 33.59520654,
33.5951027 , 33.59510482, 33.59538413, 33.5952688 , 33.59509653,
33.59501612, 33.59494995, 33.59494547, 33.59487379, 33.59481165,
33.5947298 , 33.59459121, 33.59460965, 33.59441196, 33.59447806,
33.59439846, 33.59428399, 33.59427474, 33.59410198, 33.59427181,
33.59400462, 33.59388889, 33.59407705, 33.59387735, 33.59420237,
33.5953678 , 33.59531812, 33.59520149, 33.59536007, 33.59520707,
33.59510205, 33.59510521, 33.59538385, 33.59526902, 33.59509762,
33.59501683, 33.59495015, 33.5948735 , 33.59494524, 33.59481145,
33.59472871, 33.59459095, 33.59460917, 33.59441184, 33.59447723,
33.59439906, 33.59428439, 33.59427491, 33.594101 , 33.59427155,
33.59400471, 33.5938885 , 33.59407732, 33.59387745, 33.59420175,
33.59536813, 33.59531855, 33.59520131, 33.59536029, 33.59520743,
33.5951026 , 33.59510521, 33.59538377, 33.59526879, 33.59509792,
33.59501622, 33.59495103, 33.59494534, 33.59487461, 33.59481132,
33.59460946, 33.59459114, 33.59472944, 33.5944777 , 33.59441151,
33.59428452, 33.59427576, 33.59439819, 33.59427131, 33.5941017 ,
33.59400439, 33.59388869, 33.59407702, 33.5938776 , 33.59420191]
lon = [-101.90127086, -101.90134758, -101.90147466, -101.90115969,
-101.90114855, -101.90114786, -101.90131617, -101.90108705,
-101.90101192, -101.9009005 , -101.90094145, -101.90104695,
-101.90123616, -101.90113266, -101.90095986, -101.90129601,
-101.90110232, -101.90094035, -101.90099151, -101.90119575,
-101.9014437 , -101.90123085, -101.90135262, -101.90143681,
-101.90160215, -101.90151603, -101.90166566, -101.90175199,
-101.90184897, -101.90204107, -101.90127098, -101.90134759,
-101.90147398, -101.90116076, -101.90114704, -101.90114763,
-101.90131603, -101.90108828, -101.90101285, -101.90090033,
-101.90094203, -101.90104777, -101.90113255, -101.90123608,
-101.90096103, -101.9012962 , -101.90110205, -101.90094081,
-101.90099263, -101.90119576, -101.90144357, -101.90123061,
-101.90135311, -101.9014374 , -101.90160358, -101.90151535,
-101.90166629, -101.90175237, -101.90184923, -101.90204039,
-101.90127062, -101.90134831, -101.9014742 , -101.90116025,
-101.90114673, -101.90114723, -101.90131579, -101.90108756,
-101.90101258, -101.90090105, -101.90094218, -101.90104678,
-101.90123567, -101.9011329 , -101.90096169, -101.90094104,
-101.90110318, -101.90129618, -101.90119516, -101.90099258,
-101.90123088, -101.90135351, -101.90144363, -101.90160277,
-101.90143722, -101.90151546, -101.90166631, -101.90175231,
-101.90184905, -101.90204077]
z = [0.0488 , 0.04628, 0.03782, 0.03912, 0.1803 , 0.07461, 0.07409,
0.06053, 0.1209 , 0.14101, 0.0841 , 0.07189, 0.03897, 0.04633,
0.12253, 0.07958, 0.0802 , 0.08917, 0.09295, 0.09682, 0.04812,
0.08526, 0.10893, 0.0811 , 0.04736, 0.08756, 0.04472, 0.05162,
0.04203, 0.04233, 0.07426, 0.10559, 0.0548 , 0.07571, 0.12094,
0.14117, 0.03771, 0.0591 , 0.04044, 0.08481, 0.01198, 0.08645,
0.07662, 0.0603 , 0.04663, 0.07575, 0.09041, 0.02739, 0.03672,
0.13548, 0.11217, 0.0521 , 0.06631, 0.04117, 0.08871, 0.06105,
0.03788, 0.04411, 0.04346, 0.05149, 0.07572, 0.05418, 0.02554,
0.04249, 0.15768, 0.08468, 0.01863, 0.03404, 0.06034, 0.03678,
0.06481, 0.05243, 0.05383, 0.06549, 0.07509, 0.03548, 0.04469,
0.0438 , 0.05927, 0.01797, 0.02696, 0.03577, 0.03814, 0.04057,
0.04517, 0.03143, 0.02208, 0.02153, 0.01983, 0.02084]