首页 > 编程 > Python > 正文

Python数据可视化之画图

2019-11-25 13:28:54
字体:
来源:转载
供稿:网友

安装数据可视化模块matplotlib:pip install matplotlib

导入matplotlib模块下的pyplot

1 折线图

from matplotlib import pyplot#横坐标year=[2010,2012,2014,2016]#纵坐标perple=[20,40,60,100]#生成折线图:函数poltpyplot.plot(year,perple)#设置横坐标说明pyplot.xlabel('year')#设置纵坐标说明pyplot.ylabel('population')#添加标题pyplot.title('Population year correspondence')#设置纵坐标刻度pyplot.yticks([0, 25, 50, 75, 90])# 显示网格pyplot.grid(True)显示图表pyplot.show()

2 散点图

用两种方法

第一种:只需将函数polt换成scatter即可.

from matplotlib import pyplot#横坐标year=[2010,2012,2014,2016]#纵坐标perple=[20,40,60,100]#生成散点图:函数scatterpyplot.scatter(year,perple)#设置横坐标说明pyplot.xlabel('year')#设置纵坐标说明pyplot.ylabel('population')#添加标题pyplot.title('Population year correspondence')#设置纵坐标刻度pyplot.yticks([0, 25, 50, 75, 90])# 显示网格pyplot.grid(True)显示图表pyplot.show()

第二种方法:在polt函数里添加第三个参数 “o”.

可以更改点的颜色和类型,如红色,五角型:把plot第三个参数改为'rp'.

#点的颜色

  • ccyan青色
  • rred红色
  • mmagente品红
  • ggreen绿色
  • bblue蓝色
  • yyellow黄色
  • kblack黑色
  • wwhite白色

#线的类型

  •  虚线
  • -. 形式即为-.
  • : 细小的虚线

#点的类型

  • s方形
  • h六角形
  • H六角形
  • **形
  • ±-加号
  • xx形
  • d菱形
  • D菱形
  • p五角形
from matplotlib import pyplot#横坐标year=[2010,2012,2014,2016]#纵坐标perple=[20,40,60,100]#生成散点图:函数poltpyplot.plot(year,perple,'rp')#设置横坐标说明pyplot.xlabel('year')#设置纵坐标说明pyplot.ylabel('population')#添加标题pyplot.title('Population year correspondence')#设置纵坐标刻度pyplot.yticks([0, 25, 50, 75, 90])# 显示网格pyplot.grid(True)显示图表pyplot.show()

总结

以上就是这篇文章的全部内容了,希望本文的内容对大家的学习或者工作具有一定的参考学习价值,谢谢大家对武林网的支持。如果你想了解更多相关内容请查看下面相关链接

发表评论 共有条评论
用户名: 密码:
验证码: 匿名发表