Matplotlib,如何循环? [英] Matplotlib, how to loop?

查看:74
本文介绍了Matplotlib,如何循环?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

所以我在Matplotlib中有这个.

So I have this in Matplotlib.

plt.scatter(X[: , 0:1][Y == 0], X[: , 2:3][Y==0])
plt.scatter(X[: , 0:1][Y == 1], X[: , 2:3][Y==1])
plt.scatter(X[: , 0:1][Y == 2], X[: , 2:3][Y==2])

我想知道是否有更好的循环方法来代替此方法:

I'd like to know if there's a better way to loop instead of this:

for i in range(3):
  plt.scatter(X[: , 0:1][Y == i], X[: , 2:3][Y==i])

MVCE:

# CSV: https://gist.githubusercontent.com/netj/8836201/raw/6f9306ad21398ea43cba4f7d537619d0e07d5ae3/iris.csv
data = np.loadtxt('/content/drive/My Drive/Colab Notebooks/Machine Learning/iris.csv', skiprows=1, delimiter=',')

X = data[:, 0:4]
Y = data[:, 4:5]

# Scatter
for i in range(len(np.intersect1d(Y, Y))):
  plt.scatter(X[: , 0:1][Y == i], X[: , 3:4][Y==i])

# map(lambda i: plt.scatter(X[: , 0:1][Y == i], X[: , 2:3][Y==i]), range(3))

plt.title("Scatter Sepal Length / Petal Width ")
plt.legend(('Setosa', 'Versicolor', 'Virginica'))
plt.show()

推荐答案

显示数据的最简单方法可能是使用包含多种颜色的单个绘图.

Probably the simplest way to display your data is with a single plot containing multiple colors.

关键是更有效地标记数据.使用 np.intersect1d(Y,Y)您有正确的想法,但是尽管很聪明,但这不是设置唯一值的最佳方法.相反,我建议使用 np.unique .这不仅消除了将参数硬编码为 plt的需要.legend ,但是 return_inverse 参数将允许您直接构造属性.

The key is to label the data more efficiently. You have the right idea with np.intersect1d(Y, Y), but though clever, this not the best way to set up unique values. Instead, I recommend using np.unique. Not only will that remove the need to hard-code the argument to plt.legend, but the return_inverse argument will allow you to construct attributes directly.

另一个要点是,您可以使用单个索引而不是切片来索引单个列.

A minor point is that you can index single columns with a single index, rather than a slice.

例如,

X = np.loadtxt('iris.csv', skiprows=1, delimiter=',', usecols=[0, 1, 2, 3])
Y = np.loadtxt('iris.csv', skiprows=1, delimiter=',', usecols=[4], dtype=str)

labels, indices = np.unique(Y, return_inverse=True)
scatter = plt.scatter(X[:, 0], X[:, 2], color=indices)

数组 indices 索引到 labels 中的三个唯一值中,以恢复原始数组.因此,您可以提供索引作为每个元素的标签.

The array indices indexes into the three unique values in labels to get the original array back. You can therefore supply the index as a label for each element.

正如我从 matplotlib用单个散点图有多个条目,这受此解决方案的启发.其要旨是 plt.scatter 的对象> 返回具有方法 legend_elements 为您完成所有工作:

Constructing a legend for such a labeled dataset is something that matplotlib fully supports out of the box, as I learned from matplotlib add legend with multiple entries for a single scatter plot, which was inspired by this solution. The gist of it is that the object that plt.scatter returns has a method legend_elements which does all the work for you:

plt.legend(scatter.legend_elements()[0], labels)

legend_elements 返回具有两个项目的元组.第一个是带有不同标签的元素集合的句柄,这些标签可用作 legend 的第一个参数.第二个是一组基于您提供的数字标签.我们将其丢弃,取而代之的是我们的实际文本标签.

legend_elements returns a tuple with two items. The first is handle to a collection of elements with distinct labels that can be used as the first argument to legend. The second is a set of default text labels based on the numerical labels you supplied. We discard these in favor of our actual text labels.

这篇关于Matplotlib,如何循环?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