[英]ValueError: 'c' argument has 2 elements which is not acceptable for use, when trying python matplotlib scatterplot
I have been trying to debug my code for a while, and i need help in trying to plot a scatterplot.我一直在尝试调试我的代码一段时间,我需要帮助来尝试 plot 散点图。 When i tried to plot it, it gave me an error stated:当我尝试 plot 它时,它给了我一个错误说明:
ValueError: 'c' argument has 2 elements, which is not acceptable for use with 'x' with size 48, 'y' with size 48. ValueError: 'c' 参数有 2 个元素,不能与大小为 48 的 'x' 和大小为 48 的 'y' 一起使用。
The dataset: https://data.gov.sg/dataset/monthly-revalidation-of-coe-of-existing-vehicles?view_id=b228d20d-5771-48ec-9d7b-bb52351c0f7d&resource_id=e62a59fd-ee9f-43ec-ac69-58dc5c8045be数据集: https://data.gov.sg/dataset/monthly-revalidation-of-coe-of-existing-vehicles?view_id=b228d20d-5771-48ec-9d7b-bb52351c0f7d&resource_id=e682459fd-5ecdc5c5ac-
My code:我的代码:
import numpy as np #importing numpy as np declaring as np
import matplotlib.pyplot as plt #importing matplotlib pyplot as plt
title = "COE revalidation" #title of the output
titlelen = len(title)
print("{:*^{titlelen}}".format(title, titlelen=titlelen+6))
print()
recoe = np.genfromtxt("data/annual-revalidation-of-certificate-of-entitlement-coe-of-existing-vehicles.csv", #loading dataset, storing it as recoe
dtype=(int,"U12","U18",int),
delimiter=",",
names=True)
years = np.unique(recoe["year"]) #extracting unique values from year column, storing it as years
type = np.unique(recoe["type"]) #extracting unique values from type column, storing it as type
category = np.unique(recoe["category"]) #extracting unique values from category column, storing it as category
category5 = recoe[recoe["type"]=="5 Year"] #extracting coe 5 year, storing it as category5
category10 = recoe[recoe["type"]=="10 Year"] #extracting coe 10 year, storing it as category10
category5numbers = category5["number"] #extracting 'number' from category5 and storing it as category5numbers (number of revalidation , 5 years)
category10numbers = category10["number"] #extracting 'number' from category10 and storing it as category5numbers (number of revalidation , 10 years)
colours =['tab:blue', 'tab:orange']
plt.figure(figsize=(7, 6))
plt.scatter(category5numbers,category10numbers,c= colours ,linewidth=1,alpha=0.75,edgecolor='black',s=200)
plt.title("Scatter Plot of category5 versus category10")
plt.xlabel("number of category 5 revalidation")
plt.ylabel("number of category 10 revalidation")
plt.tight_layout()
plt.show()
If I understand correctly, you are trying to make a scatter plot using two variables but a single factor.如果我理解正确,您正在尝试使用两个变量但一个因素来制作分散 plot。 You can't do this.你不能这样做。 You can, however, pass a list of colors if you can divide your data into multiple factors.但是,如果您可以将数据划分为多个因素,则可以传递 colors 列表。 The Category
column in your data set can be used to separate your data.数据集中的Category
列可用于分隔数据。
A point is represented matching the indexes of the 'x' and 'y' coordinates lists.表示与“x”和“y”坐标列表的索引匹配的点。
Now, let's figure out the problem.现在,让我们找出问题所在。 You get:你得到:
ValueError: 'c' argument has 2 elements, which is not acceptable for use with 'x' with size 48, 'y' with size 48. ValueError: 'c' 参数有 2 个元素,不能与大小为 48 的 'x' 和大小为 48 的 'y' 一起使用。
That means the parameter 'c' in the scatter function has to to have the same size than 'x' and 'y' coordinates lists (category5numbers and category10numbers in your case) .这意味着散点图 function 中的参数 'c' 必须具有与 'x' 和 'y' 坐标列表相同的大小(在您的情况下为 category5numbers 和 category10numbers) 。 You can't pass a list with only 2 elements, because the way the 'c' parameter works (given the fact you are discarding setting the same color to all the points, which can be done by setting c to a single color format string), is the following:您不能传递只有 2 个元素的列表,因为 'c' 参数的工作方式(考虑到您放弃为所有点设置相同颜色的事实,这可以通过将 c 设置为单一颜色格式字符串来完成), 如下:
That said, if you give only 2 colors for 48 points, the scatter function does not know what to do!也就是说,如果你只给 2 个 colors 48 分,散点 function 不知道该怎么办!
From the scatter docs , you get that 'c' can be...从scatter docs中,您可以看到 'c' 可以是...
So, summing up, you will have to:所以,总结起来,你将不得不:
Check out the first part of this answer to see how you can determine the colors, and to understand what I mean by saying " same size for 'x' 'y' coordinates lists and 'c' " .查看此答案的第一部分,了解如何确定 colors,并理解我所说的“'x''y' 坐标列表和 'c' 大小相同”的意思。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.