Matplotlib detailed tutorial [1] Library installation, coordinate axis settings, legend settings

Table of contents

Preface

Introduction to Matplotlib

Matplotlib installation and import

Install

import

Axis settings

Set titles for X and Y axis

Show Chinese

x, y axis setting accuracy and range

Legend settings

Epilogue


Preface

Overview of this article: Many Matplotlib tutorials on CSDN are not very well written. Many of them directly paste a bunch of code, and the comments are unclear and incomplete, which makes many readers who want to learn Matplotlib have a very poor experience, so the author intends to Write a series of Matplotlib tutorials to help newbies get started, and veterans can also check for gaps and fill in the gaps.

Author's introduction: The author is an artificial intelligence alchemist. Currently, the main research direction in the laboratory is generative models. He also has some knowledge of other directions. He hopes to communicate with friends who are also interested in artificial intelligence on the CSDN platform. Share and make progress together. Thank you everyone~~~

 如果你觉得这篇文章对您有帮助,麻烦点赞、收藏或者评论一下,这是对作者工作的肯定和鼓励。  

Introduction to Matplotlib

Matplotlib is one of the most commonly used visualization tools in Python. It can easily create 2D charts and 3D charts. Users can define their own x and y axes and draw graphics (line charts, histograms, histograms, density charts, scatter charts, etc. etc.), which can meet the needs of most data visualization in machine learning and deep learning.

Matplotlib installation and import

Install

Use the following command to install the library in the conda virtual environment. Friends who don’t know what anaconda is can read my previous article.

pip install matplotlib -i https://pypi.tuna.tsinghua.edu.cn/simple

import

All our image drawing only needs to use the pyplot method in the matplotlib module.

from matplotlib import pyplot as plt 

Axis settings

Set titles for X and Y axis

plt.plot([1,2,3,4],[2,2,3,4],':',color='r') # '.'为风格控制
plt.xlabel("this is Xlabel",fontsize=16)
plt.ylabel("this is Ylabel",fontsize=16)
plt.show()

The string in front is its title name, and the fontsize in the back is the font size of its title.

Show Chinese

Matplotlib cannot directly output Chinese titles. We need to go through some settings.

import matplotlib.pyplot as plt
plt.rcParams['font.sans-serif'] = ['SimHei'] #设置中文显示
plt.plot([1,2,3,4],[2,2,3,4],':',color='r')] # '.'为风格控制
plt.xlabel("这是X轴标题",fontsize=16)
plt.ylabel("这是Y轴标题",fontsize=16)
plt.show()

x, y axis setting accuracy and range

import matplotlib.pyplot as plt
import numpy as np
from pylab import *
mpl.rcParams['font.sans-serif'] = ['SimHei']
plt.plot([1,2,3,4],[2,2,3,4],':',color='r') # '.'为风格控制
plt.xlabel("this is Xlabel",fontsize=16)
plt.ylabel("this is Ylabel",fontsize=16)
my_x_ticks = np.arange(0, 5, 0.5)
my_y_ticks = np.arange(2, 5, 0.2)
plt.xticks(my_x_ticks)
plt.yticks(my_y_ticks)
plt.show()

Legend settings

import matplotlib.pyplot as plt
import numpy as np
from pylab import *
mpl.rcParams['font.sans-serif'] = ['SimHei']
l1,=plt.plot([1,2,3,4],[2,2,3,4],linestyle='--',color='r') # 注意需要在l1后面添加逗号
l2,=plt.plot([3,3,3,3],[1,2,3,4],linestyle=':',color='b') # 注意需要在l2后面添加逗号
plt.legend(handles=[l1,l2],labels=['折线','直线'],loc='upper left')
plt.show()

Epilogue

 如果您觉得这篇文章对您有帮忙,请点赞、收藏。您的点赞是对作者工作的肯定和鼓励,这对作者来说真的非常重要。如果您对文章内容有任何疑惑和建议,欢迎在评论区里面进行评论,我将第一时间进行回复。 

Guess you like

Origin blog.csdn.net/qq_35768355/article/details/132646852