Python 事件调用不会更改阵列后半部分的条形图颜色

Python 事件调用不会更改阵列后半部分的条形图颜色,python,matplotlib,event-handling,Python,Matplotlib,Event Handling,这是一种试图实现条形图的尝试,其中条形图根据鼠标指针y轴触发的事件与绘图中条形图的接近程度而改变颜色 观察到的一个奇怪的效果是,只有前半部分的颜色(从左到右)在变化,而不是后半部分。知道为什么吗 fig, axes = plt.subplots(1,2) ax = axes[0] #ax = plt.subplot(1,2,1) #ax = fig.gca() y = [2009, 2010, 2011, 2012, 2013, 2014, 2015, 2016, 2017, 2018] x =

这是一种试图实现条形图的尝试,其中条形图根据鼠标指针y轴触发的事件与绘图中条形图的接近程度而改变颜色

观察到的一个奇怪的效果是,只有前半部分的颜色(从左到右)在变化,而不是后半部分。知道为什么吗

fig, axes = plt.subplots(1,2)
ax = axes[0]
#ax = plt.subplot(1,2,1)
#ax = fig.gca()
y = [2009, 2010, 2011, 2012, 2013, 2014, 2015, 2016, 2017, 2018]
x = [5401.565
    ,5635.739
    ,6092.861
    ,7245.169
    ,7586.334
    ,7708.000
    ,7516.826
    ,6872.729
    ,6080.949
    ,5303.580]
bars = ax.bar(y
         ,x
#        , color=color_map(data_normalizer(df.iloc[0][23:]))
       )
text=ax.annotate( ""
                 ,xy=(0,0)
                 ,xytext=(0,0)
                 ,textcoords="offset points"
                 ,xycoords = "figure points"
                )

horizontalLine = 0
lineLabel = ax.annotate(""
                       ,xy=(2010, 0) #(event.xdata, event.ydata)
                         ,xycoords = "data"
                       )

# Color map showing probability of proximity with the line, with increments of 10%
colorMap = ['#a50026','#d73027','#f46d43','#fdae61','#fee090','#e0f3f8','#abd9e9','#74add1','#4575b4','#313695']

def onmove(event):
    if (event.inaxes == ax):
        tx = ' x=%d, y=%d, xdata=%f, ydata=%f' % (event.x, event.y, event.xdata, event.ydata)    
        if (len(ax.lines)>0):
            ax.lines[-1].remove()
        event.inaxes.axhline(event.ydata,0, 1, lw=1 )
        lineLabel.set_y(event.ydata)
        lineLabel.set_text("y = %.2f"%(event.ydata))
        text.set_text(tx)
        for bar in bars:
            bar_line_gap = bar.get_height() - event.ydata
            if(bar_line_gap < 0): # probability is 100%, show color 
                bar.set_color(colorMap[-1])
            else:
                proximityPercentageIndex = round(10*bar_line_gap/event.ydata)
                if (proximityPercentageIndex != 0):
                    bar.set_color(colorMap[proximityPercentageIndex])
                else:
                    bar.set_color('#000000')
    
cid = fig.canvas.mpl_connect('motion_notify_event', onmove)
图,轴=plt.子批次(1,2)
ax=轴[0]
#ax=零件子批次(1,2,1)
#ax=图gca()
y=[2009、2010、2011、2012、2013、2014、2015、2016、2017、2018]
x=[5401.565
,5635.739
,6092.861
,7245.169
,7586.334
,7708.000
,7516.826
,6872.729
,6080.949
,5303.580]
杆=最大杆(y
,x
#,color=color\u映射(数据规范化器(df.iloc[0][23:]))
)
text=ax.注释(“”)
,xy=(0,0)
,xytext=(0,0)
,textcoords=“偏移点”
,xycoords=“图形点”
)
水平线=0
lineLabel=ax.注释(“”)
,xy=(2010,0)#(event.xdata,event.ydata)
,xycoords=“数据”
)
#彩色地图,显示与线路接近的概率,增量为10%
彩色地图=['#a50026'、'#d73027'、'#f46d43'、'#fdae61'、'#fee090'、'#e0f3f8'、'#abd9e9'、'74add1'、'4575b4'、'313695']
def onmove(事件):
如果(event.inaxes==ax):
tx='x=%d,y=%d,扩展数据=%f,ydata=%f%%(event.x,event.y,event.xdata,event.ydata)
如果(长度(最大线)>0):
ax.lines[-1]。删除()
event.inaxes.axhline(event.ydata,0,1,lw=1)
lineLabel.set_y(event.ydata)
lineLabel.set_文本(“y=%.2f”%(event.ydata))
文本。设置文本(tx)
对于酒吧中的酒吧:
bar\u line\u gap=bar.get\u height()-event.ydata
如果(条线间距<0):#概率为100%,显示颜色
设置颜色(颜色映射[-1])
其他:
proximityPercentageIndex=圆形(10*bar\u line\u间隙/event.ydata)
如果(proximityPercentageIndex!=0):
设置颜色(颜色映射[proximityPercentageIndex])
其他:
bar.set_颜色('#000000')
cid=图canvas.mpl\u connect('motion\u notify\u event',onmove)

