Interactive Slider Bar Chart Color Control

孤者浪人 提交于 2019-12-20 06:08:54

问题


I have four sets of random normal distributed numbers. The means are used to plot bar chart with each set's 95% confidence intervals plotted with errorbar.

Given a value y, four different colors will be set to the bars corresponding to the four ranges y is in: 1. lower bound to avg; 2. avg to upper bound; 3. below lower; 4. above upper.

I want to use a slider to control the y value and update the bar color each time I slide, I tried to use the following code but the bar charts cannot be plotted every update.

Could someone give me some ideas?

import matplotlib.pyplot as plt
import pandas as pd
import numpy as np
import scipy.stats as st
from matplotlib.widgets import Slider

np.random.seed(12345)

df = pd.DataFrame([np.random.normal(33500,150000,3650), 
                   np.random.normal(41000,90000,3650), 
                   np.random.normal(41000,120000,3650), 
                   np.random.normal(48000,55000,3650)], 
                  index=[1992,1993,1994,1995])

N = len(df.columns)-1  # Degree of Freedom
avg = df.mean(axis=1)  # Mean for each row
std = df.sem(axis=1)  # Unbiased Standard Deviation

year = df.index.map(str)  # Convert to String
conf95 = st.t.ppf(0.95, N)*std  # 95% Confidence Interval

upper = avg + conf95
lower = avg - conf95
colormap = ['blue', 'aqua', 'orange', 'brown']

ini = 39900
chk1 = ini>upper  # Check if y is greater than upper bound: blue
chk2 = ini<lower  # CHeck if y is smaller than lower bound: brown
chk3 = (ini>=lower) & (ini<=avg) # Check if y is in between avg and lower: orange
chk4 = (ini>avg) & (ini<=upper) # Check if y is in between avg and upper: aqua


fig, ax =plt.subplots()   
ax.bar(df.index[chk1.values], avg.iloc[chk1.values], width=1, edgecolor='k', color='blue')
ax.bar(df.index[chk2.values], avg.iloc[chk2.values], width=1, edgecolor='k', color='brown')
ax.bar(df.index[chk3.values], avg.iloc[chk3.values], width=1, edgecolor='k', color='orange')
ax.bar(df.index[chk4.values], avg.iloc[chk4.values], width=1, edgecolor='k', color='aqua')
ax.axhline(y=ini,xmin=0,xmax=10,linewidth=1,color='k')

ax.errorbar(df.index, avg, yerr=conf95, fmt='.',capsize=15, color='k')
plt.subplots_adjust(left=0.1, bottom=0.2)
plt.xticks(df.index, year)  # Map xlabel with String
plt.yticks(np.arange(0,max(avg)+1,max(avg)/5))

axcolor = 'lightgoldenrodyellow'
axy = plt.axes([0.1, 0.1, 0.7, 0.03], axisbg=axcolor)

sy = Slider(axy, 'y', 0.1, int(max(upper)+1), valinit=ini)

Until this step the color works fine. Then the update func does not work thou.

def update(val):
    ax.cla()
    yy = sy.val    
    chk1 = yy>upper
    chk2 = yy<lower
    chk3 = (yy>=lower) & (yy<=avg)
    chk4 = (yy>avg) & (yy<=upper)
    ax.bar(df.index[chk1.values], avg.iloc[chk1.values], width=1, edgecolor='k', color='blue')
    ax.bar(df.index[chk2.values], avg.iloc[chk2.values], width=1, edgecolor='k', color='brown')
    ax.bar(df.index[chk3.values], avg.iloc[chk3.values], width=1, edgecolor='k', color='orange')
    ax.bar(df.index[chk4.values], avg.iloc[chk4.values], width=1, edgecolor='k', color='aqua')
    ax.bar(df.index, avg, width=1, edgecolor='k', color='silver')
    ax.errorbar(df.index, avg, yerr=conf95, fmt='.',capsize=15, color='k')
    ax.axhline(y=yy,xmin=0,xmax=10,linewidth=1,color='k')
    fig.canvas.draw_idle()

sy.on_changed(update)  

Really appreciate any insights and Thank you guys very much!

Best Shawn


回答1:


Simply delete the line

ax.bar(df.index, avg, width=1, edgecolor='k', color='silver')

I don't know why you put it there, but it will plot a complete unicolor barchart on top of the colored bars and hide them.


In order to make some interaction possible, an interactive backend needs to be used. So it will not work out of the box in IPython when %matplotlib inline mode is set. Options you have:
  • Using %matplotlib notebook in IPython Qt console or jupyter notebook.
  • Using a GUI backend when running code as a script, by adding plt.show(). In Spyder that can be ensured by running the script in a new dedicated window as sketched here.


来源:https://stackoverflow.com/questions/43035368/interactive-slider-bar-chart-color-control

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!