使用matplotlib.pyplot进行图表操作,数据分析

# 绘制1000个点的代码:

import matplotlib.pyplot as plt


x_values = range(1, 1001)
y_values = [x**2 for x in x_values]  # y解析,遍历x并进行计算,解析出y值

plt.style.use('seaborn')
# 函数subplots()可以绘制一张或多张图表,fig表示整张图片,ax表示图片中的各个图表
fig, ax = plt.subplots()
# 调用scatter(),将x、y值传递给scatter(),并使用参数s设置绘制图形式使用的点的尺寸,
# ax.scatter(x_values, y_values, s=10)
# ax.scatter(x_values, y_values, c='red', s=10)  # 指定颜色
# ax.scatter(x_values, y_values, c=(0, 0.8, 0), s=10)  # 指定RGB颜色,红绿蓝,0深,1浅
ax.scatter(x_values, y_values, c=y_values, cmap=plt.cm.Blues, s=10)  # 根据y值指定颜色,值越大,指定颜色越深


# 设置图表标题,并给坐标轴加上标签
ax.axis([0, 1100, 0, 1100000])

plt.show()
# 如果需要设置把图表自动保存到文件中,可以将plt.show()换成下面:
# 第一个实参'squares_plot.png'表示文件名,默认保存到当前py文件的同级路径下
# 第二个实参bbox_inches='tight'剪掉周围空白区域,可省略
# plt.savefig('squares_plot.png', bbox_inches='tight')

其中用到了多种显示颜色的属性,要了解pyplot中所有的颜色映射,可以访问Matplotib网站主页,单击Examples,向下滚动到Color,再单击Colormaps reference。

你可能感兴趣的:(数据分析,python)