使用字符串而不是数值在Python中创建3D散点图

huangapple go评论68阅读模式
英文:

3D Scatterplot in Python using strings instead of values

问题

我需要创建一个3D图,其中坐标轴将使用字符串标签而不是值。所以我有3个列表,大小相同,我需要将第一个列表的第一个元素作为X坐标,第二个列表的第一个元素作为Y坐标,第三个列表的第一个元素作为Z坐标。

以下是您的代码:

x = ['7th generation', '7th generation', '7th generation', '7th generation', '7th generation', 'Dawn Ultra', 'Dawn Ultra', 'Dawn Ultra', 'Dawn Ultra', 'Dawn Ultra', 'Mrs Meyers', 'Mrs Meyers', 'Mrs Meyers', 'Mrs Meyers', 'Mrs Meyers', 'Ultra Palmolive', 'Ultra Palmolive', 'Ultra Palmolive', 'Ultra Palmolive', 'Ultra Palmolive']
y = ['0.05 ml : 25 ml', '0.05 ml : 25 ml', '0.05 ml : 25 ml', '0.05 ml : 25 ml', '0.05 ml : 37.5 ml', '0.05 ml : 37.5 ml', '0.05 ml : 37.5 ml', '0.05 ml : 37.5 ml', '0.05 ml : 50 ml', '0.05 ml : 50 ml', '0.05 ml : 50 ml', '0.05 ml : 50 ml', '0.05 ml : 62.5 ml', '0.05 ml : 62.5 ml', '0.05 ml : 62.5 ml', '0.05 ml : 62.5 ml', '0.05 ml : 75 ml', '0.05 ml : 75 ml', '0.05 ml : 75 ml', '0.05 ml : 75 ml']
z = [0, 0, 0, 0, 0, 0, 3, 0, 0, 1, 1, 0, 0, 4, 1, 0, 0, 0, 1, 6]

%matplotlib notebook
from mpl_toolkits.mplot3d import Axes3D
fig = plt.figure()
ax = fig.add_subplot(111, projection='3d')
ax.scatter(x, y, z, c=y)
ax.set_xlabel("$detergent")
ax.set_ylabel("$concentration")
ax.set_z_label("$num_colon");

但它给我返回了一个错误:
ValueError: could not convert string to float: '7th generation'

错误发生在尝试将字符串转换为浮点数时,由于X和Y轴包含字符串标签而不是数值,所以导致此错误。你需要使用数值坐标来创建3D图。

英文:

I need to make a 3d plot, where the axes will have labels in string instead of values.
So I have 3 lists, the same size, and I need to have the first element of the first list to be my X coordinate, the first element of the second list to be my Y coordinate, and the first element of the third list to be my Z coordinate.

Here is my code:

x = ['7th generation', '7th generation', '7th generation', '7th generation', '7th generation', 'Dawn Ultra', 'Dawn Ultra', 'Dawn Ultra', 'Dawn Ultra', 'Dawn Ultra', 'Mrs Meyers', 'Mrs Meyers', 'Mrs Meyers', 'Mrs Meyers', 'Mrs Meyers', 'Ultra Palmolive', 'Ultra Palmolive', 'Ultra Palmolive', 'Ultra Palmolive', 'Ultra Palmolive']
y = ['0.05 ml : 25 ml', '0.05 ml : 25 ml', '0.05 ml : 25 ml', '0.05 ml : 25 ml', '0.05 ml : 37.5 ml', '0.05 ml : 37.5 ml', '0.05 ml : 37.5 ml', '0.05 ml : 37.5 ml', '0.05 ml : 50 ml', '0.05 ml : 50 ml', '0.05 ml : 50 ml', '0.05 ml : 50 ml', '0.05 ml : 62.5 ml', '0.05 ml : 62.5 ml', '0.05 ml : 62.5 ml', '0.05 ml : 62.5 ml', '0.05 ml : 75 ml', '0.05 ml : 75 ml', '0.05 ml : 75 ml', '0.05 ml : 75 ml']
z = [0, 0, 0, 0, 0, 0, 3, 0, 0, 1, 1, 0, 0, 4, 1, 0, 0, 0, 1, 6]

%matplotlib notebook 
from mpl_toolkits.mplot3d import Axes3D
fig = plt.figure()
ax = fig.add_subplot(111, projection='3d')
ax.scatter(x, y, z, c=y)
ax.set_xlabel("$detergent$")
ax.set_ylabel("$concentration$")
ax.set_zlabel("$num_colon$");

But it returns to me an error:
ValueError: could not convert string to float: '7th generation'

答案1

得分: 1

错误是因为您尝试在数值绘图中绘制字符串。您需要在绘制之前进行转换处理。一种选项是使用 scikitlearn 中的 LabelEncoder。

from sklearn.preprocessing import LabelEncoder

x_le = LabelEncoder()
y_le = LabelEncoder()
x_num = x_le.fit_transform(x)
y_num = y_le fit_transform(y)

然后,您可以绘制这些值,并将原始数组设置为标签。

from mpl_toolkits.mplot3d import Axes3D
fig = plt.figure()
ax = fig.add_subplot(111, projection='3d')
ax.scatter(x_num, y_num, z, c=y_num)

ax.set_xticks(range(0, len(x_le.classes_)))
ax.set_xticklabels(x_le.classes_())

ax.set_yticks(range(0, len(y_le.classes_)))
ax.set_yticklabels(y_le.classes_())
英文:

The error is given since you are trying to plot a string in a numeric plot. You need to handle the conversion before. An option is to use a LabelEncoder by scikitlearn.

from sklearn.preprocessing import LabelEncoder

x_le = LabelEncoder()
y_le = LabelEncoder()
x_num = x_le.fit_transform(x)
y_num = y_le.fit_transform(y)

Then you can plot the values and set the original arrays as labels

from mpl_toolkits.mplot3d import Axes3D
fig = plt.figure() 
ax = fig.add_subplot(111, projection='3d')
ax.scatter(x_num, y_num, z, c=y_num)

ax.set_xticks(range(0,len(x_le.classes_)))
ax.set_xticklabels(x_le.classes_)

ax.set_yticks(range(0,len(y_le.classes_)))
ax.set_yticklabels(y_le.classes_)

huangapple
  • 本文由 发表于 2023年2月24日 07:17:30
  • 转载请务必保留本文链接:https://go.coder-hub.com/75551273.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定