How to display a grid in Matplotlib Pyplot
This tutorial shows how to draw a grid on a plot in Python Matplotlib. We will use grid()
the function to achieve this. It also shows how to use grid()
the function parameters to customize the color and shape of the grid, or even draw only vertical or horizontal lines.
Plotting a Normal Distribution in Matplotlib
Let's first create two lists to represent the x and y values and use them to draw a graph. Call plot()
the function and pass the x and y lists as parameters, then call show()
the function.
Use the title()
, , xlabel
and ylabel()
functions to add titles and labels to your plots to make them easier to understand.
from matplotlib import pyplot as plt
x = [10, 20, 30, 40, 50, 60, 70, 80, 90, 100, 110]
y = [200, 300, 300, 350, 380, 450, 500, 500, 520, 525, 530]
plt.title("MyPlot")
plt.xlabel("X Axis")
plt.ylabel("Y Axis")
plt.plot(x, y)
plt.show()
However, plain drawings seem boring and missing something. Now, we need to add a grid to our drawing.
Use in Matplotlib plotsgrid()
We will use the Matplotlib grid()
function to draw a grid on the graph.
We need to show()
call grid()
the function before , so that a grid will be drawn on the previous figure.
Please see the code below.
from matplotlib import pyplot as plt
x = [10, 20, 30, 40, 50, 60, 70, 80, 90, 100, 110]
y = [200, 300, 300, 350, 380, 450, 500, 500, 520, 525, 530]
plt.title("MyPlot")
plt.xlabel("X Axis")
plt.ylabel("Y Axis")
plt.plot(x, y)
plt.grid()
plt.show()
Changing grid properties in Matplotlib
grid()
The function accepts parameters to customize the color and style of the grid. We can grid(color='r', linestyle='dotted', linewidth=1)
call it like this grid()
to get a red grid with dots and thin lines.
from matplotlib import pyplot as plt
x = [10, 20, 30, 40, 50, 60, 70, 80, 90, 100, 110]
y = [200, 300, 300, 350, 380, 450, 500, 500, 520, 525, 530]
plt.title("MyPlot")
plt.xlabel("X Axis")
plt.ylabel("Y Axis")
plt.plot(x, y)
plt.grid(color="r", linestyle="dotted", linewidth=1)
plt.show()
Line width is a float data type and here are all the color codes and line styles for you to choose from.
Valid color codes.
Code | color |
---|---|
b |
blue |
g |
Greenery |
r |
red |
c |
blue |
m |
Magenta |
y |
yellow |
k |
black |
w |
White |
Valid row format.
-
-
-
--
-
-.
-
:
-
None
- ``
-
solid
-
dashed
-
dashdot
-
dotted
Draw a vertical or horizontal line
grid()
The default for the function is to draw both the horizontal and vertical axes, but you may want to customize this. You can axis
do this using the parameter. grid()
When calling , use the default options to draw axis='x'
only vertical lines, axis='y'
only horizontal lines, or axis='both'
both.
Please see the following code and its output.
from matplotlib import pyplot as plt
x = [10, 20, 30, 40, 50, 60, 70, 80, 90, 100, 110]
y = [200, 300, 300, 350, 380, 450, 500, 500, 520, 525, 530]
_, (a, b, c) = plt.subplots(1, 3)
a.grid(axis="y", linestyle="dotted", color="b")
a.plot(x, y)
a.set_title("axis='y'")
b.grid(axis="x", linestyle="dotted", color="b")
b.plot(x, y)
b.set_title("axis='x'")
c.grid(axis="both", linestyle="dotted", color="b")
c.plot(x, y)
c.set_title("axis='both'")
plt.show()
For reprinting, please send an email to 1244347461@qq.com for approval. After obtaining the author's consent, kindly include the source as a link.
Related Articles
Enumerating a dictionary in Python
Publish Date:2025/05/05 Views:98 Category:Python
-
The function in Python enumerate() returns an object of enumeration type and adds a counter variable to iterate over a list or other type of collection. It makes looping over such objects easier. When we pass an enumeration object to list()
Changing dictionary values in Python
Publish Date:2025/05/05 Views:108 Category:Python
-
This tutorial will discuss various ways to change the value of a particular key in Python dictionary. We can do this by using the following methods. dict.update() method for cycle. Dictionary Unpacking dict.update() How to change dictionary
Finding the maximum value in a Python dictionary
Publish Date:2025/05/05 Views:60 Category:Python
-
This tutorial explains how to get a key with the maximum value in Python. Since the method has changed from the previous Python versions, it also lists some sample codes to clarify the concepts. Use operator.itemgetter() the method to get t
How to read input from stdin in Python
Publish Date:2025/05/05 Views:124 Category:Python
-
This tutorial discussed stdin the methods of reading input from in Python. You can read directly from the console or from a file name specified in the console. In Python, fileinput.input() use stdin fileinput We can use the read module in P
Maximum integer in Python
Publish Date:2025/05/05 Views:55 Category:Python
-
This tutorial will discuss the maximum integer value in different versions of Python and how we can get it. In Python 2, integers and long integers are different data types. The maximum value of an integer is 2 31 -1. If the value exceeds t
Get a list of time zones using Python
Publish Date:2025/05/05 Views:107 Category:Python
-
When developing real-world applications, software developers must ensure that the application can support users from both their own country and other parts of the world. Since most countries have different time zones and many people around
Convert NumPy array to list in Python
Publish Date:2025/05/05 Views:101 Category:Python
-
Lists and arrays are the two most basic and commonly used collection objects in Python. They are both mutable and are used to store a collection of elements under a common name and each element has a specific location that can be used to ac
Appending 2D Arrays in Python
Publish Date:2025/05/05 Views:64 Category:Python
-
In Python, we can have ND arrays. We can use NumPy module to process arrays in Python. This tutorial demonstrated the different methods you can use to append values to a two-dimensional array in Python. Use append() the function to ap
Sliding average of NumPy arrays in Python
Publish Date:2025/05/05 Views:190 Category:Python
-
The sliding average is often used to study time series data by calculating the average of data at a specific time interval. It is used to eliminate some short-term fluctuations and study data trends. When studying stock price trends, the si