如何“平滑” 3D 条形图?

时间:2021-06-09 19:41:30

标签: python matplotlib

我计划使用 3D 投影显示法国每个城市的海拔高度。通过这样做,我将能够看到法国的地形图。

当我用我的价值观这样做时,我得到了: The 3D plot I've got

我想知道是否可以平滑条形以使其看起来更像一个表面,以及是否可以将 colormap 更改为与地形图更匹配的颜色

这是值(有很多,但值较少,我们无法绘制任何有趣的东西):

import numpy as np

latitude = np.array([45.7333, 45.75, 46.25, 46.1167, 49.6333, 49.25, 49.85, 49.4333, 49.9833, 49.8333, 49.9167, 49.8833, 49.5333, 49.5333, 46.2833, 46.0667, 46.1, 46.1833, 46.15, 46.1667, 44.4167, 44.0833, 44.3923, 44.6667, 44.65, 43.65, 43.8833, 44.8167, 45.0667, 45.2333, 44.3667, 50.1, 42.9167, 42.65, 43.0, 43.15, 43.0, 43.0833, 48.2667, 48.0333, 48.2667, 48.3833, 43.0853, 43.25, 42.9833, 43.2167, 44.1, 44.15, 44.4667, 43.8333, 49.2114, 49.0, 48.85, 49.1167, 49.0312, 49.2167, 49.3167, 49.2667,48.85, 48.9167, 49.15, 45.15, 45.3167, 45.6, 45.4333, 45.3931, 45.6667, 45.6333, 45.7531, 45.8, 45.9667, 45.0333, 47.3167, 47.1833, 47.6, 47.1167, 48.4333, 48.45, 46.2833, 45.8975, 45.8667, 44.9667, 45.3833, 44.7333, 44.8333, 44.7833, 45.3167, 47.2167, 47.1333, 47.45, 46.95, 47.3167, 47.0333, 47.4833, 47.4, 47.3333, 44.6167, 44.5667, 44.6833, 44.7667, 44.55, 44.75, 49.3667, 48.7667, 49.2333, 49.2667, 49.2167, 49.1667, 48.5167, 48.4167, 48.0167, 44.15, 44.15, 44.2, 44.05, 42.8167, 43.45, 43.75, 43.45, 43.7333, 43.7167, 43.0964, 43.6333, 43.4333, 43.9583,43.5828, 43.6333, 43.4289, 44.85, 44.4, 44.6167, 45.3275, 44.3833, 43.7333, 43.4684, 43.7945, 43.5333, 48.6333, 47.8667, 46.3867, 47.35, 47.2667, 47.0333, 44.9833, 47.2167, 46.4167, 46.6167, 47.1833, 46.8, 46.7667, 43.8833, 43.6167, 46.2, 46.1706, 45.5333, 45.7833, 45.1833, 45.3, 47.0667, 47.4417, 47.0556, 47.3333, 47.2, 48.2667, 48.05, 48.0333, 47.8333, 47.85, 48.1667, 47.8667, 44.65, 44.4667, 44.45, 44.4167, 44.3, 44.4667, 44.3, 47.3333, 47.2833, 49.3, 48.7667, 48.7333, 49.3, 48.8, 49.0167, 48.6, 49.1167, 48.8, 49.3333, 48.5333, 49.05, 48.7395, 48.95,49.0667, 47.9998, 48.0333, 47.7333, 47.9333, 48.4167, 48.4203, 48.5423, 48.0167, 48.4667, 48.5, 49.3333, 48.6833, 49.097, 48.5833, 47.8167, 47.85, 49.3333, 49.0667, 49.1667, 49.3833, 48.95, 49.0333, 48.7833, 48.85, 48.8833, 49.0333, 47.4028, 47.4333, 47.4, 47.2833, 47.4167, 50.6167, 50.7, 50.6667, 50.05, 50.8667, 50.25, 49.5833, 49.6333, 49.4, 49.6, 49.5, 49.2167, 49.6, 48.7, 48.85, 48.55, 48.4833, 48.8, 50.6167, 50.8167, 50.2126, 50.8, 50.5333, 50.2833, 50.95, 50.9, 50.35, 50.1667, 50.8, 50.4167, 50.2, 46.2, 45.6667, 45.8833, 45.4167, 43.45, 43.4333, 43.3833, 43.2667, 43.3333, 43.2167, 42.7667, 42.7167, 42.6333, 48.7667, 48.9333, 48.5667, 48.95, 48.6859, 48.8139, 48.95, 46.1833, 45.5606, 45.6333, 47.65, 47.4667, 47.75, 47.8, 47.5167, 47.6167, 47.7667, 47.4333, 46.6333, 47.0073, 46.8167, 46.3, 46.8, 46.8167, 47.762, 48.3498, 47.9673, 48.1333, 48.3333, 45.55, 45.6667,45.55, 45.2167, 46.0833, 46.4, 46.3167, 46.1167, 46.3, 46.0667, 49.7, 49.5667, 49.6, 49.8333, 49.7167, 49.85, 49.3833, 49.5667, 49.8167, 48.5167, 49.0667, 48.4167, 48.45, 48.7333, 48.8667, 48.3645, 48.45, 48.9833, 48.7039, 48.8833, 46.55, 46.2, 46.4167, 46.1167, 49.9, 50.1, 49.9333, 49.5833, 50.0333, 50.15, 49.9833, 50.2333, 50.0, 50.0667, 50.0167, 49.7, 44.25, 43.5, 44.1167, 43.8333, 46.7833, 46.75, 46.3333, 46.9364, 45.75, 48.25, 48.2833, 48.3667, 48.3333, 48.35, 48.25, 48.1667, 47.9833, 47.5, 48.2167, 47.8833, 47.75, 47.4833, 48.6667, 48.8667, 48.75,49.1, 49.0, 48.9833, 48.9833, 49.0, 42.2189, 42.7, 42.5742, 42.4573]) 


