栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 面试经验 > 面试问答

更改参数后如何自动更改颜色

面试问答 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

更改参数后如何自动更改颜色

首先,您应该使用一个柱状图和一个斧头形(使用更多的柱形图会使一切变得混乱)。您可以通过设置条的颜色

for bar in bars:    bar.set_color(..)

然后您可以通过更新轴的位置

line.set_ydata(position)

现在,对于每个鼠标移动事件,您需要更新轴的位置,计算百分比并将新颜色应用到条形。因此,这些事情应该在函数中完成,每次触发鼠标移动事件时都会调用该函数。应用这些设置后,需要绘制画布以使其可见。

这是完整的代码。

import pandas as pdimport numpy as npimport matplotlib.colors as mcolimport matplotlib.cm as cmimport matplotlib.pyplot as pltnp.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 percentagesdef 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 showingupdate(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()


转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/669211.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号