CoCalc Public FilesJupyterNotebooks / in with one click!
Author: Mark Olson
Views : 46
Description: Customising the x axis and y axis of a matplotlib plot
In [1]:
%matplotlib inline # Import Libraries import matplotlib.pyplot as plt import numpy as np # Define the figure fig = plt.figure() # Define the axes of the subplot. # The figure will contain an a x b arrangement # of subplots of axes ax. This is subplot c. # add_subplot(a,b,c) ax = fig.add_subplot(1,1,1) # Subplot title ax.set_title(r"Plot of $y=2\sin(x)$") # Axes x-axis Customizations ax.xaxis.set_label_coords(1,0.5) ax.set_xlabel(r"$x$") ax.xaxis.set_ticks_position("bottom") # Axes y-axis Customizattions ax.yaxis.set_label_coords(0.5,1) # Absolute Coordinates where (0,0) Bottom-Left (1,1) Top-Right ax.set_ylabel(r"$y$",rotation="horizontal") ax.yaxis.set_label_position("right") ax.yaxis.set_ticks_position("left") # Axes Spine Customizations ax.spines["left"].set_position("center") ax.spines["left"].set_smart_bounds(True) ax.spines["bottom"].set_position("center") ax.spines["bottom"].set_smart_bounds(True) ax.spines["right"].set_visible(False) ax.spines["top"].set_visible(False) # Define the set of values of the independent variable x x = np.linspace(-np.pi, np.pi, 100) # Define the set of values of the dependent variable y y = 2 * np.sin(x) # Plot the curve on the axes ax ax.plot(x,y) # Save the plot as a png file fig.savefig("plot.png")
In [ ]: