matplotlib.pyplot使用x坐标,y坐标和颜色列表的散点图线 [英] matplotlib.pyplot scatterplot lines using lists for x-coordinates, y-coordinates, and colors

查看:1977
本文介绍了matplotlib.pyplot使用x坐标,y坐标和颜色列表的散点图线的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

Matplotlib用线连接散点图 - Python



检查了这一点,他们的解决方案非常简单,使用plt.plot(x_coordinates,y_coordinates,'-o'),但我有一个我使用的颜色列表,所以我不能使用这种方法。他们是RGB颜色。 (也不知道为什么颜色在同一系列中交替)



如何将这些点连接到与标记颜色相同的线?
pre $ import matplotlib.pyplot as plt
import random
x_coordinates = [(range(1, (0,len(4))] * 2
y_coordinates = [[3,4,5],[2,2,2]]
color_map = []
x(x)):
r = lambda:random.randint(0,255)
rgb_hex =('#%02X%02X%02X'%(r(),r(),r()))
color_map.append(rgb_hex)
plt.scatter(x_coordinates,y_coordinates,c = color_map)
plt.show()


解决方案

您可以绘制散点图后面的线,并将 zorder 设置为确保该线是在点后面。

  import matplotlib.pyplot as plt 
import random

x_coordinates = [(range(1,65))] * 2
y_coordinates = [[random.randint(0,10)for i in range(0,64)] * 2]
color_map = []

(0,len(x_coordinates)):
r = lambda:random.randint(0,255)
rgb_hex =('#%02X%02X%02X' %(r(),r(),r()))
color_map.append(rgb_hex)
plt.plot(x_coordinates [0],y_coordinates [0] [:len(x_coordinates [0] )],' - ',
zorder = 2,color =(。7,)* 3)
plt.scatter(x_coordinates,y_coordinates,c = color_map,s = 60,zorder = 3)

plt.show()


Matplotlib connect scatterplot points with line - Python

Checked this out and their solution was very simple, use plt.plot(x_coordinates, y_coordinates, '-o') but I have a list of colors that i'm using so I can't use this method. They are RGB colors. (Also not sure why colors are alternating within the same series)

How can I connect these points with lines that are the same color as the markers?

import matplotlib.pyplot as plt
import random
x_coordinates = [(range(1,4))]*2
y_coordinates = [[3,4,5],[2,2,2]]
color_map = []
for i in range(0,len(x_coordinates)):
    r = lambda: random.randint(0,255)
    rgb_hex = ('#%02X%02X%02X' % (r(),r(),r()))
    color_map.append(rgb_hex)
plt.scatter(x_coordinates,y_coordinates,c=color_map)
plt.show()

解决方案

You can plot the line behind the scatter plot, and set the zorder to ensure that the line is behind the points.

import matplotlib.pyplot as plt
import random

x_coordinates = [(range(1,65))]*2
y_coordinates = [[random.randint(0,10) for i in range(0,64)]*2]
color_map = []

for i in range(0,len(x_coordinates)):
    r = lambda: random.randint(0,255)
    rgb_hex = ('#%02X%02X%02X' % (r(),r(),r()))
    color_map.append(rgb_hex)
plt.plot(x_coordinates[0],y_coordinates[0][:len(x_coordinates[0])],'-', 
                          zorder=2, , color=(.7,)*3)
plt.scatter(x_coordinates,y_coordinates,c=color_map, s=60, zorder=3)

plt.show()

这篇关于matplotlib.pyplot使用x坐标,y坐标和颜色列表的散点图线的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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