python - 为轮廓图准备数据时遇到问题
问题描述
我想在 matplot lib 中为轮廓图绘制一些数据,我想做如下的事情:
x = np.arange(0, 10, 0.5)
y = np.arange(0, 10, 0.5)
z = x**2 - y
fig, ax = plt.subplots()
cs = ax.contourf(x, y, z)
但这会发送以下错误:
TypeError: Input z must be a 2D array.
有人可以为我推荐一种方法来按摩我的数据以使轮廓快乐。理想情况下,如果有人也解释了为什么我的数据格式不起作用,那也会很有帮助。
注意:我使用的实际数据是从数据文件中读取的,与上面相同,只是将 x、y、z 分别替换为 c_a、a、Energy。
c_a
0 1.60
1 1.61
2 1.62
3 1.63
4 1.64
5 1.65
6 1.66
7 1.67
8 1.68
9 1.69
10 1.70
11 1.60
12 1.61
13 1.62
14 1.63
15 1.64
16 1.65
17 1.66
18 1.67
19 1.68
20 1.69
21 1.70
22 1.60
23 1.61
24 1.62
25 1.63
26 1.64
27 1.65
28 1.66
29 1.67
...
91 1.63
92 1.64
93 1.65
94 1.66
95 1.67
96 1.68
97 1.69
98 1.70
99 1.60
100 1.61
101 1.62
102 1.63
103 1.64
104 1.65
105 1.66
106 1.67
107 1.68
108 1.69
109 1.70
110 1.60
111 1.61
112 1.62
113 1.63
114 1.64
115 1.65
116 1.66
117 1.67
118 1.68
119 1.69
120 1.70
Name: c_a, Length: 121, dtype: float64
a
0 6.00
1 6.00
2 6.00
3 6.00
4 6.00
5 6.00
6 6.00
7 6.00
8 6.00
9 6.00
10 6.00
11 6.01
12 6.01
13 6.01
14 6.01
15 6.01
16 6.01
17 6.01
18 6.01
19 6.01
20 6.01
21 6.01
22 6.02
23 6.02
24 6.02
25 6.02
26 6.02
27 6.02
28 6.02
29 6.02
...
91 6.08
92 6.08
93 6.08
94 6.08
95 6.08
96 6.08
97 6.08
98 6.08
99 6.09
100 6.09
101 6.09
102 6.09
103 6.09
104 6.09
105 6.09
106 6.09
107 6.09
108 6.09
109 6.09
110 6.10
111 6.10
112 6.10
113 6.10
114 6.10
115 6.10
116 6.10
117 6.10
118 6.10
119 6.10
120 6.10
Name: a, Length: 121, dtype: float64
Energy
0 -250.647503
1 -250.647661
2 -250.647758
3 -250.647791
4 -250.647762
5 -250.647668
6 -250.647511
7 -250.647297
8 -250.647031
9 -250.646721
10 -250.646378
11 -250.647624
12 -250.647758
13 -250.647831
14 -250.647841
15 -250.647788
16 -250.647671
17 -250.647493
18 -250.647258
19 -250.646972
20 -250.646644
21 -250.646282
22 -250.647726
23 -250.647835
24 -250.647884
25 -250.647871
26 -250.647794
27 -250.647655
28 -250.647456
29 -250.647200
...
91 -250.647657
92 -250.647449
93 -250.647182
94 -250.646860
95 -250.646488
96 -250.646071
97 -250.645620
98 -250.645140
99 -250.647896
100 -250.647841
101 -250.647729
102 -250.647559
103 -250.647330
104 -250.647043
105 -250.646702
106 -250.646310
107 -250.645876
108 -250.645407
109 -250.644912
110 -250.647847
111 -250.647769
112 -250.647635
113 -250.647444
114 -250.647193
115 -250.646887
116 -250.646526
117 -250.646116
118 -250.645665
119 -250.645180
120 -250.644669
Name: Energy, Length: 121, dtype: float64
解决方案
x 和 y 也需要是 2d (参见meshgrid):
import numpy as np
import matplotlib.pyplot as plt
x = np.arange(0, 10, 0.5)
y = np.arange(0, 10, 0.5)
X, Y = np.meshgrid(x, y)
Z = X**2 - Y
fig, ax = plt.subplots()
cs = ax.contourf(X, Y, Z)
plt.show()
输出:
编辑:
如果您的输入是一维数组,您需要知道如何重塑它。也许它sqrt(length)=11
在你的情况下?(假设您的域是正方形)
import numpy as np
import matplotlib.pyplot as plt
c_a = np.array([np.linspace(1.6, 1.7, num=11) for _ in range(11)]).flatten()
a = np.array([[1.6+i*0.1 for j in range(11)] for i in range(11)]).flatten()
energy = np.array([-250.644-np.random.random()*0.003 for i in range(121)])
x = c_a.reshape((11, 11))
y = a.reshape((11, 11))
z = energy.reshape((11, 11))
fig, ax = plt.subplots()
cs = ax.contourf(x, y, z)
plt.show()
输出:
推荐阅读
- android - Kotlin-Android First App Unresolved Reference TextView with Button
- html - 背景图像没有留在原地
- mocking - 启用缓存时模拟服务人员和 Apollo 客户端的最佳实践
- javascript - 有没有办法使用样式化的组件有条件地更改组件的类型?
- hadoop - 什么是适用于云存储或 HDFS 的 I/O 差异?
- php - 你能在 codeigniter 视图中定义你自己的 php 函数吗?
- r - 根据种族过滤数据
- pug - PUG:为什么在尝试编译 href="#" 时出现“Assigning to rvalue”错误?
- flask - IIS 上未提供 Flask 静态文件夹
- apache-spark - 仅获取另一个数据框中存在列子集的数据框行