나는 네 세트의 임의의 정규 분포 번호를가집니다. 이 수단은 각 세트의 95 % 신뢰 구간을 오류 막대와 함께 플롯하여 막대 그래프를 그리는 데 사용됩니다.대화 형 슬라이더 막대 차트 색 컨트롤
값 y가 주어지면 네 개의 범위에 해당하는 막대에 네 가지 색상이 설정됩니다. y는 다음과 같습니다. 1. 평균의 하한값. 2. 상한까지의 평균; 3. 아래; 위 4. 위.
슬라이더를 사용하여 y 값을 제어하고 슬라이드 할 때마다 막대 색을 업데이트하려면 다음 코드를 사용하려고했지만 막대 차트는 모든 업데이트를 플로팅 할 수 없습니다.
누군가 나에게 아이디어를 줄 수 있습니까?
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)
이 단계가 완료 될 때까지 색상이 정상적으로 작동합니다. 그러면 업데이트 기능이 작동하지 않습니다.
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)
정말 통찰력을 주셔서 대단히 감사합니다!
최저 숀
죄송합니다. 이전 트라이얼에서 남은 라인은 죄송합니다. –
하지만이 줄을 지울 때마다 슬라이더를 움직일 때마다 막대는 여전히 사라집니다. 당신의 방법에 대해 좀 더 힌트를 주시겠습니까? –
이전 색상의 상단에 단색 barchart를 그릴 때, 슬라이더 이벤트에서 새로운 yy 값에 따라 색상을 업데이트하는 방법>. < –