longitude = np.array([5.65, 5.75, 4.85, 4.86667, 3.85, 3.53333, 3.23333, 3.3, 3.21667, 3.9, 3.25, 3.2, 3.25, 3.16667, 3.6, 3.51667, 3.36667, 3.53333, 3.0, 3.3, 6.73333, 5.65, 6.62778, 6.65, 6.13333, 6.86667, 7.01667, 4.76667, 4.68333, 4.58333, 4.65, 4.76667, 1.68333, 1.83333, 1.05, 1.51667, 1.35, 1.36667, 3.81667, 4.43333, 3.65, 3.65, 2.70056, 1.96667, 2.19167, 1.83333, 2.9, 3.1, 2.71667, 4.95, -0.513889, 0.15, -0.233333, -0.083333, -0.422778, -0.533333, 0.033333, -0.483333, -0.766667, -0.483333, -0.216667, 2.93333, 2.58333, 0.35, -0.033333, -0.0727778, 0.5, -0.516667, -0.506111, -0.216667, -0.716667, 1.8, 5.11667, 5.28333, 4.26667, 4.6, -2.96667, -3.31667, 2.21667, 1.82111, 1.8, 0.933333, 0.716667, 0.533333, 1.2, 1.0, 0.6, 6.55, 6.8, 6.41667, 6.45, 6.4, 6.13333, 6.83333, 6.63333, 5.9, 5.08333, 5.16667, 4.95,4.98333, 4.85, 5.26667, 1.38333, 1.1, 0.9, 0.916667, 0.55, 0.7, 1.75, 1.48333, -4.56667, 3.68333, 4.68333, 4.53333, 4.7, 0.55, 1.73333, 1.11667, 1.06667, 1.41667, 1.56667, 0.625833, 0.35, 0.466667, 0.141667, 0.614167, 0.066667, 0.415, -0.25, -0.05, -0.066667, -0.575833, -0.216667, 4.08333, 3.17889, 3.91445, 3.93333, -2.06667, -1.88333, 1.30722, 0.466667, 0.6, 0.45, 5.88333, 5.68333, 5.46667, 5.7,5.61667, 5.83333, 6.0, -0.733333, -0.566667, 4.38333, 3.855, 3.98333, 3.85, 3.9, 3.65, -1.31667, -1.425, 0.487223, -2.43333, -1.75, 2.23333, 2.4, 2.8, 1.7, 2.93333, 2.28333, 2.23333, 1.43333, 1.23333, 0.183333, 1.05, 3.31667, 3.7, 3.18333,-0.666667, -0.983333, -1.63333, -1.23333, -1.38333, -1.56667, -1.4, -1.55, -1.08333, 3.8, 4.26667, 3.76667, 3.83333, 4.78333, 4.31139, 3.61667, 4.2, 5.49834, 4.8, 5.1, 5.86667, 5.21667, -0.1325, -0.206944, -0.4, 6.23333, 6.1, 5.75, 6.38333, 5.59278, 5.63333, -2.5, -3.0, 6.11667, 6.61667, 6.03333, 6.31667, 6.85, 6.38333, 6.46667, 6.78333, 6.86667, 6.2, 3.40028, 3.5, 3.6, 3.4, 3.58333, 3.01667, 2.4, 3.23333, 3.93333, 2.28333, 3.53333, 2.5, 2.03333, 1.88333, 1.93333, 1.8, 2.66667, 2.6, -0.316667, 0.6, 0.3, 0.85, -0.7, 2.36667, 1.9, 2.53056, 2.0, 1.93333, 2.91667, 2.01667, 1.83333, 2.6, 2.56667, 2.1, 2.81667, 2.78333, 2.75, 3.63333, 2.91667, 3.38333, -0.3, -1.55, -0.9, -0.683333, 0.133333, 0.433333, 2.63333, 2.53333, 2.95, 7.95, 7.83333, 7.43333, 7.71667, 7.44945, 7.53028, 7.8, 4.55, 4.80612,4.6, 6.23333, 6.28333, 6.48333, 6.33333, 6.48333, 6.3, 5.78333, 5.56667, 5.18333, 4.47139, 4.88333, 4.83333, 4.6, 4.96667, 0.655278, 0.254722, -0.137778, 0.716667, 0.083333, 6.21667, 5.91667, 5.91667, 6.23333, 6.43333, 6.6, 6.3, 6.28333, 6.78333, 6.6, 1.25, 1.66667, 0.133333, 1.15, 0.666667, 0.65, 0.883333, 1.11667, 0.8, 3.16667, 2.6, 2.95, 2.55, 2.63333, 3.35, 2.98834, 2.83333, 1.66667, 1.98417,1.71667, -0.483333, -0.316667, -0.35, -0.166667, 1.83333, 1.65, 2.71667, 2.65, 1.5, 1.96667, 2.91667, 1.66667, 2.75, 1.83333, 2.2, 2.35, 1.13333, 5.73333, 5.1,5.21667, -1.43333, -1.91667, 0.2, 0.605833, 1.11667, 6.08333, 6.16667, 5.43333,6.33333, 6.43333, 6.41667, 7.01667, 5.91667, 3.81667, 3.06667, 3.95, 3.41667, 6.91667, 2.53333, 2.43333, 2.46667, 2.45, 2.33333, 2.35, 2.2, 2.38333, 9.3714, 9.44945, 9.0089, 9.34334]) 


