首页 > 解决方案 > 如何在 matplotlib 中删除 Axes3D 图形上的比例/轴?

问题描述

我正在使用 python 在 jupyter notebook 中绘制 3D 图。现在我想在我的图中删除这个二级比例(不是 x、y、z 轴)。我曾尝试使用“axes.axis(“off”)”,但是,这只会禁用我想要保留的正常 x、y、z 轴。我将附上我的代码和 2 张图像,一张带有 3 个轴 + 我希望删除的比例,另一张带有我想禁用的轴和剩余的比例。为了清晰起见,我想离开,任何帮助将不胜感激。

import warnings
warnings.simplefilter(action = 'ignore', category = FutureWarning)

import pandas as pd
import numpy as np
import seaborn as sns
import matplotlib
import matplotlib.pyplot as plt

from sklearn.linear_model import LinearRegression
from sklearn.preprocessing import PolynomialFeatures
from sklearn.preprocessing import MinMaxScaler
from sklearn.metrics import r2_score

from scipy.stats import norm

from mpl_toolkits.mplot3d import Axes3D

import math

pd.options.mode.chained_assignment = None

data = pd.read_csv("./kc_house_data.csv")

size = data['sqft_living'].values.reshape(-1, 1)
price = data['price'].values.reshape(-1, 1)
bedrooms = data['bedrooms'].values.reshape(-1, 1)

size_scaler = MinMaxScaler()
price_scaler = MinMaxScaler()
bedroom_scaler = MinMaxScaler()

norm_size = size_scaler.fit_transform(size).reshape(-1)
norm_price = price_scaler.fit_transform(price).reshape(-1)
norm_bedrooms = bedroom_scaler.fit_transform(bedrooms).reshape(-1)

graphWidth = 400
graphHeight = 300

f = plt.figure(figsize = (graphWidth / 100.0, graphHeight / 100.0), dpi = 100)
matplotlib.pyplot.grid(True)
axes = Axes3D(f)
axes.scatter(norm_size, norm_bedrooms, norm_price)
plt.show()

这是带有轴+我要删除的比例的图像:

这是只有比例的图像(我要删除的元素)

https://drive.google.com/file/d/1N5lTwj3s1tVgy_VuvHcSvf6p259wgDJJ/view?usp=sharing

澄清一下,预期的结果将是第二张图像仅显示图形,而第一张图像仅具有 x、y、z 轴。

标签: pythonmatplotlibjupyter-notebook

解决方案


如果需要,您可以关闭grid并旋转。你也有一些你没有使用的导入,我暂时删除了它们。

import warnings
warnings.simplefilter(action = 'ignore', category = FutureWarning)

import pandas as pd
import numpy as np
import matplotlib
import matplotlib.pyplot as plt
from sklearn.linear_model import LinearRegression
from sklearn.preprocessing import PolynomialFeatures
from sklearn.preprocessing import MinMaxScaler
from sklearn.metrics import r2_score
from scipy.stats import norm
from mpl_toolkits.mplot3d import Axes3D
import math

pd.options.mode.chained_assignment = None

data = pd.read_csv("kc_house_data.csv")

size = data['sqft_living'].values.reshape(-1, 1)
price = data['price'].values.reshape(-1, 1)
bedrooms = data['bedrooms'].values.reshape(-1, 1)

size_scaler = MinMaxScaler()
price_scaler = MinMaxScaler()
bedroom_scaler = MinMaxScaler()

norm_size = size_scaler.fit_transform(size).reshape(-1)
norm_price = price_scaler.fit_transform(price).reshape(-1)
norm_bedrooms = bedroom_scaler.fit_transform(bedrooms).reshape(-1)

graphWidth = 400
graphHeight = 300

f = plt.figure(figsize = (graphWidth / 100.0, graphHeight / 100.0), dpi = 100)

axes = Axes3D(f)
axes.scatter(norm_size, norm_bedrooms, norm_price, cmap="RdYlGn", edgecolors="black")
plt.show()

在此处输入图像描述


推荐阅读