python - 数据不绘图有问题
问题描述
我目前在 sqlite3 中有一个数据库,其中大约有 750 行工资/智商数据。我正在尝试使用 matplotlib 在曲面图上对此进行可视化,但是使用我当前的代码,我的图上一无所获。没有打印错误让我相信我一定在某处遗漏了一些变量。
from mpl_toolkits.mplot3d import axes3d
import matplotlib.pyplot as plt
import numpy as np
import sqlite3
conn = sqlite3.connect('wages2.sqlite')
cur = conn.cursor()
cur.execute('SELECT mother_edu, log_wage_1980, tenure_1980, reference, iq FROM WageIQ')
rows = dict()
mother = list()
iq = list()
wages = list()
tenure = list()
count = 0
for medu in cur:
count = count +1
rows[count] = (medu[0],medu[1],medu[2],medu[3],medu[4])
mother.append(medu[0])
iq.append(medu[4])
wages.append(medu[1])
tenure.append(medu[2])
mother_edu = np.array([mother])
iq_a = np.array([iq])
wages_a = np.array([wages])
print(mother_edu)
fig = plt.figure()
ax = fig.add_subplot(projection='3d')
x, y, z, = mother_edu, iq_a, wages_a
ax.plot_surface(x, y, z)
[plot photo][1]
[snip of data printout][2]
AMENDED CODE BELOW...
from mpl_toolkits.mplot3d import axes3d
from matplotlib import cm
import matplotlib.pyplot as plt
from matplotlib.ticker import LinearLocator, FormatStrFormatter
import numpy as np
import sqlite3
conn = sqlite3.connect('wages2.sqlite')
cur = conn.cursor()
cur.execute('SELECT mother_edu, log_wage_1980, tenure_1980, reference, iq
FROM WageIQ')
rows = dict()
mother = list()
iq = list()
wages = list()
tenure = list()
count = 0
for medu in cur:
count = count +1
rows[count] = (medu[0],medu[1],medu[2],medu[3],medu[4])
mother.append(medu[0])
iq.append(medu[4])
wages.append(medu[1])
tenure.append(medu[2])
mother_edu = np.array(mother)
iq_a = np.array(iq)
wages_a = np.array(wages)
tenure_a = np.array(tenure)
X,Y = np.meshgrid(
np.arange(mother_edu.min(), mother_edu.max()),
np.arange(iq_a.min(), iq_a.max())
)
Z = np.empty(X.shape)
for x,y,z in zip(mother_edu, iq_a, tenure):
Z[np.where((X == x) & (Y == y))] = z
fig = plt.figure()
ax = fig.add_subplot(111, projection='3d')
surf = ax.plot_surface(X,Y,Z, cmap=cm.coolwarm,
linewidth=0, antialiased=False)
ax.set_zlim()
ax.zaxis.set_major_locator(LinearLocator(1))
ax.zaxis.set_major_formatter(FormatStrFormatter('%.02f'))
fig.colorbar(surf, shrink=0.5, aspect =5)
plt.show()
解决方案
根据我的评论(问题是您将数据强制转换为 2D 数组,但您没有使用曲面图所需的正确 XY 网格),这是一个如何操作数据以制作曲面图的示例。(当然我使用的是随机数,而你应该使用自己的数据)
import numpy as np
import matplotlib.pyplot as plt
from mpl_toolkits.mplot3d import Axes3D
# Generate random data, here you just use the data from your database
mother = np.random.randint(0,100, size=(80))
iq = np.random.randint(60,250, size=(80))
wages = np.random.randint(500,2000, size=(80))
mother_edu = np.array(mother)
iq_a = np.array(iq)
wages_a = np.array(wages)
# Generate a X,Y meshgrid, sampling the XY space in steps of 1
X,Y = np.meshgrid(
np.arange(mother_edu.min(), mother_edu.max()),
np.arange(iq_a.min(), iq_a.max())
)
# Create a 2D Z array from the 1d one
# eg: x = [x0, ....] y = [y0, ...] z = [z0, ...]
# X = [[..., x0, ...], ..., [..., x0, ...]]
# Y = [[...], ..., [y0 , y0, y0, ...], ..., [...]]
# Z = [[...], ..., [..., z0, ...], ..., [...]]
Z = np.empty(X.shape)
for x,y,z in zip(mother_edu, iq_a, wages_a):
Z[np.where((X == x) & (Y == y))] = z
fig = plt.figure()
ax = fig.add_subplot(111, projection='3d')
ax.plot_surface(X,Y,Z)
plt.show()
你的代码应该是什么样子
from mpl_toolkits.mplot3d import axes3d
import matplotlib.pyplot as plt
import numpy as np
import sqlite3
conn = sqlite3.connect('wages2.sqlite')
cur = conn.cursor()
cur.execute('SELECT mother_edu, log_wage_1980, tenure_1980, reference, iq FROM WageIQ')
rows = dict()
mother = list()
iq = list()
wages = list()
tenure = list()
count = 0
for medu in cur:
count = count +1
rows[count] = (medu[0],medu[1],medu[2],medu[3],medu[4])
mother.append(medu[0])
iq.append(medu[4])
wages.append(medu[1])
tenure.append(medu[2])
mother_edu = np.array(mother)
iq_a = np.array(iq)
wages_a = np.array(wages)
# Generate a X,Y meshgrid, sampling the XY space in steps of 1
X,Y = np.meshgrid(
np.arange(mother_edu.min(), mother_edu.max()),
np.arange(iq_a.min(), iq_a.max())
)
# Create a 2D Z array from the 1d one
# eg: x = [x0, ....] y = [y0, ...] z = [z0, ...]
# X = [[..., x0, ...], ..., [..., x0, ...]]
# Y = [[...], ..., [y0 , y0, y0, ...], ..., [...]]
# Z = [[...], ..., [..., z0, ...], ..., [...]]
Z = np.empty(X.shape)
for x,y,z in zip(mother_edu, iq_a, wages_a):
Z[np.where((X == x) & (Y == y))] = z
fig = plt.figure()
ax = fig.add_subplot(111, projection='3d')
ax.plot_surface(X,Y,Z)
plt.show()
推荐阅读
- rest - 德尔福 10.4。快速报告 6. REST 应用程序。打印失败
- c# - 如何在 ThreadStart 中使用“StartService”任务?
- python - 在 Python 中训练一个 tensorflow 模型,但在 Go 中提供服务
- nuxt.js - TypeError: this.$el.querySelector 不是函数 nuxt filepond
- regex - 如何删除字符串中的括号字符(飞镖)
- regex - 正则表达式匹配除模式之外的所有内容
- angular - 通过@input 更改角度主机
- c# - BINARY_CHECKSUM 实体框架核心 5
- ios - 具有矩形功能的相机访问 (Objc iOS)
- kubernetes - 如何配置 ClusterIssuer 以自定义证书持续时间和续订?