更改Matplotlib条形图的正值/负值的颜色

2024-06-28 20:53:46 发布

您现在位置:Python中文网/ 问答频道 /正文

我希望根据是否有任何负值来更改条形图的颜色。我希望正值为深红色,负值为深蓝。我似乎找不到实现这一目标的最佳方法

在我尝试进行更改之前,我的图形是什么样子的:Link to figure

下面是我的代码段,最后四行是问题所在(TypeError:“>;=”在“list”和“int”实例之间不受支持):

    filename='2015_tAnoms.csv'

    lat = []
    tAnom = []
    with open(filename,'r') as csvfile:
      points = csv.reader(csvfile, delimiter=',')
      next(points)
      for row in points:
        lat.append(int(row[0]))
        tAnom.append(Decimal(row[1]))


    import matplotlib.pyplot as plt
    from matplotlib.pyplot import figure
    import numpy as np
   
    figure(num=None, figsize=(8, 6), dpi=300, facecolor='w', edgecolor='k')

    axes = plt.gca()
    axes.grid(color='lightgray', alpha=0.6, which='major', axis='y', linestyle='-')
    axes.set_facecolor((0,0,0))
    axes.set_xlim([-90,90])
    axes.set_ylim([-1,3])

    lat = np.array(lat)
    tAnom = np.array(tAnom)

    posValues = tAnom >= 0
    negValues = tAnom < 0

    plt.bar(lat[posValues],tAnom[posValues],color='crimson')
    plt.bar(lat[negValues],tAnom[negValues],color='deepskyblue')

Image with correct coloring, but squished data


Tags: importasnppltpointscolorrowfigure