如何修改2D Scatterplot以基于csv文件中的第三个数组显示颜色? [英] How to modify 2d Scatterplot to display color based off third array in csv file?

查看:91
本文介绍了如何修改2D Scatterplot以基于csv文件中的第三个数组显示颜色?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用Python和CSV文件.我目前正在尝试修改下面的散点图(2d),以基于csv文件中的第三列更改颜色.在搜索了多个帖子之后,我基本上想使用通用的颜色图(彩虹),然后将我的第三个数组乘以颜色图,以便为每个xy点显示不同的颜色.我想我可以通过ax.scatter函数执行所有操作,但是我不确定如何将每个不同的x,y坐标乘以颜色图和第三个数组编号.它看起来应该类似于等高线图,但我希望使用其他有色散点图.

I am using Python and a CSV file. I am currently trying to modify the scatter plot(2d) below to change colors based on a third column in my csv file. After searching through multiple posts, I basically want to use a generic colormap (rainbow) and multiply my third array by the colormap in order to display different colors for each of the xy points. I think I can do everything from the ax.scatter function but I am not sure how to multiply each different x,y coordinate by the colormap and the third array number. It should look similar to a contour plot, but I would prefer a different colored scatter plot.

这是我正在使用的代码:

Here is the code I am using:

import matplotlib   
from matplotlib.backends.backend_agg import FigureCanvasAgg as FigureCanvas    
from matplotlib.figure import Figure
import matplotlib.mlab as mlab
import numpy as np

r = mlab.csv2rec('test.csv')
fig = Figure(figsize=(6,6))
canvas = FigureCanvas(fig)

ax = fig.add_subplot(111)
ax.set_title("X vs Y AVG",fontsize=14)
ax.set_xlabel("XAVG",fontsize=12)
ax.set_ylabel("YAVG",fontsize=12)
ax.grid(True,linestyle='-',color='0.75')

x = r.xavg #first column
y = r.yavg #second column
z = r.wtr #third column

ax.scatter(x,y,s=.2,c='b', marker = ',', cmap = ?);

推荐答案

查看散布线

import matplotlib.pyplot as plt
from matplotlib  import cm
import numpy as np

fig = plt.figure(figsize=(6,6))
ax = fig.add_subplot(111)
ax.set_title("X vs Y AVG",fontsize=14)
ax.set_xlabel("XAVG",fontsize=12)
ax.set_ylabel("YAVG",fontsize=12)
ax.grid(True,linestyle='-',color='0.75')
x = np.random.random(30)
y = np.random.random(30)
z = np.random.random(30)

# scatter with colormap mapping to z value
ax.scatter(x,y,s=20,c=z, marker = 'o', cmap = cm.jet );

plt.show()

它会产生

这篇关于如何修改2D Scatterplot以基于csv文件中的第三个数组显示颜色?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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