altitude = np.array([495.0, 623.0, 212.0, 267.0, 116.0, 140.0, 128.0, 156.0, 146.0, 208.0, 129.0, 123.0, 76.0, 76.0, 342.0, 608.0, 375.0, 482.0, 729.0, 343.0, 3027.0, 1430.0, 2879.0, 2694.0, 1340.0, 790.0, 1108.0, 535.0, 486.0, 912.0, 415.0, 355.0, 1093.0, 2840.0, 1247.0, 383.0, 931.0, 580.0, 235.0, 333.0, 263.0, 186.0, 344.0, 346.0, 602.0, 360.0, 1087.0, 846.0, 711.0, 67.0, 72.0, 186.0, 229.0, 50.0, 186.0, 77.0, 112.0, 59.0, 246.0, 261.0, 22.0, 1169.0, 980.0, 204.0, 162.0, 166.0, 284.0, 52.0, 89.0, 97.0, 28.0, 366.0, 251.0, 204.0, 387.0, 452.0, 254.0, 287.0, 656.0, 798.0,741.0, 229.0, 208.0, 197.0, 218.0, 219.0, 192.0, 943.0, 1087.0, 470.0, 1171.0, 561.0, 690.0, 417.0, 592.0, 251.0, 651.0, 1088.0, 485.0, 415.0, 210.0, 1000.0, 140.0, 179.0, 177.0, 142.0, 171.0, 157.0, 156.0, 157.0, 79.0, 1200.0, 268.0, 359.0, 261.0, 2676.0, 262.0, 274.0, 331.0, 175.0, 225.0, 515.0, 222.0, 302.0, 181.0,285.0, 204.0, 286.0, 77.0, 152.0, 83.0, 90.0, 133.0, 92.0, 150.0, 241.0, 3.0, 56.0, 100.0, 237.0, 101.0, 98.0, 123.0, 2856.0, 353.0, 563.0, 590.0, 391.0, 678.0, 804.0, 57.0, 136.0, 721.0, 595.0, 1236.0, 900.0, 948.0, 1145.0, 54.0, 52.0, 39.0, 60.0, 27.0, 137.0, 142.0, 127.0, 113.0, 203.0, 129.0, 131.0, 394.0, 332.0, 77.0, 263.0, 1012.0, 1699.0, 1005.0, 104.0, 146.0, 41.0, 205.0, 120.0, 44.0, 134.0, 43.0, 117.0, 243.0, 177.0, 193.0, 85.0, 197.0, 230.0, 231.0, 132.0, 458.0, 358.0, 486.0, 332.0, 390.0, 393.0, 360.0, 116.0, 340.0, 319.0, 327.0, 269.0, 391.0, 381.0, 156.0, 137.0, 232.0, 406.0, 362.0, 310.0, 261.0, 314.0, 313.0, 275.0, 249.0, 237.0, 289.0, 217.0, 299.0, 384.0, 280.0, 49.0, 76.0, 43.0, 217.0, 63.0, 107.0, 127.0, 192.0, 234.0, 186.0, 198.0, 102.0, 102.0, 270.0, 261.0, 203.0, 237.0, 211.0, 62.0, 181.0, 178.0, 173.0, 192.0, 99.0, 5.0, 59.0, 131.0, 168.0, 116.0, 66.0, 108.0, 597.0, 663.0, 994.0, 767.0, 250.0, 94.0, 145.0, 266.0, 245.0, 563.0, 423.0, 800.0, 28.0, 128.0, 199.0, 273.0, 262.0, 290.0, 275.0, 446.0, 1004.0,548.0, 929.0, 400.0, 313.0, 340.0, 312.0, 375.0, 447.0, 372.0, 238.0, 209.0, 530.0, 191.0, 347.0, 438.0, 206.0, 153.0, 210.0, 109.0, 200.0, 152.0, 656.0, 1513.0, 576.0, 3464.0, 1843.0, 770.0, 520.0, 931.0, 2430.0, 1197.0, 207.0, 191.0, 105.0, 138.0, 128.0, 120.0, 129.0, 176.0, 103.0, 161.0, 157.0, 123.0, 132.0, 107.0,203.0, 108.0, 117.0, 128.0, 178.0, 174.0, 228.0, 89.0, 116.0, 115.0, 157.0, 90.0, 122.0, 126.0, 123.0, 117.0, 152.0, 5.0, 135.0, 87.0, 134.0, 158.0, 270.0, 872.0, 453.0, 693.0, 84.0, 19.0, 154.0, 127.0, 331.0, 445.0, 392.0, 441.0, 382.0, 383.0, 377.0, 1302.0, 339.0, 346.0, 169.0, 278.0, 292.0, 575.0, 89.0, 117.0, 88.0, 101.0, 186.0, 123.0, 170.0, 147.0, 735.0, 963.0, 1163.0, 1236.0])

