繁体   English   中英

如何使用 python 根据坐标平面的面积更改点的 colors?

[英]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()

如果你能给我一些提示,那就太好了

改变点颜色的主要思想是

  • 通过xy计算颜色
  • 将颜色保存到列表中
  • 使用计算的颜色列表调用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.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM