How to turn off the scale value display while leaving the grid with matplotlib

problem

If you simply display the grid with grid () in matplotlib It only draws a line where the scale is displayed

import numpy as np
import matplotlib.pyplot as plt

x = np.arange(0, 2 * 100 + 1)
f = sin(x)

plt.rc('font', family='serif')
plt.figure()
plt.ylim([-1.5, 1.5])
plt.plot(f, color='black')
plt.grid()
plt.show()

例1.png

Suppose you want to add 5 lines on the x-axis and 0.1 lines on the y-axis to this graph. Since grid "draws a line where the scale is displayed", try increasing the scale using xticks and yticks.

plt.rc('font', family='serif')
plt.figure()
plt.ylim([-1.5, 1.5])
plt.plot(f, color='black')

#Scale display in 5 increments
plt.xticks(list(filter(lambda x: x%5==0, np.arange(201))))
# 0.Scale display in 1 increments
plt.yticks(list(map(lambda y: y*0.1, np.arange(-15,15))))

plt.grid()
plt.show()

例2.png

Although it is displayed like this, the value display on the scale is terribly difficult to see.

solution

In the first place, matplotlib scales have large and small scales, and the value of the scale is displayed only on the large scale. Since xticks and yticks were operating the display of the large scale, This time, by manipulating the display of the small scale with set_minor_locator Realize "turn off the scale while leaving the grid"

import numpy as np
import matplotlib.pyplot as plt
import matplotlib.ticker as tick #Load the library required for scale operation

x = np.arange(0, 2 * 100 + 1)
f = sin(x)

plt.rc('font', family='serif')
plt.figure()
plt.ylim([-1.5, 1.5])
plt.plot(f, color='black')

#Small scale in 5 increments on x-axis(minor locator)display
plt.gca().xaxis.set_minor_locator(tick.MultipleLocator(5))
#0 on y axis.Small scale in 1 increments(minor locator)display
plt.gca().yaxis.set_minor_locator(tick.MultipleLocator(0.1))

#Grid display for small scales
plt.grid(which='minor')

plt.show()

例3.png In this way, the grid can be displayed in small steps, while the scale part can be refreshed.

Recommended Posts

How to turn off the scale value display while leaving the grid with matplotlib
How to display images continuously with matplotlib Note
Display the graph while changing the parameters with PySimpleGUI + Matplotlib
matplotlib log scale display and grid display
[Memo] How to use BeautifulSoup4 (2) Display the article headline with Requests
[Memo] How to use BeautifulSoup4 (3) Display the article headline with class_
How to title multiple figures with matplotlib
How to suppress display error in matplotlib
[Python] How to specify the window display position and size of matplotlib
Connect to VPN with your smartphone and turn off / on the server
(Mac) How to display Japanese with Matplotlib and Seaborn At MacOS Sierra
How to display python Japanese with lolipop
How to power off Linux with Ultra96-V2
Display markers above the border with matplotlib
Match the colorbar to the figure with matplotlib
How to display in the entire window when setting the background image with tkinter
[Python Tips] How to retrieve multiple keys with the maximum value from the dictionary
Mouse over Matplotlib to display the corresponding image
How to assign multiple values to the Matplotlib colorbar
How to specify the NIC to scan with amazon-dash
[Python] How to change the date format (display format)
I want to display multiple images with matplotlib.
[Python] How to draw multiple graphs with Matplotlib
How to try the friends-of-friends algorithm with pyfof
How to Learn Kaldi with the JUST Corpus
How to retrieve the nth largest value in Python
How to delete the specified string with the sed command! !! !!
[Python] How to draw a line graph with Matplotlib
Add information to the bottom of the figure with Matplotlib
[Introduction to Python] How to iterate with the range function?
How to create a submenu with the [Blender] plugin
[python] option to turn off the output of click.progressbar
[Python] How to specify the download location with youtube-dl
How to extract non-missing value nan data with pandas
[Python] How to rewrite the table style with python-pptx [python-pptx]
[Python] How to create a 2D histogram with Matplotlib
Display / update the graph according to the input with PySimpleGui
[Python] How to draw a scatter plot with Matplotlib
How to extract non-missing value nan data with pandas
How to get the current weather data and display it on the GUI while updating it automatically
How to plot a lot of legends by changing the color of the graph continuously with matplotlib
How to use Matplotlib
I tried to simulate how the infection spreads with Python
Memo to get the value on the html-javascript side with jupyter
How to return to the previous directory with the Bash cd command
[python] How to use the library Matplotlib for drawing graphs
[Python] Read the csv file and display the figure with matplotlib
[Ev3dev] How to display bmp image on LCD with python
How to manipulate the DOM in an iframe with Selenium
How to display a list of installable versions with pyenv
How to get the last (last) value in a list in Python
A story about how to deal with the CORS problem
How to get into the python development environment with Vagrant
[Introduction to Python] How to get data with the listdir function
How to display the CPU usage, pod name, and IP address of a pod created with Kubernetes