Home >Backend Development >Python Tutorial >Introduction to the usage of matplotlib library in Python

Introduction to the usage of matplotlib library in Python

不言
不言forward
2018-10-24 17:11:354280browse

This article brings you an introduction to the usage of the matplotlib library in Python. It has certain reference value. Friends in need can refer to it. I hope it will be helpful to you.

Introduction

The matplotlib library is a 2D drawing library in Python.

Installation

$ pip install matplotlib

Getting started

Drawing a line chart

pyplot function to draw a line chart

Here is a line chart of months and temperature :

import matplotlib.pyplot as plt

x_month = [3,4,5,6,7,8,9]
y_temperature = [18,24,27,30,35,37,31]
plt.plot(x_month,y_temperature)
plt.show()
# plt.savefig('xxx.png') # 保存图标函数

Import the pyplot module here and name it plt.
show() is used to display pictures. savefig() is used to save pictures.

Set x,y axis

import matplotlib.pyplot as plt

x_month = [1,2,3,4,5,6,7,8]
y_temperature = [11,22,23,43,20,23,42,42]
plt.plot(x_month,y_temperature,linewidth=5) # linewidth定义折线宽度
plt.title("This is Title")  # 标题
plt.xlabel("month",fontsize=14) # x轴
plt.ylabel("temperature",fontsize=14) # y轴
plt.tick_params(axis='both',labelsize=14) # 影响xy刻度的函数
plt.show() # 展示
# plt.savefig('xxx.png') # 保存图标函数

fontsize is used to set the font size of each axis.

Draw a single point graph

scatter function draws a single point graph

import matplotlib.pyplot as plt

plt.scatter(1,8,s=50,c='red') 
plt.show()

Parameterss sets the size of the points used when drawing graphics. cThe color of the parameter setting point can also be specified through RDB: c=(0,0,0.8)

Draw a series of points

import matplotlib.pyplot as plt
x_values = list(range(1001))
y_values = [x**2 for x in x_values]
plt.scatter(x_values,y_values,s=40,edgecolors='none') # edgecolors参数用于去除外轮廓
plt.show()

edgecolors can specify white or black, etc.

Draw the color map

import matplotlib.pyplot as plt


x_values = list(range(1001))
y_values = [x**2 for x in x_values]
plt.scatter(x_values,y_values,s=40,edgecolors='none',c=y_values,cmap=plt.cm.Blues)

Pass the y_values ​​parameter into c, and use cmap to tell the function which color to use for mapping.

The above is the detailed content of Introduction to the usage of matplotlib library in Python. For more information, please follow other related articles on the PHP Chinese website!

Statement:
This article is reproduced at:segmentfault.com. If there is any infringement, please contact admin@php.cn delete