Table of Contents
How To Change:
To change the size of figures drawn with Matplotlib, you can use the figure()
function from the matplotlib.pyplot
module and set the figsize
parameter to a tuple of width and height in inches. For example:
import matplotlib.pyplot as plt
plt.figure(figsize=(10, 5))
plt.plot(x, y)
plt.show()
This will create a figure with a width of 10 inches and a height of 5 inches.
You can also use the rcParams
dictionary to set the default size of figures for the entire session or script. For example:
import matplotlib as mpl mpl.rcParams['figure.figsize'] = (10, 5) plt.plot(x, y) plt.show()
You can use Matplotlib to plot different types of plots such as line plots, scatter plots, histograms, etc. Here is an example of a line plot:
import matplotlib.pyplot as plt
x = [1, 2, 3, 4, 5]
y = [2, 4, 6, 8, 10]
plt.plot(x, y)
plt.show()
ou can use the savefig()
function to save the figure to a file:
plt.savefig('figure.png')
Syntax of matplotlib.pyplot:
import matplotlib.pyplot as plt
figure_name = plt.figure(figsize=(width, height))
How to use it for plotting:
To use Matplotlib for plotting, you will first need to import the matplotlib.pyplot
module, which provides a collection of functions for creating various types of plots. Here is an example of how to create a simple line plot:
import matplotlib.pyplot as plt
# create some data to plot
x = [1, 2, 3, 4, 5]
y = [2, 4, 6, 8, 10]
# create the plot
plt.plot(x, y)
# add a title and labels for the axes
plt.title("Line Plot")
plt.xlabel("X-axis")
plt.ylabel("Y-axis")
# show the plot
plt.show()
You can also use the scatter()
function to create a scatter plot, hist()
function to create a histogram and bar()
function to create a bar plot.
#create scatter plot
plt.scatter(x, y)
plt.title("Scatter Plot")
plt.xlabel("X-axis")
plt.ylabel("Y-axis")
plt.show()
#create histogram
plt.hist(y)
plt.title("Histogram")
plt.xlabel("X-axis")
plt.ylabel("Y-axis")
plt.show()
#create bar plot
plt.bar(x, y)
plt.title("Bar Plot")
plt.xlabel("X-axis")
plt.ylabel("Y-axis")
plt.show()
You can also customize the appearance of the plots, such as changing the colors, line styles, marker styles, etc. by passing additional parameters to the plotting functions.