这是我为绘制 3D 条形图所做的代码:

from mpl_toolkits.mplot3d import Axes3D
from matplotlib import cm
from matplotlib.colors import Normalize
import matplotlib.pyplot as plt
import matplotlib as mpl


z=np.ones(len(longitude))

dx=np.ones(len(longitude))
dy=np.ones(len(latitude))

fig = plt.figure()
ax = fig.add_subplot(projection='3d')


cmap = plt.cm.get_cmap('jet')
norm=Normalize(vmin=min(np.array(altitude)),vmax=max(np.array(altitude)))
colors=cmap(norm(np.array(altitude)))

bars=ax.bar3d(longitude,latitude,z,dx,dy,altitude, color=colors,shade=False)

ax.set_title('Relief', weight='bold')

ax.set_xticks([])
ax.set_yticks([])
plt.show()

编辑 1: 感谢@JohanC的回复,我把colormap改成了地形就完美了。

我也尝试使用 ax.plot_trisurf(longitude, latitude, altitude, cmap='terrain') 代替 ax.bar3d(longitude,latitude,z,dx,dy,altitude, color=colors,shade=False),因为我有更好的可视化效果,但它并不完美,尤其是边框。这是我所拥有的: The result I got with plot_trisurf

有没有办法自动去除一些三角形,以便更好地与真正的法国领土一起塑形?

0 个答案:

没有答案