首页 > 解决方案 > 数据不绘图有问题

问题描述

我目前在 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]

绘图输出 numpy 数组输出

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()

颜色条曲面图

标签: pythonnumpymatplotlibplot

解决方案


根据我的评论(问题是您将数据强制转换为 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()

推荐阅读