您的代码有三个问题:

1.
proximityPercentageIndex=圆形(10*bar\u line\u gap/event.ydata)
,乘以9而不是10

2.
proximityPercentageIndex=round(10*bar\u line\u gap/event.ydata)
,我想应该除以
bar.get\u height()

3.调用
fig.canvas.draw()
内部
onmove()
,至少对于mydesktop中的matplotlib版本

fig, axes = plt.subplots(1,2)
ax = axes[0]
#ax = plt.subplot(1,2,1)
#ax = fig.gca()
y = [2009, 2010, 2011, 2012, 2013, 2014, 2015, 2016, 2017, 2018]
x = [5401.565,5635.739,6092.861,7245.169,7586.334,7708.000,7516.826,6872.729,6080.949,5303.580]
bars = ax.bar(y,x)
text=ax.annotate( "",xy=(0,0),xytext=(0,0),textcoords="offset points",xycoords = "figure points")

horizontalLine = 0
lineLabel = ax.annotate("",xy=(2010, 0),xycoords = "data")

# Color map showing probability of proximity with the line, with increments of 10%
colorMap = ['#a50026','#d73027','#f46d43','#fdae61','#fee090','#e0f3f8','#abd9e9','#74add1','#4575b4','#313695']

def onmove(event):
    if (event.inaxes == ax):
        tx = ' x=%d, y=%d, xdata=%f, ydata=%f' % (event.x, event.y, event.xdata, event.ydata)    
        if (len(ax.lines)>0):
            ax.lines[-1].remove()
        event.inaxes.axhline(event.ydata,0, 1, lw=1 )
        lineLabel.set_y(event.ydata)
        lineLabel.set_text("y = %.2f"%(event.ydata))
        text.set_text(tx)
        for bar in bars:
            bar_line_gap = bar.get_height() - event.ydata
            if(bar_line_gap < 0): # probability is 100%, show color 
                bar.set_color(colorMap[-1])
            else:
                # should times 9, not 10, as 0-9 got 10 integers
                # also should divide bar.get_height()
                proximityPercentageIndex = round(9*(bar_line_gap)/bar.get_height())
                if (proximityPercentageIndex != 0):
                    bar.set_color(colorMap[proximityPercentageIndex])
                else:
                    bar.set_color('#000000')
        fig.canvas.draw()
    
cid = fig.canvas.mpl_connect('motion_notify_event', onmove)
图,轴=plt.子批次(1,2)
ax=轴[0]
#ax=零件子批次(1,2,1)
#ax=图gca()
y=[2009、2010、2011、2012、2013、2014、2015、2016、2017、2018]
x=[5401.5655635.7396092.8617245.1697586.3347708.0007516.8266872.7296080.9495303.580]
钢筋=最大钢筋(y,x)
text=ax.注释(“,xy=(0,0),xytext=(0,0),textcoords=“偏移点”,xycoords=“地物点”)
水平线=0
lineLabel=ax.annotate(“,xy=(2010,0),xycoords=“data”)
#彩色地图,显示与线路接近的概率,增量为10%
彩色地图=['#a50026'、'#d73027'、'#f46d43'、'#fdae61'、'#fee090'、'#e0f3f8'、'#abd9e9'、'74add1'、'4575b4'、'313695']
def onmove(事件):
如果(event.inaxes==ax):
tx='x=%d,y=%d,扩展数据=%f,ydata=%f%%(event.x,event.y,event.xdata,event.ydata)
如果(长度(最大线)>0):
ax.lines[-1]。删除()
event.inaxes.axhline(event.ydata,0,1,lw=1)
lineLabel.set_y(event.ydata)
lineLabel.set_文本(“y=%.2f”%(event.ydata))
文本。设置文本(tx)
对于酒吧中的酒吧:
bar\u line\u gap=bar.get\u height()-event.ydata
如果(条线间距<0):#概率为100%,显示颜色
设置颜色(颜色映射[-1])
其他:
#应该乘以9,而不是10,因为0-9得到10个整数
#还应划分条。获取_高度()
proximityPercentageIndex=圆形(9*(条线间隙)/条线高度()
如果(proximityPercentageIndex!=0):
设置颜色(颜色映射[proximityPercentageIndex])
其他:
bar.set_颜色('#000000')
图canvas.draw()
cid=图canvas.mpl\u connect('motion\u notify\u event',onmove)

您的代码没有为我运行(proximityPercentageIndex的计算结果为浮点)。即使我将其转换为
int
,它给出的索引数也远远超过10。请添加相关信息,例如,如果这不是最新matplotlib版本的Python 3,而是更旧的版本。