Python 将数据输入散点图的Matplot/pylab
我正在尝试创建一些数据的散点图。数据以列表列表中x和y坐标的形式出现 stepsPlot=[[1,0],[2,0],[2,-1],[3,-1],[3,-2],[4,-2],[4,-1],[4,0],[4,-1],[5,-1]] 运行以下代码Python 将数据输入散点图的Matplot/pylab,python,matplotlib,Python,Matplotlib,我正在尝试创建一些数据的散点图。数据以列表列表中x和y坐标的形式出现 stepsPlot=[[1,0],[2,0],[2,-1],[3,-1],[3,-2],[4,-2],[4,-1],[4,0],[4,-1],[5,-1]] 运行以下代码 import pylab as plt plt.figure('Random Walk Scatter Plot') plt.clf() plt.title('Random Walk Scatter Plot') plt.xlabel('X Axis')
import pylab as plt
plt.figure('Random Walk Scatter Plot')
plt.clf()
plt.title('Random Walk Scatter Plot')
plt.xlabel('X Axis')
plt.ylabel('Y Axis')
plt.plot(stepsPlot)
plt.show()
生成一个排序图(用于测试)——因此我知道它正在获取数据,但它不知道数据代表单个点。当我将plt.plot(stepsPlot)更改为plt.scatter(stepsPlot)时——我从阅读文档中假设这就是我所需要的——我得到了错误
TypeError:scatter()缺少1个必需的位置参数:“y”
我想这意味着pylab不明白数据代表x和y坐标
有人能告诉我哪里出了问题吗?短暂性脑缺血发作
我想这意味着pylab不理解数据
表示x和y坐标
你的假设是对的。你应该给绘图函数两个向量
1包含所有X值
第二个包含所有Y值的SysOverdrive需要提供两个向量。一种方法是将列表列表转换为dataframe,然后提供列作为两个向量
In [73]: df = pd.DataFrame(stepsPlot)
In [74]: df
Out[74]:
0 1
0 1 0
1 2 0
2 2 -1
3 3 -1
4 3 -2
5 4 -2
6 4 -1
7 4 0
8 4 -1
9 5 -1
In [75]: df.columns = ['X','Y']
In [82]: import pylab as plt
...:
...: plt.figure('Random Walk Scatter Plot')
...: plt.clf()
...: plt.title('Random Walk Scatter Plot')
...: plt.xlabel('X Axis')
...: plt.ylabel('Y Axis')
...: plt.scatter(x=df['X'],y=df['Y'])
...: plt.show()
您需要改变两件事:
- 首先,指定x轴和y轴,如下所示:
- 其次,您需要使用
而不是散点
绘图
import pylab as plt
stepsPlot = [[1, 0], [2, 0], [2, -1], [3, -1], [3, -2], [4, -2], [4, -1], [4, 0], [4, -1], [5, -1]]
plt.figure('Random Walk Scatter Plot')
plt.clf()
plt.title('Random Walk Scatter Plot')
plt.xlabel('X Axis')
plt.ylabel('Y Axis')
X = [item[0] for item in stepsPlot]
Y = [item[1] for item in stepsPlot]
plt.scatter(X, Y)
plt.show()
这将生成以下图形:
完美:-)非常感谢你,我能帮上忙!!使用numpy:
将numpy作为np导入;stepsPlot=np.array(stepsPlot);plt.plot(stepsPlot[:,0],stepsPlot[:,1],'ro')