9.绘制不同颜色不同样式的线条
import matplotlib.pyplot as plt import numpy as np x np.linspace(0, 10, 100) plt.plot(x, x 0, --g ) plt.plot(x, x 1, -.r ) plt.plot(x, x 2, :b ) plt.plot(x, x 3, .k ) plt.plot(x, x 4, ,c ) plt.plot(x, x 5, *y ) plt.show()
10.给图添加图例
import matplotlib.pyplot as plt import numpy as np # label后使用legend()添加图例 x np.linspace(0, 10, 100) plt.rcParams[ font.sans-serif ] [ SimHei ] plt.plot(x, x 0, --g , label 绿色 ) plt.plot(x, x 1, -.r , label 红色 ) plt.plot(x, x 2, :b , label 蓝色 ) plt.plot(x, x 3, .k , label 黑色 ) plt.plot(x, x 4, ,c , label 青色 ) plt.plot(x, x 5, *y , label 黄色 ) # plt.legend() 默认位置在左上角,可以通过loc修改 plt.legend(loc lower right ) plt.show()
11.绘制柱状图
from matplotlib import pyplot as plt import numpy as np # 使用bar()绘制柱状图,barh()绘制水平柱状图 # x表示年份, y表示x对应年份的销量 plt.rcParams[ font.sans-serif ] [ SimHei ] x [1980, 1985, 1990, 1995] x_label [ 1980年 , 1985年 , 1990年 , 1995年 ] y [1000, 3000, 4000, 5000] # 给图添加标题 plt.title( 销量统计图 ) # 设置x轴坐标 plt.xticks(x, x_label) # xlabel()或ylabel()给x,y轴添加注释 plt.xlabel( 年份 , size 15) plt.ylabel( 销售额 , size 15) plt.bar(x, y, width 2.5) # width修改柱状宽度 plt.show()
12.bar()barh()函数使用
import matplotlib.pyplot as plt import numpy as np np.random.seed(0) x np.arange(5) y np.random.randint(-5, 5, 5) plt.subplot(1, 2, 1) # axhline()水平添加在0坐标,颜色黑,宽度为2的线 plt.axhline(0, color k , linewidth 2) plt.bar(x, y, color r ) plt.subplot(1, 2, 2) plt.axvline(0, color b , linewidth 2) plt.barh(x, y) plt.show()
13.不同颜色设置柱
import matplotlib.pyplot as plt import numpy as np np.random.seed(0) x np.arange(5) y np.random.randint(-5, 5, 5) v_bar plt.bar(x, y, color b ) # 对y值大于0设置为蓝色, 对y小于零的柱设置为绿色 for bar, height in zip(v_bar, y): if height 0: bar.set(color g ) if height 0: bar.set(color purple ) plt.show()
14.柱状图的使用,五部电影五日的票房数
import matplotlib.pyplot as plt
import numpy as np
plt.rcParams[ font.sans-serif ] [ SimHei ]
plt.rcParams[ axes.unicode_minus ] False
movie_names [ 千与千寻 , 玩具总动员 , 黑衣人:全球追击 , 蜘蛛侠:英雄归来 , 冰雪奇缘 ]
day_1 [7548, 4013, 1673, 4312, 690]
day_2 [5453, 1840, 1080, 3222, 1467]
day_3 [4348, 2345, 1890, 2231, 4321]
day_4 [5321, 2221, 2342, 3612, 2411]
day_5 [4221, 4311, 2311, 4122, 1234]
total [day_1, day_2, day_3, day_4, day_5]
x np.arange(len(movie_names))
plt.title( 五日电影票房数字 , size 15)
plt.xlabel( 时间/天 )
plt.ylabel( 销售票房数 )
plt.text(1.0, 6000, 电影票房 , size 30, alpha 0.5, color c )
x_width 0.15
for j in range(0, 5):
plt.bar([i j * x_width for i in x], total[j], alpha 0.8, width x_width, label movie_names[j])
x_label [ 第{}天 .format(i 1) for i in x]
plt.xticks([i 0.285 for i in x], x_label)
plt.legend()
plt.show()
# plt.bar(x, day_1, alpha 0.5, width x_width, label movie_names[0])
# plt.bar([i x_width for i in x], day_2, alpha 0.5, width x_width, label movie_names[1])
# plt.bar([i 2*x_width for i in x], day_3, alpha 0.5, width x_width, label movie_names[2])
# plt.bar([i 3*x_width for i in x], day_4, alpha 0.5, width x_width, label movie_names[3])
# plt.bar([i 4*x_width for i in x], day_5, alpha 0.5, width x_width, label movie_names[4])
15.绘制饼状图
import matplotlib.pyplot as plt import numpy as np # 展示我国男女比例 plt.rcParams[ font.sans-serif ] [ SimHei ] man 71351 woman 68187 man_perc man / (man woman) woman_perc woman / (man woman) colors [ blue , r ] labels [ man , woman ] # labels名称, color颜色, explode分裂, autopct显示百分比 paches, texts, autotexts plt.pie([man_perc, woman_perc], labels labels, colors colors, explode (0, 0.05), autopct %0.1f%% ) # 设置字体颜色 for text in autotexts: text.set_color( white ) # 设置字体大小 for text in texts autotexts: text.set_fontsize(20) plt.show()
16.绘制直方图
import matplotlib.pyplot as plt import numpy as np # x np.random.randn(1000)#生成一千个正态分布数 x np.random.normal(0, 0.8, 1000) # 指定期望和均值的正态分布 y np.random.normal(-2, 1, 1000) z np.random.normal(3, 2, 1000) # plt.hist(x) # 修改柱的宽度bins kwargs dict(bins 100, alpha 0.75) plt.hist(x, **kwargs) # 10柱在一起 plt.hist(y, **kwargs) plt.hist(z, **kwargs) plt.show()
17.绘制等高线图和三维图
①等高线图
import matplotlib.pyplot as plt import numpy as np x np.linspace(-10, 10, 100) y np.linspace(-10, 10, 100) X,Y np.meshgrid(x, y) Z np.sqrt(X**2 Y**2) plt.contour(X, Y,Z) # contourf()可填充颜色, contour()不填充颜色 plt.show()
②三维图
import matplotlib.pyplot as plt from mpl_toolkits.mplot3d import Axes3D X [1, 1, 2, 2] Y [3, 4, 4, 3] Z [1, 100, 1, 1] figure plt.figure() ax Axes3D(figure) ax.plot_trisurf(X, Y, Z) plt.show()


![[Python] 使用Matplotlib库画图 [Python] 使用Matplotlib库画图](http://www.mshxw.com/aiimages/31/267826.png)
