ValueError: 'c' argument has 2 elements which is not acceptable for use, when trying python matplotlib scatterplot
ValueError: 'c' argument has 2 elements which is not acceptable for use, when trying python matplotlib scatterplot
我已经尝试调试我的代码一段时间了,我需要帮助来尝试绘制散点图。
当我试图绘制它时,它给了我一个错误:
ValueError: 'c' argument has 2 elements, which is not acceptable for use with 'x' with size 48, 'y' with size 48.
我的代码:
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()
如果我没理解错的话,你是想用两个变量和一个因子来绘制散点图。你不能这样做。但是,如果您可以将数据划分为多个因素,则可以传递颜色列表。您的数据集中的 Category
列可用于分隔您的数据。
表示匹配 'x' 和 'y' 坐标列表的索引的点。
现在,让我们找出问题所在。你得到:
ValueError: 'c' argument has 2 elements, which is not acceptable for use with 'x' with size 48, 'y' with size 48.
这意味着 散点函数中的参数 'c' 必须与 'x' 和 'y' 坐标列表(您的类别 5 编号和类别 10 编号)具有相同的大小例)。您不能传递只有 2 个元素的列表,因为 'c' 参数的工作方式(考虑到您要放弃为所有点设置相同的颜色,这可以通过将 c 设置为单一颜色来完成格式字符串),如下:
- 每个点都将映射到与 'xs' 'ys' 和 'c' 列表中的索引匹配的颜色。每个点都必须有一个颜色规范...
也就是说,如果你只给48个点2种颜色,散点函数就不知道怎么办了!
从 scatter docs,你得到 'c' 可以...
因此,总而言之,您将必须:
- 创建一个列表来表示颜色
- 全部48个位置填上你想要的颜色代表
- 传递给散点函数
查看 this answer 的第一部分,了解如何确定颜色,并理解我所说的 " same size for 'x' [=53] 的意思=] 坐标列表和 'c' ".
我已经尝试调试我的代码一段时间了,我需要帮助来尝试绘制散点图。 当我试图绘制它时,它给了我一个错误:
ValueError: 'c' argument has 2 elements, which is not acceptable for use with 'x' with size 48, 'y' with size 48.
我的代码:
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()
如果我没理解错的话,你是想用两个变量和一个因子来绘制散点图。你不能这样做。但是,如果您可以将数据划分为多个因素,则可以传递颜色列表。您的数据集中的 Category
列可用于分隔您的数据。
表示匹配 'x' 和 'y' 坐标列表的索引的点。
现在,让我们找出问题所在。你得到:
ValueError: 'c' argument has 2 elements, which is not acceptable for use with 'x' with size 48, 'y' with size 48.
这意味着 散点函数中的参数 'c' 必须与 'x' 和 'y' 坐标列表(您的类别 5 编号和类别 10 编号)具有相同的大小例)。您不能传递只有 2 个元素的列表,因为 'c' 参数的工作方式(考虑到您要放弃为所有点设置相同的颜色,这可以通过将 c 设置为单一颜色来完成格式字符串),如下:
- 每个点都将映射到与 'xs' 'ys' 和 'c' 列表中的索引匹配的颜色。每个点都必须有一个颜色规范...
也就是说,如果你只给48个点2种颜色,散点函数就不知道怎么办了!
从 scatter docs,你得到 'c' 可以...
因此,总而言之,您将必须:
- 创建一个列表来表示颜色
- 全部48个位置填上你想要的颜色代表
- 传递给散点函数
查看 this answer 的第一部分,了解如何确定颜色,并理解我所说的 " same size for 'x' [=53] 的意思=] 坐标列表和 'c' ".