简体   繁体   English

ValueError: 'c' 参数有 2 个不能使用的元素,在尝试 python matplotlib 散点图时

[英]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 设置为单一颜色格式字符串来完成), 如下:

  • every point will be mapped to a color matching the indexes in the 'xs' 'ys' and 'c' lists.每个点都将映射到与“xs”、“ys”和“c”列表中的索引匹配的颜色。 There has to be a color specification for each point...每个点都必须有一个颜色规范......

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:所以,总结起来,你将不得不:

  1. create a list to represent the colors创建一个列表来代表 colors
  2. fill all the 48 positions with the color representative you want用您想要的颜色代表填充所有 48 个位置
  3. Pass it to the scatter function将其传递给分散器 function

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.

相关问题 matplotlib scatter Valueerror: 'c' 参数有 n 个元素,不能与大小为 m 的 'x' 和 'y' 一起使用 - matplotlib scatter Valueerror: 'c' argument has n elements, which is not acceptable for use with 'x' and 'y' with size m ValueError: c 参数有 n 个元素,这对于大小为 0 的 x 和大小为 0 的 y 是不可接受的 - ValueError: c argument has n elements, which is not acceptable for use with x with size 0, y with size 0 matplotlib scatter 失败并出现错误:“c”参数有 n 个元素,不能与大小为 n 的“x”、大小为 n 的“y”一起使用 - matplotlib scatter fails with error: 'c' argument has n elements, which is not acceptable for use with 'x' with size n, 'y' with size n 散点问题:ValueError: 'c' 参数有 2 个元素,与大小为 70120 的 'x' 和 'y' 不一致 - Scatter Problem : ValueError: 'c' argument has 2 elements, which is inconsistent with 'x' and 'y' with size 70120 ValueError: 'c' 参数有 1000 个元素,与大小为 500 的 'x' 和 'y' 不一致 - ValueError: 'c' argument has 1000 elements, which is inconsistent with 'x' and 'y' with size 500 matplotlib rc colorcycle参数与scatterplot - matplotlib rc colorcycle argument with scatterplot 尝试在matplotlib中制作3D散点图时,'TypeError:此类型未实现' - 'TypeError: Not implemented for this type' when trying to make 3D scatterplot in matplotlib 什么时候在Python中使用分号被认为是“好的”还是“可接受的”? - When is semicolon use in Python considered “good” or “acceptable”? 散点图(matplotlib)(python)的关键错误 - Key error with scatterplot (matplotlib)(python) 尝试使用matplotlib保存图像时,Python tkinter冻结 - Python tkinter freezes when trying to use matplotlib to save an image
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM