Python 参数更改后如何自动更改颜色
在以下代码中,随着阈值的更改,条形图的颜色也会更改。我希望在OnMouseMove函数中使用y参数,以便用户可以更改“threshold”的位置,而不是在代码中使用threshold并绘制水平线。然后,我希望颜色随着y的变化而更新 我想我需要的是所谓的“观察者模式”,或者是一个使用动画工具的技巧,但不知道如何实现它。我很欣赏任何关于如何做到这一点的见解。谢谢Python 参数更改后如何自动更改颜色,python,matplotlib,Python,Matplotlib,在以下代码中,随着阈值的更改,条形图的颜色也会更改。我希望在OnMouseMove函数中使用y参数,以便用户可以更改“threshold”的位置,而不是在代码中使用threshold并绘制水平线。然后,我希望颜色随着y的变化而更新 我想我需要的是所谓的“观察者模式”,或者是一个使用动画工具的技巧,但不知道如何实现它。我很欣赏任何关于如何做到这一点的见解。谢谢 %matplotlib notebook import pandas as pd import numpy as np from scip
%matplotlib notebook
import pandas as pd
import numpy as np
from scipy import stats
import matplotlib.colors as mcol
import matplotlib.cm as cm
import matplotlib.pyplot as plt
np.random.seed(12345)
df = pd.DataFrame([np.random.normal(335,1500,300),
np.random.normal(410,900,300),
np.random.normal(410,1200,300),
np.random.normal(480,550,300)],
index=[1,2,3,4])
fig, ax = plt.subplots()
plt.show()
bars = plt.bar(range(df.shape[0]), df.mean(axis = 1), color = 'lightslategrey')
fig = plt.gcf()
threshold=420
plt.axhline(y = threshold, color = 'grey', alpha = 0.5)
cm1 = mcol.LinearSegmentedColormap.from_list("Test",["b", "white", "purple"])
cpick = cm.ScalarMappable(cmap=cm1)
cpick.set_array([])
percentages = []
for bar in bars:
percentage = (bar.get_height()-threshold)/bar.get_height()
if percentage>1: percentage = 1
if percentage<0: percentage=0
percentages.append(percentage)
cpick.to_rgba(percentages)
bars = plt.bar(range(df.shape[0]), df.mean(axis = 1), color = cpick.to_rgba(percentages))
plt.colorbar(cpick, orientation='horizontal')
def onMouseMove(event):
ax.lines = [ax.lines[0]]
plt.axhline(y=event.ydata, color="k")
fig.canvas.mpl_connect('motion_notify_event', onMouseMove)
plt.xticks(range(df.shape[0]), df.index, alpha = 0.8)
%matplotlib笔记本
作为pd进口熊猫
将numpy作为np导入
从scipy导入统计信息
将matplotlib.colors导入为mcol
将matplotlib.cm导入为cm
将matplotlib.pyplot作为plt导入
np.random.seed(12345)
df=pd.数据帧([np.随机.正常(3351500300),
np.随机。正常(410900300),
np.随机。正常(4101200300),
np.随机.正态(480550300)],
指数=[1,2,3,4])
图,ax=plt.子批次()
plt.show()
条形图=plt.bar(范围(df.shape[0]),df.mean(轴=1),颜色='浅灰色')
图=plt.gcf()
阈值=420
plt.axhline(y=阈值,颜色=灰色,alpha=0.5)
cm1=mcol.LinearSegmentedColormap.来自_列表(“测试”、“b”、“白色”、“紫色”)
cpick=cm.ScalarMappable(cmap=cm1)
cpick.set_数组([])
百分比=[]
对于酒吧中的酒吧:
百分比=(bar.get\u height()-阈值)/bar.get\u height()
如果百分比>1:百分比=1
如果百分比首先,您应该只使用一个条形图和一个axhline(使用更多将使一切变得混乱)。您可以通过设置条的颜色
for bar in bars:
bar.set_color(..)
您可以通过行更新axhline的位置。设置数据(位置)
现在,对于每个鼠标移动事件,您都需要更新axhline的位置,计算百分比并将新的颜色应用于条。所以这些事情应该在一个函数中完成,这个函数在每次触发鼠标移动事件时都会被调用。应用这些设置后,需要绘制画布,使其可见
这是一个完整的代码
import pandas as pd
import numpy as np
import matplotlib.colors as mcol
import matplotlib.cm as cm
import matplotlib.pyplot as plt
np.random.seed(12345)
df = pd.DataFrame([np.random.normal(335,1500,300),
np.random.normal(410,900,300),
np.random.normal(410,1200,300),
np.random.normal(480,550,300)],
index=[1,2,3,4])
fig, ax = plt.subplots()
threshold=420.
bars = plt.bar(range(df.shape[0]), df.mean(axis = 1), color = 'lightslategrey')
axline = plt.axhline(y = threshold, color = 'grey', alpha = 0.5)
cm1 = mcol.LinearSegmentedColormap.from_list("Test",["b", "white", "purple"])
cpick = cm.ScalarMappable(cmap=cm1)
cpick.set_array([])
plt.colorbar(cpick, orientation='horizontal')
def percentages(threshold):
percentages = []
for bar in bars:
percentage = (bar.get_height()-threshold)/bar.get_height()
if percentage>1: percentage = 1
if percentage<0: percentage=0
percentages.append(percentage)
return percentages
def update(threshold):
axline.set_ydata(threshold)
perc = percentages(threshold)
for bar, p in zip(bars, perc):
bar.set_color(cpick.to_rgba(p))
# update once before showing
update(threshold)
def onMouseMove(event):
if event.inaxes == ax:
update(event.ydata)
fig.canvas.draw_idle()
fig.canvas.mpl_connect('motion_notify_event', onMouseMove)
plt.xticks(range(df.shape[0]), df.index, alpha = 0.8)
plt.show()
将熊猫作为pd导入
将numpy作为np导入
将matplotlib.colors导入为mcol
将matplotlib.cm导入为cm
将matplotlib.pyplot作为plt导入
np.random.seed(12345)
df=pd.数据帧([np.随机.正常(3351500300),
np.随机。正常(410900300),
np.随机。正常(4101200300),
np.随机.正态(480550300)],
指数=[1,2,3,4])
图,ax=plt.子批次()
阈值=420。
条形图=plt.bar(范围(df.shape[0]),df.mean(轴=1),颜色='浅灰色')
axline=plt.axhline(y=threshold,color=grey,alpha=0.5)
cm1=mcol.LinearSegmentedColormap.来自_列表(“测试”、“b”、“白色”、“紫色”)
cpick=cm.ScalarMappable(cmap=cm1)
cpick.set_数组([])
plt.colorbar(cpick,方向=水平)
def百分比(基本要求):
百分比=[]
对于酒吧中的酒吧:
百分比=(bar.get\u height()-阈值)/bar.get\u height()
如果百分比>1:百分比=1
如果百分比年平均值是多少?df.平均值(轴=1)。谢谢这是一个错误。刚刚更正。请注意,如果要在jupyter笔记本中使用此代码,则需要使用交互式后端,%matplotlib notebook
。