如何绘制多年来许多国家人口密度的 3d 散点图?
How to plot 3d scatter of population density in many countries over the years?
我有 160 个国家的数据集以及每个国家 12 年的人口密度。我想在 3D 散点图中绘制它,但出现此错误:
- ValueError:要解压的值太多(预期为 3)
我创建了三个列表 - "year"、"country name"、"population density"
但似乎我做对了。
这是数据集的样本:
这是我的代码:
g1 = population_density["year"]
g2 = population_density["country_name"]
g3 = population_density["population_density_(people per sq. km of land area)"]
data = (g1, g2, g3)
colors= list(np.random.choice(range(256), size=160))
groups = ("year", "population density per sq.km", "countries")
# Create plot
fig = plt.figure(figsize = (10,8))
#ax = Axes3D(fig)
ax = fig.add_subplot(111, projection='3d')
#ax = fig.gca(projection='3d')
for data, color, group in zip(data, colors, groups):
x, y, z = data
ax.scatter(x, y, z, alpha=0.8, c=color, edgecolors='none', s=30, label=group)
plt.title('Population Density Over The Years')
plt.legend(loc=2)
plt.show()
最后,我想要这个 3d 图所有年份的散点图。请帮忙!
将 ax.scatter(x, y, z, alpha=0.8, c=color, edgecolors='none', s=30, label=group)
替换为 ax.scatter(g1, g2, g3, alpha=0.8, c=color, edgecolors='none', s=30, label=group)
您应该将 x 替换为 g1,将 y 替换为 g2,将 z 替换为 g3。根据matplotlib scatter 3d中的文档,传入的参数可以是数组形式。通过使用 for
循环,您可以解压缩列表中的值。
(编辑)
查看数据集后,您在 x 和 y 轴上有分类值,但是 3d 中的散点图需要您定义笛卡尔坐标。
因此,您可以做的是设置 xticks
和 yticks
.
您可以通过此代码做到这一点
import matplotlib.pyplot as plt
from mpl_toolkits.mplot3d import Axes3D
import numpy as np
fig = plt.figure()
ax = fig.add_subplot(111, projection='3d')
g1 = population_density["year"]
g2 = population_density["country_name"]
g3 = population_density["population_density_(people per sq. km of land area)"]
data = (g1, g2, g3)
colors= list(np.random.choice(range(256), size=len(g1)))
ax.scatter(g1, range(len(g2)), g3, alpha=0.8, c=colors, edgecolors='none', s=30)
ax.set(xticks=range(len(g1)), xticklabels=g1,
yticks=range(len(g2)), yticklabels=g2,
zticks=range(len(g3)), zticklabels=g3)
ax.set_xlabel('year')
ax.set_ylabel('countries')
ax.set_zlabel('population density per sq.km')
plt.title('Population Density Over The Years')
plt.legend(loc=2)
plt.show()
我有 160 个国家的数据集以及每个国家 12 年的人口密度。我想在 3D 散点图中绘制它,但出现此错误:
- ValueError:要解压的值太多(预期为 3)
我创建了三个列表 - "year"、"country name"、"population density" 但似乎我做对了。 这是数据集的样本:
这是我的代码:
g1 = population_density["year"]
g2 = population_density["country_name"]
g3 = population_density["population_density_(people per sq. km of land area)"]
data = (g1, g2, g3)
colors= list(np.random.choice(range(256), size=160))
groups = ("year", "population density per sq.km", "countries")
# Create plot
fig = plt.figure(figsize = (10,8))
#ax = Axes3D(fig)
ax = fig.add_subplot(111, projection='3d')
#ax = fig.gca(projection='3d')
for data, color, group in zip(data, colors, groups):
x, y, z = data
ax.scatter(x, y, z, alpha=0.8, c=color, edgecolors='none', s=30, label=group)
plt.title('Population Density Over The Years')
plt.legend(loc=2)
plt.show()
最后,我想要这个 3d 图所有年份的散点图。请帮忙!
将 ax.scatter(x, y, z, alpha=0.8, c=color, edgecolors='none', s=30, label=group)
替换为 ax.scatter(g1, g2, g3, alpha=0.8, c=color, edgecolors='none', s=30, label=group)
您应该将 x 替换为 g1,将 y 替换为 g2,将 z 替换为 g3。根据matplotlib scatter 3d中的文档,传入的参数可以是数组形式。通过使用 for
循环,您可以解压缩列表中的值。
(编辑)
查看数据集后,您在 x 和 y 轴上有分类值,但是 3d 中的散点图需要您定义笛卡尔坐标。
因此,您可以做的是设置 xticks
和 yticks
.
您可以通过此代码做到这一点
import matplotlib.pyplot as plt
from mpl_toolkits.mplot3d import Axes3D
import numpy as np
fig = plt.figure()
ax = fig.add_subplot(111, projection='3d')
g1 = population_density["year"]
g2 = population_density["country_name"]
g3 = population_density["population_density_(people per sq. km of land area)"]
data = (g1, g2, g3)
colors= list(np.random.choice(range(256), size=len(g1)))
ax.scatter(g1, range(len(g2)), g3, alpha=0.8, c=colors, edgecolors='none', s=30)
ax.set(xticks=range(len(g1)), xticklabels=g1,
yticks=range(len(g2)), yticklabels=g2,
zticks=range(len(g3)), zticklabels=g3)
ax.set_xlabel('year')
ax.set_ylabel('countries')
ax.set_zlabel('population density per sq.km')
plt.title('Population Density Over The Years')
plt.legend(loc=2)
plt.show()