条形图上的负值使用不同的颜色-Python

时间:2018-12-06 19:45:18

标签: python matplotlib bar-chart

这是一些我必须绘制降水异常条形图的代码。我想用其他颜色显示负值。我尝试查找一些东西,似乎我必须为带有两个子图的负值创建一个不同的列表。有没有更简单的方法可以做到这一点?

def delta_mean_precipitation():
    """Delta_mean_precipitation addresses task 7 of the project and is a function that takes no arguments and gives us the 
    rate of change in average precipitation year-to-year. The goal is to create a bar chart representing the change in value
    of global mean precipitation between each pair of consecutive years. A lot of the code is similar to the the anomoly code 
    """
    data=np.load('annual_precip.npy') #load the data
    data=data.astype(np.float) #convert type to float
    yearly_precip=[] #this will only have 115 values 
    for i in range(115):
        rate_of_change=[] #create an empty array called rate of change. This will account for ALL the years 
        for j in range (85794):
            avg_precip=data[i][j][4]
            rate_of_change.append(float(avg_precip))#add all the average values to the empty array 
        yearly_precip.append(sum(rate_of_change)/(85794)) #sicne I want the average for each year, I will make an array with ALL of the data from the year and then take the average. The average will then be added to a new list. 
    year_to_year=[] #create an empty array 
    for k in range(114):
        year_to_year.append(yearly_precip[k]-yearly_precip[k-1]) #add to the list by using the yearly_precip array 
    x=[i for i in range (1901,2015)] #line 199 - 205 are plot parameters 
    y=year_to_year
    pl.figure(figsize=(16,8))
    pl.bar(x,y)
    pl.title('Rate of change in global annual mean precipition between 1900 and 2014')
    pl.xlabel('Year')
    pl.ylabel('Rate of change in mean precipitation mL/year')
    pl.show

0 个答案:

没有答案