matplotlib:如何基于某些变量更改数据点颜色 [英] matplotlib: how to change data points color based on some variable

查看:304
本文介绍了matplotlib:如何基于某些变量更改数据点颜色的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有2个变量(x,y)随时间(t)改变.我想绘制x与t的关系图,并根据y的值为刻度线着色.例如对于y最大值,刻度颜色为深绿色,对于最小值为暗红色,对于中间值,颜色将在绿色和红色之间缩放.

I have 2 variables (x,y) that change with time (t). I want to plot x vs. t and color the ticks based on the value of y. e.g. for highest values of y the tick color is dark green, for lowest value is dark red, and for intermediate values the color will be scaled in between green and red.

这可以用python中的matplotlib完成吗?

Can this be done with matplotlib in python?

推荐答案

这是matplotlib.pyplot.scatter的用途.

作为一个简单的例子:

import matplotlib.pyplot as plt
import numpy as np

# Generate data...
t = np.linspace(0, 2 * np.pi, 20)
x = np.sin(t)
y = np.cos(t)

plt.scatter(t,x,c=y)
plt.show()

这篇关于matplotlib:如何基于某些变量更改数据点颜色的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