当前位置:网站首页>ValueError: color kwarg must have one color per data set. 9 data sets and 1 colors were provided

ValueError: color kwarg must have one color per data set. 9 data sets and 1 colors were provided

2022-06-26 00:04:00 Falling ink painting snow

About use pandas Medium read_csv() Read existing csv Data and analyze the data hist Show when histogram is drawn ValueError: color kwarg must have one color per data set. 9 data sets and 1 colors were provided Solutions for

1 Problem description

Because of practical needs , Store a one-dimensional list data obtained in the experiment , And read data from it for visual operation , For example, the topic "drawing histogram" , The example code is as follows :

import matplotlib.pyplot as plt
import pandas as pd
import numpy as np

a = np.array([1,2,3,4,5,5,5,5,6,3])# I wrote it myself 
data = pd.Series(a)
print(data.shape)
print(data)
data.to_csv('xxx.csv')
b = pd.read_csv('xxx.csv')
print(b.shape)# For inspection 
print(b.head(10))

fig = plt.figure()
ax = fig.add_subplot(1,1,1)

data = b

plt.hist(data,color='b',bins=10)#bins Is the number of intervals to be divided 
plt.show()

Some of the results are as follows :

(10,)
0    1
1    2
2    3
3    4
4    5
5    5
6    5
7    5
8    6
9    3
dtype: int32
(9, 2)
   0  1
0  1  2
1  2  3
2  3  4
3  4  5
4  5  5
5  6  5
6  7  5
7  8  6
8  9  3

Error reporting hist On ,

ValueError: color kwarg must have one color per data set. 9 data sets and 1 colors were provided

2 Problem analysis

Because something went wrong , So I added some print functions to the above code , Results found , Store data to csv front , namely to_csv and read_csv The data structure has changed , Before storage yes (10,) The change of reading after storage becomes (9,2), The reason should appear here , Therefore, the above error report should change the original column data into [[1,2,3],[1,2,3]…] Multidimensional list form of , Therefore, the above error report appears , However, there is a problem with the structure after data reading , The reason is that when reading , The originally automatically generated index page is taken as a column of data , And the original first 0 Row data as a column field results in , So it needs to be studied again ,to_csv( ) and read_csv( ) Parameter settings in .
Detailed about read_csv() For parameter problems, please move to pandas.read_csv Sort out the usage of common parameters

3 Problem solving

Don't talk much , Directly solve the code , See the effect

a = np.array([1,2,3,4,5,5,5,5,6,3])
data = pd.Series(a)
print(data.shape)
print(data)
data.to_csv('xxx.csv')
b = pd.read_csv('xxx.csv',names=['rate'])
print(b.shape)
print(b.head(10))

aa = b.loc[:,'rate']
print(aa)

fig = plt.figure()
ax = fig.add_subplot(1,1,1)

data = aa
plt.hist(data,color='b',bins=10)#bins Is the number of intervals to be divided 
plt.show()

In fact, a column name is added to the data when reading , The metadata will not be used as column names because of some default parameters , meanwhile aa = b.loc[:,'rate'] Passed in as data hist among , This ensures that automatically added... Will not be passed in index
give the result as follows :

(10,)
0    1
1    2
2    3
3    4
4    5
5    5
6    5
7    5
8    6
9    3
dtype: int32
(10, 1)
   rate
0     1
1     2
2     3
3     4
4     5
5     5
6     5
7     5
8     6
9     3
0    1
1    2
2    3
3    4
4    5
5    5
6    5
7    5
8    6
9    3
Name: rate, dtype: int64

 Insert picture description here

原网站

版权声明
本文为[Falling ink painting snow]所创,转载请带上原文链接,感谢
https://yzsam.com/2022/176/202206252107242842.html