Python数据分析:折线图和散点图的绘制

[复制链接]
6 y: p5 |4 V5 f! p B( x( t$ N

原创:宋宋 Python专栏

8 x$ g* x$ C G" y" \

来源:Python数据分析:折线图和散点图的绘制

' w; z5 ~6 O0 X& ]7 T. C: Y

折线图

, F5 e3 d$ q% g

折线图用于分析自变量和因变量之间的趋势关系,最适合用于显示随着时间而变化的连续数据,同时还可以看出数量的差异,增长情况。

) ~. V& E* A8 @% D& S

Matplotlib 中绘制散点图的函数为 plot() ,使用语法如下:

matplotlib.pyplot.plot(*args, scalex=True, scaley=True, data=None, **kwargs)

1) 简单的折线图

d% X6 F: o/ @( V; |0 d

在matplotlib面向对象的绘图库中,pyplot是一个方便的接口。

/ g6 R8 \: `0 M8 ]; b

plot()函数:支持创建单条折线的折线图,也支持创建包含多条折线的复式折线图----只要在调用plot()时传入多个分别代表X轴和Y轴数据的list列表即可。

( [ h! D' ?; T7 k
import matplotlib.pyplot as plt / p5 C A* v7 l7 r; O, ?: N3 a- m3 y. N1 j x_data = [2011,2012,2013,2014,2015,2016,2017] % t g9 g3 V3 Z. O E; I C" r _- t, { y_data = [58000,60200,63000,71000,84000,90500,107000]' |, ~; ` B# R ! E1 i, t4 R' t" Y! v8 q2 I" K- M plt.plot(x_data,y_data) $ E' T; t7 j" o' S" c5 \ plt.show()# {- d6 ~, X! H
$ X* |+ J( T/ \1 f% x
9 K! f- l+ u6 f' Z3 b

2)复式折线图:

- o4 f! ?, d6 ~$ l2 |" d/ h$ J
import matplotlib.pyplot as plt * L- @, W- d9 G/ W* Y/ w x_data = [2011,2012,2013,2014,2015,2016,2017] / F+ S& X+ {& Y( N y_data = [58000,60200,63000,71000,84000,90500,107000] 0 z1 j4 M7 V5 m% F$ { y_data2 = [52000,54200,51500,58300,56800,59500,62700] 9 A9 o$ K' \# K1 i W* s0 p$ f+ Z1 G# t$ e7 A+ E plt.plot(x_data,y_data,color=red,linewidth=2.0,linestyle=--)1 [3 Z: s& B0 |% f plt.plot(x_data,y_data2,color=blue,linewidth=3.0,linestyle=-.) 4 e: a; D, w6 N9 b+ A6 j U plt.show()" M) `- l5 [1 s4 ?6 `4 j
0 M2 g, B7 f& X( `0 d; G) ^
) x) G% u& x+ L8 o5 b

注:说明:参数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 是指标记点,有如下的:

/ H4 C% Y) d2 q% [0 k# [
" M: k% Z+ P- ]2 I# L

3) 管理图例 对于复式折线图,应该为每条折线添加图例,可以通过legend()函数来实现。该函数可传入两个list参数,其中第一个list参数(handles参数)用于引用折线图上的每条折线;第二个list参数(labels)代表为每条折线所添加的图例

3 j/ Q) p5 w& k
import pandas as pd& [& ?, a! L% u" B8 B% ~ import matplotlib.pyplot as plt 2 i9 t% b. M2 ?( c$ j% t# q D* g Y& J# a" L #读取数据 / Y* X, [* _ [ K9 |& V data = pd.read_excel(matplotlib.xlsx) 4 Z& k0 O7 p, ? + M) x2 h3 J3 F: \% v plt.figure(figsize=(10,5))#设置画布的尺寸0 O, V* i4 Z4 t5 R plt.title(Examples of line chart,fontsize=20)#标题,并设定字号大小 . }) R: _6 U* `5 ~ plt.xlabel(ux-year,fontsize=14)#设置x轴,并设定字号大小! X9 _: F8 a: ]# } plt.ylabel(uy-income,fontsize=14)#设置y轴,并设定字号大小0 C2 V. d7 s( `/ l ( V/ I" k8 [# l0 ~5 F3 ^5 \ #color:颜色,linewidth:线宽,linestyle:线条类型,label:图例,marker:数据点的类型 3 L* U! k, E' `1 L5 k* p5 A in1, = plt.plot(data[时间],data[收入_Jay],color="deeppink",linewidth=2,linestyle=:, marker=o) $ O; f3 c: W+ X, R. F( l" m in2, = plt.plot(data[时间],data[收入_JJ],color="darkblue",linewidth=1,linestyle=--, marker=+) q" N4 P. f# s2 g* `( K5 {! b$ m in3, = plt.plot(data[时间],data[收入_Jolin],color="goldenrod",linewidth=1.5,linestyle=-, marker=*). \+ e! v9 B3 O. q5 a4 `" V0 h0 X " S% z. |; d- s; k plt.legend(handles = [in1,in2,in3],labels=[Jay income,JJ income,Jolon income],loc=2)#图例展示位置,数字代表第几象限& Z o! V. E# }0 ]9 ]3 I' @% X plt.show()#显示图像 3 s3 n d B9 d" {: Q9 k% O
/ ]; {9 k, Z+ R9 V
9 F7 ?) f. k* v* b8 Q% T3 k. q" L

4) 管理多个子图

& E: Y$ C" p4 {' V
import matplotlib.pyplot as plt& |" [5 S9 l8 T! ~* d import numpy as np$ M& [7 t4 y" L; n( h4 @ import matplotlib.gridspec as gridspec" k& I/ m a: j; c- e+ X* U, v import matplotlib.font_manager as fm #字体管理器9 M! K9 ] t$ @: L+ g9 b ( ~, c* b0 K* n, ` j1 y my_font = fm.FontProperties(fname="/System/Library/Fonts/PingFang.ttc")1 k& l+ e; W8 \4 l 4 u7 X0 |# m. i) _$ m plt.figure()1 g: u% W& e$ G+ v8 I6 H 8 C$ G+ H$ Y( w. s5 n4 _8 n. V x_data = np.linspace(-np.pi,np.pi,64,endpoint=True) + x* v$ {& J6 [( v3 x5 o gs = gridspec.GridSpec(2,3) #将绘图区分成两行三列 " |2 M& a; q( } a ax1 = plt.subplot(gs[0,:])#指定ax1占用第一行(0)整行 ) Z+ }, a/ T2 L$ P0 G) g2 d) I ax2 = plt.subplot(gs[1,0])#指定ax2占用第二行(1)的第一格(第二个参数为0)+ K3 w0 `% c/ [ N& o _/ P/ b ax3 = plt.subplot(gs[1,1:3])#指定ax3占用第二行(1)的第二、三格(第二个参数为1:3)* h p- }* C h* x4 g7 s ; f0 p' ?1 Q( ~, v8 k `; P #绘制正弦曲线 . y4 B; M& c1 r4 I U. D ax1.plot(x_data,np.sin(x_data)) " a( } G& E. h, c) p! Q ax1.spines[right].set_color(none) ' S6 Z& z4 J# V. o% D3 ` ax1.spines[top].set_color(none) & u( ]2 F$ c2 h ax1.spines[bottom].set_position((data,0))$ n. J+ T' C0 s" i; C7 f0 t2 ?) L ax1.spines[left].set_position((data,0))' g* J$ t( D0 r# ]( m ax1.set_title(正弦曲线,fontproperties=my_font)# T' E% W) u# [) Q) d# G 4 {7 u7 i5 [ ~ #绘制余弦曲线 " {$ D& ~7 n1 m6 x/ m$ V ax2.plot(x_data,np.cos(x_data))6 a/ H7 }: O; z! j, G6 V& f ax2.spines[right].set_color(none) . z/ P: C# ]$ ]7 j8 { ax2.spines[top].set_color(none)5 u) a* L. o9 F# Z6 R0 D) V. v5 { ax2.spines[bottom].set_position((data,0)) 1 E6 U- M; E" C+ } v% z ax2.spines[left].set_position((data,0)) : e2 r, q( E: { T% ~0 A1 W3 L ax2.set_title(余弦曲线,fontproperties=my_font)- w# n f, m7 E& h6 ? 2 s: ~, m) l' E/ @8 D #绘制正切曲线5 ]! n! O( u3 ] ax3.plot(x_data,np.tan(x_data))6 @ S5 r R& J0 ~6 ?+ p. }) v ax3.spines[right].set_color(none) : x7 C. `6 V; Z% k7 ]9 V ax3.spines[top].set_color(none) . b' O! i+ @. `$ H" k1 Y; ~0 k ax3.spines[bottom].set_position((data,0)) ) {/ y. x7 \- q ax3.spines[left].set_position((data,0)): M0 }9 W9 x8 d& [ ax3.set_title(正切曲线,fontproperties=my_font)3 ~% H9 w' H- O- E1 e0 S; k, w% f plt.show()8 r4 d/ @1 N6 k$ \6 f/ W% Z# u. B
- W: }! o% _* K- J2 Q

结果:

( q. v3 F ~ ^" X; G
5 D* E' i! I$ u# e6 W
3 R$ u- r, }* h 2 Y) k! D* y; o3 P& S# o6 w; \/ l& x/ |( x. ]6 A- C/ A 2 q. P, M% ^9 Z4 z. l( R4 x0 X: D ! w" p7 n. B- V6 B0 M
回复

举报 使用道具

相关帖子

全部回帖
暂无回帖,快来参与回复吧
懒得打字?点击右侧快捷回复 【吾爱海洋论坛发文有奖】
您需要登录后才可以回帖 登录 | 立即注册
伪丶笑
活跃在2025-1-25
快速回复 返回顶部 返回列表