python绘制散点图,非常全,非常详细(已验证)
少废话,直接上代码
import matplotlib.pyplot as plt
import numpy as np
# 1. 首先是导入包,创建数据
n = 10
x = np.random.rand(n) * 2# 随机产生10个0~2之间的x坐标
y = np.random.rand(n) * 2# 随机产生10个0~2之间的y坐标
# 2.创建一张figure
fig = plt.figure(1)
# 3. 设置颜色 color 值【可选参数,即可填可不填】,方式有几种
# colors = np.random.rand(n) # 随机产生10个0~1之间的颜色值,或者
colors = ['r', 'g', 'y', 'b', 'r', 'c', 'g', 'b', 'k', 'm'] # 可设置随机数取
# 4. 设置点的面积大小 area 值 【可选参数】
area = 20*np.arange(1, n+1)
# 5. 设置点的边界线宽度 【可选参数】
widths = np.arange(n)# 0-9的数字
# 6. 正式绘制散点图:scatter
plt.scatter(x, y, s=area, c=colors, linewidths=widths, alpha=0.5, marker='o')
# 7. 设置轴标签:xlabel、ylabel
#设置X轴标签
plt.xlabel('X坐标')
#设置Y轴标签
plt.ylabel('Y坐标')
# 8. 设置图标题:title
plt.title('test绘图函数')
# 9. 设置轴的上下限显示值:xlim、ylim
# 设置横轴的上下限值
plt.xlim(-0.5, 2.5)
# 设置纵轴的上下限值
plt.ylim(-0.5, 2.5)
# 10. 设置轴的刻度值:xticks、yticks
# 设置横轴精准刻度
plt.xticks(np.arange(np.min(x)-0.2, np.max(x)+0.2, step=0.3))
# 设置纵轴精准刻度
plt.yticks(np.arange(np.min(y)-0.2, np.max(y)+0.2, step=0.3))
# 也可按照xlim和ylim来设置
# 设置横轴精准刻度
plt.xticks(np.arange(-0.5, 2.5, step=0.5))
# 设置纵轴精准刻度
plt.yticks(np.arange(-0.5, 2.5, step=0.5))# 11. 在图中某些点上(位置)显示标签:annotate
# plt.annotate("(" + str(round(x[2], 2)) + ", " + str(round(y[2], 2)) + ")", xy=(x[2], y[2]), fontsize=10, xycoords='data')# 或者
plt.annotate("({0},{1})".format(round(x[2],2), round(y[2],2)), xy=(x[2], y[2]), fontsize=10, xycoords='data')
# xycoords='data' 以data值为基准
# 设置字体大小为 10
# 12. 在图中某些位置显示文本:text
plt.text(round(x[6],2), round(y[6],2), "good point", fontdict={'size': 10, 'color': 'red'}) # fontdict设置文本字体
# Add text to the axes.
# 13. 设置显示中文
plt.rcParams['font.sans-serif']=['SimHei'] #用来正常显示中文标签
plt.rcParams['axes.unicode_minus']=False #用来正常显示负号
# 14. 设置legend,【注意,'绘图测试’:一定要是可迭代格式,例如元组或者列表,要不然只会显示第一个字符,也就是legend会显示不全】
plt.legend(['绘图测试'], loc=2, fontsize=10)
# plt.legend(['绘图测试'], loc='upper left', markerscale = 0.5, fontsize = 10) #这个也可
# markerscale:The relative size of legend markers compared with the originally drawn ones.
# 15. 保存图片 savefig
plt.savefig('test_xx.png', dpi=200, bbox_inches='tight', transparent=False)
# dpi: The resolution in dots per inch,设置分辨率,用于改变清晰度
# If *True*, the axes patches will all be transparent
# 16. 显示图片 show
plt.show()
scatter主要参数:
def scatter(self, x, y, s=None, c=None, marker=None, cmap=None, norm=None,vmin=None, vmax=None, alpha=None, linewidths=None,verts=None, edgecolors=None,**kwargs):"""A scatter plot of *y* vs *x* with varying marker size and/or color.Parameters----------x, y : array_like, shape (n, )The data positions.s : scalar or array_like, shape (n, ), optionalThe marker size in points**2.Default is ``rcParams['lines.markersize'] ** 2``.c : color, sequence, or sequence of color, optional, default: 'b'The marker color. Possible values:- A single color format string.- A sequence of color specifications of length n.- A sequence of n numbers to be mapped to colors using *cmap* and*norm*.- A 2-D array in which the rows are RGB or RGBA.Note that *c* should not be a single numeric RGB or RGBA sequencebecause that is indistinguishable from an array of values to becolormapped. If you want to specify the same RGB or RGBA value forall points, use a 2-D array with a single row.marker : `~matplotlib.markers.MarkerStyle`, optional, default: 'o'The marker style. *marker* can be either an instance of the classor the text shorthand for a particular marker.See `~matplotlib.markers` for more information marker styles.cmap : `~matplotlib.colors.Colormap`, optional, default: NoneA `.Colormap` instance or registered colormap name. *cmap* is onlyused if *c* is an array of floats. If ``None``, defaults to rc``image.cmap``.alpha : scalar, optional, default: NoneThe alpha blending value, between 0 (transparent) and 1 (opaque).linewidths : scalar or array_like, optional, default: NoneThe linewidth of the marker edges. Note: The default *edgecolors*is 'face'. You may want to change this as well.If *None*, defaults to rcParams ``lines.linewidth``.
14. 设置legend,【注意,'绘图测试’:一定要是可迭代格式,例如元组或者列表,要不然只会显示第一个字符,也就是legend会显示不全】
plt.legend(['绘图测试'], loc=2, fontsize = 10)
# plt.legend(['绘图测试'], loc='upper left', markerscale = 0.5, fontsize = 10) #这个也可
# markerscale:The relative size of legend markers compared with the originally drawn ones.
其参数loc对应为:
运行结果:
python绘制散点图,非常全,非常详细(已验证)相关推荐
- python如何根据数据画散点图_用python绘制散点图
用python绘制散点图 标签:#Python##散点图# 时间:2019/03/27 21:13:00 作者:夏天的风 今天下午学习了如何使用python绘制简单的散点图,写成博客分享一下. 在py ...
- python绘制散点图的步骤_python绘制散点图并标记序号的方法
python绘制散点图并标记序号的方法 实现二维平面上散点的绘制,并可以给每个散点标记序号或者名称: import numpy as np import matplotlib.pyplot as pl ...
- python绘制散点图:二分类样本
在做机器学习数据集的探索时,需要绘制二变量的散点图,散点图要能够区分正负样本. 如何用python绘制散点图呢?思路其实不复杂: 1.绘制正样本,使用"+"图标 2.在同一张图上绘 ...
- 【python图像处理】】python绘制散点图
python中用于绘图是matplotlib模块中的pyplot类,直接使用plot()函数绘制出的是折线图.而绘制散点图使用的是scatter()函数. 直接看下面的代码 #-*- coding: ...
- python绘制散点图、如何选两列作为横坐标_Python利用matplotlib绘制散点图的新手教程...
前言 上篇文章介绍了使用matplotlib绘制折线图,参考:https://www.jb51.net/article/198991.htm,本篇文章继续介绍使用matplotlib绘制散点图. 一. ...
- python绘制散点图运行结果是_用python绘制散点图
今天下午学习了如何使用python绘制简单的散点图,写成博客分享一下. 在python中画散点图主要是用matplotlib模块中的scatter函数,先来看一下scatter函数的基本信息. 网址为 ...
- python绘制语谱图(详细注释)
用python 绘制语谱图 1.步骤: 1)导入相关模块 2)读入音频并获取音频参数 3)将音频转化为可处理形式(注意读入的是字符串格式,需要转换成int或short型) 代码如下: import ...
- python绘制散点图-Python:matplotlib绘制散点图
与线型图类似的是,散点图也是一个个点集构成的.但不同之处在于,散点图的各点之间不会按照前后关系以线条连接起来. 用plt.plot画散点图 奇怪,代码和前面的例子差不多,为什么这里显示的却是散点图而不 ...
- python绘制散点图将整个区域分为10乘10个网格,并统计每个网格中点的个数
#绘制散点图将整个区域分为10乘10个网格,并统计每个网格中点的个数 from matplotlib import pyplot as plt import matplotlib as mpl imp ...
- 用python绘制散点图适用于通过读取txt的数据进行绘制
功能 1.绘制散点图 2.切片读取操作 3.改写容易 代码如下: import matplotlib.pyplot as plt import numpy as np# 定义画散点图的函数 def d ...
最新文章
- MySQL数据库启动报The server quit without updating PID file
- AIX HA模拟宕机--维护磁带机
- 大家都能读懂的IT生活枕边书
- TabHost 和 FragmentTabHost
- stm32烧不进去程序_STM32的FLASH和SRAM的使用情况分析
- iPhone 12 mini被质疑锁屏触摸不灵
- 样式中指定调用的效果
- 查看matlab当前路径,MATLAB R2012a 的当前路径和路径搜索
- securerandom java_Java 随机数 Random VS SecureRandom
- WINDOWS 2008的trustedinstallerexe占用过多CPU导致服务器性能下降的问题处理
- MATLAB基础教程-台大郭彦甫-学习笔记
- vs vsvim viemu vax 备忘
- leetcode【链表—中等】707.设计链表
- Unity——射线检测
- 项目合同管理:合同分类、费用支付方式、违约责任承担方式、签订注意事项、合同索赔流程
- 小人物吃金币_android小游戏(1)
- MCV EF增删改查
- php 多个一维数组合拼成二维数组的方法
- 华为太空人智能表盘代码仅需100行?
- ArcGIS投影坐标系下坐标值转换成地理坐标系经纬度