【发布时间】:2019-11-29 13:27:54
【问题描述】:
我有一个非常简单的 matplot 图,想要更改线条颜色,当数据高于 50 时,线条将变为红色,低于 50 时,线条颜色将变为绿色。
如何更改线条颜色,目前我使用绘图加载 x 和 y 数据然后显示图表。
我尝试了两种将数据加载到图表中的方法,但它们都不起作用,也无法弄清楚我们在处理看似如此简单的事情时要做什么。
# ==========================
# First try at the changing the line colour
import matplotlib.pyplot as plt
import pandas as pd
for i in range(0,100):
if (i>50):
plt.plot(i,i, color = 'r') #plot red line
if (i<49):
plt.plot(i,i, color = 'g') #plot red line
plt.show()
# ==========================
# Second try at the changing the line colour
for i in range(0,50):
x.append(i)
y.append(i)
plt.plot(x,y, color='green')
for i in range(50,100):
x.append(i)
y.append(i)
plt.plot(x,y, color='red')
plt.show()
【问题讨论】:
标签: python matplotlib graph