需要向子图中的轴添加更多 'ticks'(大于数据的最小值和最大值)
Need to add more 'ticks' to axes in subplots (greater than min and max of data)
我需要使用 python 中的函数创建四个如下图所示的图形。 picture of 4 3d graphs using MATPLOTLIB with data centered on the y axis and all data clearly labelled
另一方面,我一直无法让我的数据像图片所示那样以 y 轴为中心。我试图添加刻度、更改刻度、增加刻度的最大值和最小值。不幸的是,我的任何尝试唯一成功的就是摆脱所有 y 轴标签并保持我的数据不变。
Picture of my four graphs using the code below that do NOT look like the 4 graphs above. (Specifically look at the y axis labels on both sets of graphs)
这是我创建四个子图的函数:
def plot3Ddata(df:pd.DataFrame):
#1st
figure = plt.figure(figsize=(12,15))
ax1=figure.add_subplot(2,2,1, projection='3d')
ax1.view_init(0,87)
ax1.scatter(xdata,ydata,zdata, color = "blue")
ax1.xaxis.set_major_locator(ticker.MultipleLocator(2))
ax1.set_xlabel("x", color = "darkred")
ax1.set_ylabel("y", color = "darkred")
ax1.set_zlabel("z", color = "darkred")
#2nd
ax2=figure.add_subplot(2,2,2, projection='3d')
ax2.view_init(36,1)
ax2.scatter(xdata,ydata,zdata, color = "blue")
ax2.set_xlabel("x", color = "darkred")
ax2.set_ylabel("y", color = "darkred")
ax2.set_zlabel("z", color = "darkred")
#3rd
ax3=figure.add_subplot(2,2,3, projection='3d')
ax3.view_init(40,40)
ax3.scatter(xdata,ydata,zdata, color = "blue")
ax3.set_xlabel("x", color = "darkred")
ax3.set_ylabel("y", color = "darkred")
ax3.set_zlabel("z", color = "darkred")
#4th
ax4=figure.add_subplot(2,2,4, projection='3d')
ax4.view_init(20,20)
ax4.scatter(xdata,ydata,zdata, color = "blue")
ax4.set_xlabel("x")
ax4.set_ylabel("y")
ax4.set_zlabel("z")
#SHOW ME THE MONEY!!
plt.show()
可以说,最简单的方法是使用 set_xticks, set_yticks, set_zticks
方法手动设置每个轴的刻度:
import numpy as np
import matplotlib.pyplot as plt
# generate random data
xdata = np.random.uniform(0, 13, 25)
ydata = np.random.uniform(-4, 4, 25)
zdata = np.random.uniform(0, 10, 25)
# Create the ticks.
xticks = np.arange(0, 15, 2)
yticks = np.arange(-6, 7, 2)
zticks = np.arange(0, 11, 2)
views = [(0,87), (36,1), (40,40), (20, 20)]
def plot3Ddata():
figure = plt.figure(figsize=(12,15))
for i in range(4):
ax=figure.add_subplot(2,2,i+1, projection='3d')
ax.view_init(*views[i])
ax.scatter(xdata, ydata, zdata, color = "blue")
ax.set_xticks(xticks)
ax.set_yticks(yticks)
ax.set_yticks(yticks)
ax.set_xlabel("x", color = "darkred")
ax.set_ylabel("y", color = "darkred")
ax.set_zlabel("z", color = "darkred")
#SHOW ME THE MONEY!!
plt.tight_layout()
plt.show()
plot3Ddata()
我需要使用 python 中的函数创建四个如下图所示的图形。 picture of 4 3d graphs using MATPLOTLIB with data centered on the y axis and all data clearly labelled
另一方面,我一直无法让我的数据像图片所示那样以 y 轴为中心。我试图添加刻度、更改刻度、增加刻度的最大值和最小值。不幸的是,我的任何尝试唯一成功的就是摆脱所有 y 轴标签并保持我的数据不变。 Picture of my four graphs using the code below that do NOT look like the 4 graphs above. (Specifically look at the y axis labels on both sets of graphs)
这是我创建四个子图的函数:
def plot3Ddata(df:pd.DataFrame):
#1st
figure = plt.figure(figsize=(12,15))
ax1=figure.add_subplot(2,2,1, projection='3d')
ax1.view_init(0,87)
ax1.scatter(xdata,ydata,zdata, color = "blue")
ax1.xaxis.set_major_locator(ticker.MultipleLocator(2))
ax1.set_xlabel("x", color = "darkred")
ax1.set_ylabel("y", color = "darkred")
ax1.set_zlabel("z", color = "darkred")
#2nd
ax2=figure.add_subplot(2,2,2, projection='3d')
ax2.view_init(36,1)
ax2.scatter(xdata,ydata,zdata, color = "blue")
ax2.set_xlabel("x", color = "darkred")
ax2.set_ylabel("y", color = "darkred")
ax2.set_zlabel("z", color = "darkred")
#3rd
ax3=figure.add_subplot(2,2,3, projection='3d')
ax3.view_init(40,40)
ax3.scatter(xdata,ydata,zdata, color = "blue")
ax3.set_xlabel("x", color = "darkred")
ax3.set_ylabel("y", color = "darkred")
ax3.set_zlabel("z", color = "darkred")
#4th
ax4=figure.add_subplot(2,2,4, projection='3d')
ax4.view_init(20,20)
ax4.scatter(xdata,ydata,zdata, color = "blue")
ax4.set_xlabel("x")
ax4.set_ylabel("y")
ax4.set_zlabel("z")
#SHOW ME THE MONEY!!
plt.show()
可以说,最简单的方法是使用 set_xticks, set_yticks, set_zticks
方法手动设置每个轴的刻度:
import numpy as np
import matplotlib.pyplot as plt
# generate random data
xdata = np.random.uniform(0, 13, 25)
ydata = np.random.uniform(-4, 4, 25)
zdata = np.random.uniform(0, 10, 25)
# Create the ticks.
xticks = np.arange(0, 15, 2)
yticks = np.arange(-6, 7, 2)
zticks = np.arange(0, 11, 2)
views = [(0,87), (36,1), (40,40), (20, 20)]
def plot3Ddata():
figure = plt.figure(figsize=(12,15))
for i in range(4):
ax=figure.add_subplot(2,2,i+1, projection='3d')
ax.view_init(*views[i])
ax.scatter(xdata, ydata, zdata, color = "blue")
ax.set_xticks(xticks)
ax.set_yticks(yticks)
ax.set_yticks(yticks)
ax.set_xlabel("x", color = "darkred")
ax.set_ylabel("y", color = "darkred")
ax.set_zlabel("z", color = "darkred")
#SHOW ME THE MONEY!!
plt.tight_layout()
plt.show()
plot3Ddata()