在matplotlib中添加多行不同颜色

时间:2017-08-20 06:36:14

标签: python matplotlib plot

我试图在一个图中绘制多条线,并且我希望每条线都具有独特的颜色,我开始知道默认情况下matplotlib会这样做。但就我而言,它并不起作用。我为图中的每一行获得相同的颜色。

from sklearn import datasets
import pandas as pd
import numpy as np
import random
import matplotlib.pyplot as plt

# Loading the Boston dataset from the sklearn and creating a dataframe 
boston = datasets.load_boston()
data = pd.DataFrame(boston.data, columns = boston.feature_names)


#for dropping multiple column 
dataDrop = data.drop(['CRIM','DIS', 'ZN', 'INDUS','CHAS','NOX', 'RAD', 
'TAX','LSTAT', 'B', 'PTRATIO', 'RM'], axis=1)


#converting to numpy array
m = 20
dataArray = dataDrop['AGE'].values
absMean = dataArray.mean()

for k in range(0,10):
    n = len(dataDrop.index)
    p = random.random()
    c = int(n*p)
    #Uniform sampling of c elements from the above
    mean = 0 
    values = np.empty([1, 2])
    for i in range(0,m):
        mean = ( mean*i + np.random.choice(dataArray, c).mean() ) / (i+1)
        print mean, i
        tuple = np.array([mean, i])
        values = np.vstack([values,tuple])
    print values
    plt.plot(values[1:,1], values[1:,0])
plt.axhline(absMean, color = 'red')
plt.show()

1 个答案:

答案 0 :(得分:1)

https://matplotlib.org/users/colors.html。例如:

plt.plot(values[1:,1], values[1:,0], 'C1')