![](/img/trans.png)
[英]How can I change the colors of a chart's data points depending on the value of the Y-axis? (Python: Matplotlib)
[英]How can I change the colors of the points based on the areas of the coordinate plane using python?
我是 python 编程的新手,我想根据坐标平面区域(如(∞,∞)->绿色(-∞,∞)->黄色等)更改图表中点的颜色。数据必须来自csv 文件。 我尝试了一些 if-else 条件,但无法做到。 这里的数据来自 csv
X,Y
1,3
-8,-8
8,8
8,-4
-8,-7
-14,-13
-8,-7
-16,-16
在这里我的代码,我用 arrays 尝试了一些荒谬的事情
from matplotlib import pyplot as plt
import math
import csv
import pandas as pd
path2Csv="sonuçlar.csv"
with open(path2Csv,newline='\n') as g:
reader=csv.reader(g)
dataResult=list(reader)[1:]
col_list=["X","Y"]
df = pd.read_csv(path2Csv, usecols=col_list)
xleft=[0]*len(dataResult)
xright=[0]*len(dataResult)
yup=[0]*len(dataResult)
ydown=[0]*len(dataResult)
eu1=df["X"]
eu2=df["Y"]
for i in range(len(dataResult)):
if eu1[i]<0:
xleft[i]=eu1[i]
if eu1[i]>0:
xright[i]=eu1[i]
if eu2[i]<0:
ydown[i]=eu2[i]
if eu2[i]>0:
yup[i]=eu2[i]
plt.scatter(xleft,yup,c="yellow")
plt.scatter(xleft,ydown,c="blue")
plt.scatter(xright,yup,c="gray")
plt.scatter(xright,ydown,c="green")#should i create new arrays from df["X"] and df["Y"] or
#use something else instead of scatter?
plt.show()
如果你能给我一些提示,那就太好了
改变点颜色的主要思想是
x
和y
计算颜色plt.scatter
我写了一个演示来绘制如下的颜色表。
使用HSV绘制渐变色更容易:)
import matplotlib.colors
import matplotlib.pyplot as plt
xList = []
yList = []
colors = []
count = 50
for x in range(0, count):
for y in range(0, count):
xList.append(x)
yList.append(y)
hue = x / count
saturation = (count - y) / count
colors.append(matplotlib.colors.hsv_to_rgb([hue, saturation, 1]))
plt.scatter(xList, yList, c=colors)
plt.show()
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.