|
% t, m. B. ?6 r! U! \
原创:宋宋 Python专栏 $ E. W7 |6 I9 ~3 m5 J- G; ^% ]
来源:Python数据分析:折线图和散点图的绘制 * Z" N. D- n0 R- r8 ~
折线图
0 C' d( g O5 x 折线图用于分析自变量和因变量之间的趋势关系,最适合用于显示随着时间而变化的连续数据,同时还可以看出数量的差异,增长情况。
( @2 L# l; R: d( _, K% u Matplotlib 中绘制散点图的函数为 plot() ,使用语法如下: matplotlib.pyplot.plot(*args, scalex=True, scaley=True, data=None, **kwargs)1) 简单的折线图 $ ~+ X/ l4 k4 \
在matplotlib面向对象的绘图库中,pyplot是一个方便的接口。
# S7 l1 B! K' v5 G* b plot()函数:支持创建单条折线的折线图,也支持创建包含多条折线的复式折线图----只要在调用plot()时传入多个分别代表X轴和Y轴数据的list列表即可。
! I% ?, Y( n! u, R- Y3 W |2 e import matplotlib.pyplot as plt; |$ O$ n9 A! s( H' K0 I, H3 ^% c
9 D9 F7 c9 J7 n( Q z1 X/ _% J
x_data = [2011,2012,2013,2014,2015,2016,2017]3 Q' h! G- Z: z1 k. Z) ~
y_data = [58000,60200,63000,71000,84000,90500,107000]
4 s# O" ?% v( K: k, i* `2 i4 l; Z% Y
plt.plot(x_data,y_data)
; ]# {6 z+ K! Z! b plt.show()
. W K9 W P8 ]2 _ ( {; [% D/ v2 _. y9 W$ r( \# y* A2 [
: s2 R/ m: b, }/ T' [0 Y3 y' Q
2)复式折线图:
7 E( p Z8 W* _+ U: T4 Q import matplotlib.pyplot as plt3 Y# X, E. L! s
x_data = [2011,2012,2013,2014,2015,2016,2017]
) K* a5 k0 V2 U8 C y_data = [58000,60200,63000,71000,84000,90500,107000]# z0 P1 t: h! y, ^. X3 X# N% b
y_data2 = [52000,54200,51500,58300,56800,59500,62700]
6 z$ h6 L" r0 g- z
7 M4 c* Z# k3 K. H" j5 n plt.plot(x_data,y_data,color=red,linewidth=2.0,linestyle=--)+ @! |1 M, `4 ?# w6 Y- m
plt.plot(x_data,y_data2,color=blue,linewidth=3.0,linestyle=-.), H9 Y1 t' V& ?3 ^: [3 s3 k
plt.show()
5 [: w, {* Z1 i: _7 T: u3 f: k
% d o% G$ ~( j 4 ~4 v, o" `. B* Y! j
注:说明:参数color=’ red ‘,可以换成color=’ #054E9F’,每两个十六进制数分别代表R、G、B分量,除了使用red、blue、green等还可以参照下图小白参数linestyle可以选择使用下面的样式: - solid line style 表示实线-- dashed line style 表示虚线-. dash-dot line style 表示短线、点相间的虚线: dotted line style 表示点线参数 linewidth 可以指定折线的宽度参数 marker 是指标记点,有如下的: 7 X5 V2 q4 F* |: U
9 Z3 ]2 g) T9 r 3) 管理图例 对于复式折线图,应该为每条折线添加图例,可以通过legend()函数来实现。该函数可传入两个list参数,其中第一个list参数(handles参数)用于引用折线图上的每条折线;第二个list参数(labels)代表为每条折线所添加的图例
3 C: O4 U5 l2 K0 a8 H3 m import pandas as pd
# |: K/ ]6 B8 V3 _; `/ A! j import matplotlib.pyplot as plt
! g' L" r3 [) L7 z/ ]
5 ]4 B5 N+ z- _: | #读取数据7 i8 Y& U& N6 C0 s& `0 S9 M1 n
data = pd.read_excel(matplotlib.xlsx)
) y: P0 d) |5 V" a- w; F* Q- [7 V; f% d8 @: {6 k/ t* O# M
plt.figure(figsize=(10,5))#设置画布的尺寸) i- n7 k: l- T. n2 f9 N% ~. V$ r0 R
plt.title(Examples of line chart,fontsize=20)#标题,并设定字号大小& S x' K. a; A% ]+ J
plt.xlabel(ux-year,fontsize=14)#设置x轴,并设定字号大小% l+ h- U% `7 y* w' k- i
plt.ylabel(uy-income,fontsize=14)#设置y轴,并设定字号大小
! g( l* T5 H+ L, {( D6 \* }( {
5 M5 i, K- J3 _% y \4 N+ g0 E5 a* O #color:颜色,linewidth:线宽,linestyle:线条类型,label:图例,marker:数据点的类型
- x; ?8 d/ k b; H( ~) Y" f in1, = plt.plot(data[时间],data[收入_Jay],color="deeppink",linewidth=2,linestyle=:, marker=o)
6 n- h+ d3 ^8 H6 A/ G8 j in2, = plt.plot(data[时间],data[收入_JJ],color="darkblue",linewidth=1,linestyle=--, marker=+)
4 N# b6 E' o3 ^3 O' f* l in3, = plt.plot(data[时间],data[收入_Jolin],color="goldenrod",linewidth=1.5,linestyle=-, marker=*)) Y: n0 q' v0 I H1 f2 r5 w3 Z& g4 n
, E) V* f. `/ u' v* h plt.legend(handles = [in1,in2,in3],labels=[Jay income,JJ income,Jolon income],loc=2)#图例展示位置,数字代表第几象限
# v8 S5 X& w9 b, x. q. I7 A plt.show()#显示图像
. v! E" @: E" H
( S H: U3 O) B, C V , e$ C( [; V9 s z! v! {
4) 管理多个子图
r$ N# e* } S* g/ y Z! o import matplotlib.pyplot as plt
/ O. d0 e* P) Z: e2 z T; y import numpy as np
8 {) [0 p! K9 E' x f5 ? import matplotlib.gridspec as gridspec- [2 S- b# g( u3 s
import matplotlib.font_manager as fm #字体管理器5 T6 `' |% {5 e0 c* g
1 E% u' J8 i- R/ h7 Q% B my_font = fm.FontProperties(fname="/System/Library/Fonts/PingFang.ttc")9 L: B; v2 h3 q
1 U' g9 i7 F$ }% K7 X& S plt.figure()
$ [8 P5 Y' \- g; E, H- y
2 F5 k' c7 `1 F3 p% x x_data = np.linspace(-np.pi,np.pi,64,endpoint=True)
2 _; ]+ z4 u+ x4 `2 ^( V gs = gridspec.GridSpec(2,3) #将绘图区分成两行三列
- b, _- l, J9 s. X7 P ax1 = plt.subplot(gs[0,:])#指定ax1占用第一行(0)整行4 z7 W# ^2 j: Z# L: c
ax2 = plt.subplot(gs[1,0])#指定ax2占用第二行(1)的第一格(第二个参数为0)7 E' r0 K! P1 H( N u1 V6 N
ax3 = plt.subplot(gs[1,1:3])#指定ax3占用第二行(1)的第二、三格(第二个参数为1:3)8 _! q7 q, f9 S1 B. t" t
. [. X I) R9 `* u8 j& O
#绘制正弦曲线! H+ U: J! ]$ x" T# B* A X8 T0 G3 w$ ^
ax1.plot(x_data,np.sin(x_data))
x0 G4 X+ T/ D# ^ ax1.spines[right].set_color(none)
T _7 ]& s; q& j3 d6 X ax1.spines[top].set_color(none)) R4 ~& L d: }; Y* o
ax1.spines[bottom].set_position((data,0)). t/ a0 G$ c: R4 x
ax1.spines[left].set_position((data,0))
3 F( Y- C# k! ]) W5 N2 b ax1.set_title(正弦曲线,fontproperties=my_font)
g& V1 R/ d' I. E6 K9 k" ?; W: C( Z/ K* K
#绘制余弦曲线" V8 U6 ?2 |% S# O# D
ax2.plot(x_data,np.cos(x_data))% G* Z; f/ b- y0 u# e
ax2.spines[right].set_color(none)- Z. I) u5 t& b0 t
ax2.spines[top].set_color(none)
s. {/ H2 ^1 r6 }9 @ ax2.spines[bottom].set_position((data,0))
; r; T! C7 F+ Q2 e9 ]$ w ax2.spines[left].set_position((data,0))6 i/ L2 T2 m% g7 e: Z4 d
ax2.set_title(余弦曲线,fontproperties=my_font)* g& V/ v' t6 q3 [/ u, ?: d
+ A+ m1 u# Y, d& |6 c9 e
#绘制正切曲线
. {& y! j0 z- t9 i7 A* \0 T ax3.plot(x_data,np.tan(x_data))- N# [$ A/ S) X c+ q
ax3.spines[right].set_color(none)
; Y+ z* ~3 |; e1 @ r& W ax3.spines[top].set_color(none)3 E6 G& ^0 }* S3 R. d% s
ax3.spines[bottom].set_position((data,0)). H. D+ H' e& E5 t
ax3.spines[left].set_position((data,0))
6 |* h- ]# U. t; S2 F4 g ax3.set_title(正切曲线,fontproperties=my_font)
$ F2 T5 r7 q% \' f& _! A plt.show()
: H! W# ~3 `% z* _1 s# B+ r2 n % ~" }- a7 ~; ?; b3 _4 f2 Z8 m
结果:
$ {* e" y8 ^4 R5 }+ a
' f/ G. d# V8 ~ : `2 X; R C. | n. Z; m, P* j
& K6 p! y2 y/ c3 O8 }: u2 r$ H# _+ F4 R: S, _, V
6 r+ \% x6 a- I
1 M2 i) h* F2 ?0 X- V7 W |