-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathMatplotlib3DAreaExample.py
More file actions
76 lines (63 loc) · 2.06 KB
/
Matplotlib3DAreaExample.py
File metadata and controls
76 lines (63 loc) · 2.06 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
#!/usr/bin/env python3
import matplotlib as mpl
import matplotlib.pyplot as plt
import numpy as np
from mpl_toolkits.mplot3d import Axes3D
mpl.style.use("seaborn")
countries = ["Brazil", "USA", "France", "Germany"]
months = ["Apr", "May", "Jun", "Jul"]
temperatures = [
# Each column is a country, each row is a month.
[24.6, 11.1, 09.9, 07.0], # Apr: Brazil, USA, France, Germany
[23.7, 16.0, 13.6, 11.7], # May: Brazil, USA, France, Germany
[22.8, 20.4, 16.9, 15.0], # Jun: Brazil, USA, France, Germany
[22.4, 22.9, 19.2, 16.6], # Jul: Brazil, USA, France, Germany
]
# For textual data we need a numeric representation.
ticks_countries = range(0, len(countries))
ticks_months = range(0, len(months))
x, y = np.meshgrid(ticks_countries, ticks_months)
z = np.asarray(temperatures)
fig, ax = plt.subplots(subplot_kw=dict(projection="3d"))
plt.title("Average Temperature in Countries")
# Plotting the area.
ax.plot_surface(
x,
y,
z,
rstride=1,
cstride=1,
cmap="viridis",
antialiased=True,
shade=True,
alpha=0.95,
edgecolor="black",
linewidth=0.3,
zorder=1,
)
# Plotting the text labels for each data point.
for month, i_month in zip(months, ticks_months):
for country, i_country in zip(countries, ticks_countries):
temperature = temperatures[i_month][i_country]
# Parameters are: position in X, position in Y, position in Z, text to plot.
ax.text(
i_country,
i_month,
temperature,
temperature,
color="black",
ha="left",
va="baseline",
bbox={"pad": 2, "alpha": 0.5, "facecolor": "white", "linewidth": 0,},
)
# Now we set the texts for the numeric representation of textual data.
ax.set_xlabel("Countries")
ax.set_xticks(ticks_countries)
ax.set_xticklabels(countries)
ax.set_ylabel("Months")
ax.set_yticks(ticks_months)
ax.set_yticklabels(months)
ax.set_zlabel("Avg Temperature (C)")
fig.set_size_inches(10, 8)
# plt.show()
plt.savefig('./Matplotlib3DAreaExample.png', dpi=150, bbox_inches='tight')