简体   繁体   English

如何使用这些数据绘制散点图?

[英]How do I make a scatter plot with these data?

I am trying to make a 2D representation of a 3D data in matplotlib. 我正在尝试在matplotlib中对3D数据进行2D表示。

I have some data files, for example: 我有一些数据文件,例如:

a_1.dat
a_2.dat
a_3.dat

b_1.dat
b_2.dat
b_3.dat

From each data file I can extract the letter, the number, and a parameter associated with the letter-number pair. 我可以从每个数据文件中提取字母,数字以及与字母数字对关联的参数。

I am trying to make a scatter plot where one axis is the range of letters, another axis is the range of numbers, and the scattered points represent the magnitude of the parameter associated with each letter-number pair. 我正在尝试绘制一个散点图,其中一个轴是字母的范围,另一轴是数字的范围,散点表示与每个字母-数字对相关的参数的大小。 I would prefer is this was a 2D plot with a colorbar of some kind, as opposed to a 3D plot. 我更喜欢这是带有某种颜色条的2D图,而不是3D图。

At this point, I can make a stack of 2d numpy arrays, where each 2d array looks something like 在这一点上,我可以制作一个2d numpy数组的堆栈,其中每个2d数组看起来像

[a 1 val_a1
 a 2 val_a2
 a 3 val_a3]

[b 1 val_b1
 b 2 val_b2
 b 3 val_b3]
  • First question: Is this the best way to store the data for the plot I am trying to make? 第一个问题:这是存储我要制作的绘图数据的最佳方法吗?

  • Second question: How do I make the plot using python (I am most familiar with matplotlib pyplot )? 第二个问题:如何使用python(我对matplotlib pyplot最熟悉)进行matplotlib pyplot

To be able to fully determine if your way of storing data is correct, you should consider how you use it. 为了能够完全确定存储数据的方式是否正确,应考虑如何使用它。 If you're using it only want to use it for plotting as described here, then for the sake of the simplicity you can just use three 1D arrays. 如果您仅使用它来进行此处所述的绘图,则为简单起见,您可以仅使用三个一维数组。 If, however, you wish to achieve tighter structure, you might consider using a 2D array with custom dtype . 但是,如果您希望获得更紧密的结构,则可以考虑使用带有自定义dtype的2D数组。

Having this in mind, you can easily create a 2D scatter plot with different colors, where exact color is determined by the value associated with each pair (letter, number). 考虑到这一点,您可以轻松地创建具有不同颜色的2D散点图,其中确切的颜色由与每对关联的值(字母,数字)确定。

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

# You might note that in this simple case using numpy for creating array
# was actually unnecessary as simple lists would suffice
letters = np.array(['a', 'a', 'a', 'b', 'b', 'b'])
numbers = np.array([1, 2, 3, 1, 2, 3])
values = np.array([1, 2, 3, 1.5, 3.5, 4.5])

items = len(letters)

# x and y should be numbers, so we first feed it some integers
# Parameter c defines color values and cmap defines color mappings
plt.scatter(xrange(items), numbers, c=values, cmap=cm.jet)

# Now that data is created, we can re-set xticks
plt.xticks(xrange(items), letters)

代码结果

Hopefully, this should be enough for a good start. 希望这足以为一个良好的开端。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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