python 散点图点击链接图片_Python散点图。 标记的大小和样式
我有一组要显示为散点图的数据。 我希望将每个点绘制为大小dx的正方形。
1
2
3
4
5
6x = [0.5,0.1,0.3]
y = [0.2,0.7,0.8]
z = [10.,15.,12.]
dx = [0.05,0.2,0.1]
scatter(x,y,c=z,s=dx,marker='s')
问题是散布函数读取的大小s以点^ 2为单位。 我想要的是让每个点都由面积dx ^ 2的正方形表示,该面积以"真实"单位(绘图单位)表示。 希望您能明白这一点。
我还有另一个问题。 散点图功能用黑色边框绘制标记,如何删除该选项并且完全没有边框?
从用户数据坐标系转换为显示坐标系。
并使用edgecolors ='none'绘制没有轮廓的面。
1
2
3
4
5
6import numpy as np
fig = figure()
ax = fig.add_subplot(111)
dx_in_points = np.diff(ax.transData.transform(zip([0]*len(dx), dx)))
scatter(x,y,c=z,s=dx_in_points**2,marker='s', edgecolors='none')
这不会按照OP的要求以绘图单位绘制正方形,但是不会调整大小的固定大小的正方形(例如,通过手动更改图形框大小)。
这可能是一个愚蠢的问题。但是,如果dx不是数组而是每个点(x,y,z)都相同,那么如何更改上面的代码。此外,我真的需要使用add_subplot吗?
您如何找到edgecolors参数?
如果您希望标记的大小与图形大小相同,则可以使用补丁:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18from matplotlib import pyplot as plt
from matplotlib.patches import Rectangle
x = [0.5, 0.1, 0.3]
y = [0.2 ,0.7, 0.8]
z = [10, 15, 12]
dx = [0.05, 0.2, 0.1]
cmap = plt.cm.hot
fig = plt.figure()
ax = fig.add_subplot(111, aspect='equal')
for x, y, c, h in zip(x, y, z, dx):
ax.add_artist(Rectangle(xy=(x, y),
color=cmap(c**2), # I did c**2 to get nice colors from your numbers
width=h, height=h)) # Gives a square of area h*h
plt.show()
注意:
正方形不在(x,y)的中心。 x,y实际上是
左下角的正方形。我以这种方式简化了我的代码。您
应该使用(x + dx/2, y + dx/2)。
颜色是从热色图中获取的。我用z ** 2给出颜色。
您还应该根据自己的需要进行调整
最后是您的第二个问题。您可以使用关键字参数edgecolor或edgecolors来获得散点标记的边框。它们分别是matplotlib颜色参数或rgba元组序列。如果将参数设置为"无",则不会绘制边框。
我认为我们可以通过添加补丁来做得更好。
根据文件:
This (PatchCollection) makes it easier to assign a color map to a heterogeneous
collection of patches.
This also may improve plotting speed, since PatchCollection will
draw faster than a large number of patches.
假设您要以数据单位绘制具有给定半径的圆的散布:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67def circles(x, y, s, c='b', vmin=None, vmax=None, **kwargs):
"""
Make a scatter of circles plot of x vs y, where x and y are sequence
like objects of the same lengths. The size of circles are in data scale.
Parameters
----------
x,y : scalar or array_like, shape (n, )
Input data
s : scalar or array_like, shape (n, )
Radius of circle in data unit.
c : color or sequence of color, optional, default : 'b'
`c` can be a single color format string, or a sequence of color
specifications of length `N`, or a sequence of `N` numbers to be
mapped to colors using the `cmap` and `norm` specified via kwargs.
Note that `c` should not be a single numeric RGB or RGBA sequence
because that is indistinguishable from an array of values
to be colormapped. (If you insist, use `color` instead.)
`c` can be a 2-D array in which the rows are RGB or RGBA, however.
vmin, vmax : scalar, optional, default: None
`vmin` and `vmax` are used in conjunction with `norm` to normalize
luminance data. If either are `None`, the min and max of the
color array is used.
kwargs : `~matplotlib.collections.Collection` properties
Eg. alpha, edgecolor(ec), facecolor(fc), linewidth(lw), linestyle(ls),
norm, cmap, transform, etc.
Returns
-------
paths : `~matplotlib.collections.PathCollection`
Examples
--------
a = np.arange(11)
circles(a, a, a*0.2, c=a, alpha=0.5, edgecolor='none')
plt.colorbar()
License
--------
This code is under [The BSD 3-Clause License]
(http://opensource.org/licenses/BSD-3-Clause)
"""
import numpy as np
import matplotlib.pyplot as plt
from matplotlib.patches import Circle
from matplotlib.collections import PatchCollection
if np.isscalar(c):
kwargs.setdefault('color', c)
c = None
if 'fc' in kwargs: kwargs.setdefault('facecolor', kwargs.pop('fc'))
if 'ec' in kwargs: kwargs.setdefault('edgecolor', kwargs.pop('ec'))
if 'ls' in kwargs: kwargs.setdefault('linestyle', kwargs.pop('ls'))
if 'lw' in kwargs: kwargs.setdefault('linewidth', kwargs.pop('lw'))
patches = [Circle((x_, y_), s_) for x_, y_, s_ in np.broadcast(x, y, s)]
collection = PatchCollection(patches, **kwargs)
if c is not None:
collection.set_array(np.asarray(c))
collection.set_clim(vmin, vmax)
ax = plt.gca()
ax.add_collection(collection)
ax.autoscale_view()
if c is not None:
plt.sci(collection)
return collection
scatter函数的所有参数和关键字(marker除外)将以相似的方式工作。
我写了一个要点,包括圆形,椭圆形和正方形/矩形。如果您想要其他形状的集合,则可以自己修改。
如果要绘制颜色条,只需运行colorbar()或将返回的收集对象传递给colorbar函数。
一个例子:
1
2
3
4
5
6
7
8
9
10
11
12
13
14from pylab import *
figure(figsize=(6,4))
ax = subplot(aspect='equal')
#plot a set of circle
a = arange(11)
out = circles(a, a, a*0.2, c=a, alpha=0.5, ec='none')
colorbar()
#plot one circle (the lower-right one)
circles(1, 0, 0.4, 'r', ls='--', lw=5, fc='none', transform=ax.transAxes)
xlim(0,10)
ylim(0,10)
输出:
我想在开源项目中使用您的函数,但不能这样做,因为默认情况下,所有SO代码均受CC BY-SA许可。您可以明确声明代码的许可,最好是类似BSD的许可吗?
@neo很高兴知道这一点。我不熟悉许可证,我认为应该与matplotlib保持一致,因为我只是基于scatter函数编写了此代码。所以应该是PSF或其他?
您的代码段不是matplotlib的派生作品,因此您可以在任何许可下许可代码。我只会使用BSD 3子句,它在Python世界中很常见。
@neo那很好。不适使用BSD 3句。
为了使此Python 3兼容,我添加了以下代码片段
1
2
3
4try:
basestring
except NameError:
basestring = str
从
如何检查变量是否为具有python 2和3兼容性的字符串
这是必需的,因为basestring在Python 3中不可用。在Python 2中,basestring的目的是同时包含str和unicode。在Python 3中,str和unicode之间没有区别,只是str。
python 散点图点击链接图片_Python散点图。 标记的大小和样式相关推荐
- python 散点图点击链接图片_在Python和matplotlib中连接三维散点图中的两点
在这些点之间绘制线段:import matplotlib.pyplot from mpl_toolkits.mplot3d import Axes3D dates = [20020514, 20020 ...
- python散点图点的大小-Python散点图。 标记的大小和样式
我有一组要显示为散点图的数据. 我希望将每个点绘制为大小dx的正方形. 1 2 3 4 5 6x = [0.5,0.1,0.3] y = [0.2,0.7,0.8] z = [10.,15.,12.] ...
- python docx 合并文档 图片_Python+pymupdf处理PDF文档案例6则
推荐图书:<Python程序设计(第3版)>,(ISBN:978-7-302-55083-9),清华大学出版社,2020年6月第1次印刷,7月第2次印刷 京东购买链接:https://it ...
- python爬取贴吧图片_Python爬取贴吧多页图片
Python爬取贴吧图片都只能爬取第一页的,加了循环也不行,现在可以了. #coding:utf-8 import urllib import urllib2 import re import os ...
- python 生成pdf 文字和图片_Python系列—PDF文本与图片抽取
PDF是人们日常使用最多的跨平台文档.其是一种用独立于应用程序.硬件.操作系统的方式呈现文档的文件格式.每个PDF文件包含固定布局的平面文档的完整描述,包括文本.字形.图形及其他需要显示的信息.具有良 ...
- python docx 合并文档 图片_Python检查Word文件中包含特定关键字的所有页码
推荐教材:<Python程序设计基础与应用>(ISBN:9787111606178),董付国,机械工业出版社图书详情:配套资源:用书教师可以联系董老师获取教学大纲.课件.源码.教案.考试系 ...
- python gui按顺序显示图片_python tkinter GUI绘制,以及点击更新显示图片代码
tkinter 绘制GUI简单明了,制作一些简单的GUI足够,目前遇到的一个问题是不能同时排列显示多幅图片(目前没找到同时显示解决方法), 退而求其次,改成增加一个update按钮,每次点下按钮自动更 ...
- python设置散点图点的大小_Python散点图 . 标记的大小和样式
我想我们可以通过一系列补丁来做得更好 . 根据文件: 此(PatchCollection)可以更轻松地将颜色映射分配给异构补丁集合 . 这也可以提高绘图速度,因为PatchCollection将比大量 ...
- python散点图拟合曲线如何求拟合_python散点图最佳拟合直线的代码
你可以用numpy的polyfit.我使用以下(您可以安全地删除关于确定系数和误差界限的位,我只是认为它看起来不错):#!/usr/bin/python3 import numpy as np imp ...
最新文章
- SQL Server 之AdventureWorks 2008 安
- java寄存器_汇编学习 1 寄存器的作用 寻址方式 - DraculaW - JavaEye技术网站
- mysql webservice接口_WebService接口在PHP中的使用
- 开发日记-20190522 关键词 读书笔记《鸟哥的Linux私房菜-基础学习篇》
- pythonjoin函数所在包_Python中的join()函数
- java写入文件编码格式为ansi_Windows10 bat批处理删除 快速打开文件夹 固定到开始菜单或任务栏...
- 2017282110261-高级软件工程第二次作业
- 2021-08-04 Mysql自连接
- linux内核源码多大,需要多久才能看完linux内核源码?
- 傅里叶变换(时域频域)
- Thrift交流(二)thrift服务端和客户端实现 Nifty
- 我的Lenovo ThinkPad R60e 键盘按键失灵!
- Apue学习:高级I/O
- 保监会欲放险资投房产
- 吴国忠先生谈郑曼青太极拳之思路
- 联想小新14pro锐龙版网卡rtl8852ae在ubunru18.04装网卡驱动
- 联想拯救者笔记本(R720、y7000、y7000p)安装ubuntu无法使用无线网卡
- javascript鼠标拖尾特效
- Python推荐几个很不错的学习资源
- 如何通过短信转发在iPad和Mac上发送和接收短信