当前位置:主页 > 软件编程 > Python代码 >

Python数据可视化之画图

时间:2021-09-16 09:54:06 | 栏目:Python代码 | 点击:

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

导入matplotlib模块下的pyplot

1 折线图

from matplotlib import pyplot
#横坐标
year=[2010,2012,2014,2016]
#纵坐标
perple=[20,40,60,100]
#生成折线图:函数polt
pyplot.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]
#生成散点图:函数scatter
pyplot.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'.

#点的颜色

#线的类型

#点的类型

from matplotlib import pyplot
#横坐标
year=[2010,2012,2014,2016]
#纵坐标
perple=[20,40,60,100]
#生成散点图:函数polt
pyplot.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()

总结

您可能感兴趣的文章:

相关文章