user18869232
user18869232

Reputation: 5

Need to add more 'ticks' to axes in subplots (greater than min and max of data)

I need to create four graphs that look like the image below using a function in python. picture of 4 3d graphs using MATPLOTLIB with data centered on the y axis and all data clearly labelled

I on the other hand have been unable to get my data to center on the y axis like the image shows. I have attempted to add ticks, changes ticks, increase the max and min of the ticks. Unfortunately the only thing any of my attempts have managed is to get rid of all y axis labels and leave my data unmoved. 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)

This is my function to create four subplots:

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

Upvotes: 0

Views: 832

Answers (1)

Davide_sd
Davide_sd

Reputation: 13185

Arguably the easiest way is to manually set the ticks for each axis using the set_xticks, set_yticks, set_zticks methods:

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

enter image description here

Upvotes: 2

Related Questions