我正在绘制3D散点图,并打算根据y轴(国家/地区)标签的值为每个标记提供不同的颜色。我有以下代码。标记的颜色不是他们需要的。我认为我在for循环中做错了什么或效率低下。你能指出我所犯的错误吗?
from mpl_toolkits.mplot3d import Axes3D
import matplotlib.pyplot as plt
import numpy as np
src = ['facebook', 'google', 'amazon','facebook','facebook','google']
year = [2014,2014,2013,2013,2012,2013]
country = ['uk','ru','de','us','uk','us']
avg = [154,267,187,312,274,439]
colors = {'uk' : 'b',
'de' : 'y',
'ru' : 'r',
'us' : 'c'}
unique_src, idx_src = np.unique(src, return_inverse=True)
unique_cty, idx_cty = np.unique(country, return_inverse=True)
fig = plt.figure()
ax = fig.add_subplot(111, projection='3d')
for col, val in zip(colors, country):
ax.scatter(idx_src, idx_cty, year, s=avg, c=colors[val])
plt.yticks(range(len(unique_cty)), unique_cty, rotation=340)
plt.xticks(range(len(unique_src)), unique_src, rotation=45, horizontalalignment='right')
ax.set_zticks(np.unique(year))
plt.show()
另外,在调用分散功能时,我无法写:
ax.scatter(source, country, year, s=avg, c=colors[val])
因为我收到以下错误信息:
ValueError:无法将字符串转换为float:facebook
为什么会这样?我正在使用matplotlib版本2.1.2
答案 0 :(得分:1)
您似乎希望按国家/地区着色点。
private void setListeners() {
findViewById(R.id.thirdText).setOnClickListener(new View.OnClickListener() {
@RequiresApi(api = Build.VERSION_CODES.KITKAT)
@Override
public void onClick(final View v) {
final Scene scene3 = Scene.getSceneForLayout((ViewGroup) findViewById(R.id.mainlayout),
R.layout.scene_3, MainActivity.this);
Transition mFadeTransition = new ChangeBounds();
TransitionManager.go(scene3, mFadeTransition);
setListeners();
}
});
findViewById(R.id.firsttext).setOnClickListener(new View.OnClickListener() {
@RequiresApi(api = Build.VERSION_CODES.KITKAT)
@Override
public void onClick(final View v) {
final Scene scene1 = Scene.getSceneForLayout((ViewGroup) findViewById(R.id.mainlayout),
R.layout.activity_main, MainActivity.this);
Transition mFadeTransition = new ChangeBounds();
TransitionManager.go(scene1,mFadeTransition);
setListeners();
}
});
findViewById(R.id.secondText).setOnClickListener(new View.OnClickListener() {
@RequiresApi(api = Build.VERSION_CODES.KITKAT)
@Override
public void onClick(final View v) {
final Scene scene2 = Scene.getSceneForLayout((ViewGroup) findViewById(R.id.mainlayout),
R.layout.scene_2, MainActivity.this);
Transition mFadeTransition = new ChangeBounds();
TransitionManager.go(scene2, mFadeTransition);
setListeners();
}
});
}
完整示例:
c = [colors[val] for val in country]
ax.scatter(idx_src, idx_cty, year, s=avg, c=c)