- c - 在位数组中找到第一个零
- linux - Unix 显示有关匹配两种模式之一的文件的信息
- 正则表达式替换多个文件
- linux - 隐藏来自 xtrace 的命令
我想根据 x/y 轴上的点数缩放 matplotlib.pyplot.Axes.scatter
图的 markersize
。
import matplotlib.pyplot as plt
import numpy as np
vmin = 1
vmax = 11
x = np.random.randint(vmin, vmax, 5)
y = np.random.randint(vmin, vmax, 5)
fig, ax = plt.subplots()
for v in np.arange(vmin, vmax):
ax.axvline(v - 0.5)
ax.axvline(v + 0.5)
ax.axhline(v - 0.5)
ax.axhline(v + 0.5)
ax.set_xlim(vmin - 0.5, vmax + 0.5)
ax.set_ylim(vmin - 0.5, vmax + 0.5)
ax.scatter(x, y)
ax.set_aspect(1)
plt.show()
ax
始终使用相等的纵横比,并且两个轴具有相同的 lim
值。
两个图中的 markersize
保留为默认值,即 markersize=6
。
我的问题是,如何计算 markersize
值,以便 marker
接触到每个单元格的边缘? (每个单元格最多有一个数据点。)
最佳答案
一个简单的选择是用一个 PatchCollection
替换 scatter,该 PatchCollection
由半径为 0.5 的 Circles
组成。
circles = [plt.Circle((xi,yi), radius=0.5, linewidth=0) for xi,yi in zip(x,y)]
c = matplotlib.collections.PatchCollection(circles)
ax.add_collection(c)
如果需要散点图,另一种方法是将标记大小更新为数据单位。
这里最简单的解决方案是先绘制图形一次,然后获取轴大小并从中计算以点为单位的标记大小。
import matplotlib.pyplot as plt
import numpy as np
vmin = 1
vmax = 11
x = np.random.randint(vmin, vmax, 5)
y = np.random.randint(vmin, vmax, 5)
fig, ax = plt.subplots(dpi=141)
for v in np.arange(vmin, vmax):
ax.axvline(v - 0.5)
ax.axvline(v + 0.5)
ax.axhline(v - 0.5)
ax.axhline(v + 0.5)
ax.set_xlim(vmin - 0.5, vmax + 0.5)
ax.set_ylim(vmin - 0.5, vmax + 0.5)
ax.set_aspect(1)
fig.canvas.draw()
s = ((ax.get_window_extent().width / (vmax-vmin+1.) * 72./fig.dpi) ** 2)
ax.scatter(x, y, s = s, linewidth=0)
plt.show()
有关如何使用散点标记大小的一些背景,请参见例如this answer .上述解决方案的缺点是将标记大小固定为图的大小和状态。万一轴限制发生变化或图被缩放,散点图将再次具有错误的大小。
因此,以下解决方案将更通用。这有点复杂,其工作方式与 Plotting a line with width in data units 类似。 .
import matplotlib.pyplot as plt
import numpy as np
vmin = 1
vmax = 32
x = np.random.randint(vmin, vmax, 5)
y = np.random.randint(vmin, vmax, 5)
fig, ax = plt.subplots()
for v in np.arange(vmin, vmax):
ax.axvline(v - 0.5)
ax.axvline(v + 0.5)
ax.axhline(v - 0.5)
ax.axhline(v + 0.5)
ax.set_xlim(vmin - 0.5, vmax + 0.5)
ax.set_ylim(vmin - 0.5, vmax + 0.5)
class scatter():
def __init__(self,x,y,ax,size=1,**kwargs):
self.n = len(x)
self.ax = ax
self.ax.figure.canvas.draw()
self.size_data=size
self.size = size
self.sc = ax.scatter(x,y,s=self.size,**kwargs)
self._resize()
self.cid = ax.figure.canvas.mpl_connect('draw_event', self._resize)
def _resize(self,event=None):
ppd=72./self.ax.figure.dpi
trans = self.ax.transData.transform
s = ((trans((1,self.size_data))-trans((0,0)))*ppd)[1]
if s != self.size:
self.sc.set_sizes(s**2*np.ones(self.n))
self.size = s
self._redraw_later()
def _redraw_later(self):
self.timer = self.ax.figure.canvas.new_timer(interval=10)
self.timer.single_shot = True
self.timer.add_callback(lambda : self.ax.figure.canvas.draw_idle())
self.timer.start()
sc = scatter(x,y,ax, linewidth=0)
ax.set_aspect(1)
plt.show()
(由于 this issue,我更新了代码以使用计时器重绘 Canvas )
关于python - 按 x 比例缩放 matplotlib.pyplot.Axes.scatter markersize,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48172928/
我有三个相同长度的向量。其中两个包含我要绘制的内容的 X 和 Y 坐标。第三个包含我想关联绘制圆的半径的值。 我读到plot中的'MarkerSize'对应圆周上的点数,如果我想使用scatter,第
我正在尝试绘制一个 DataFrame 并且我想修改标记大小,但似乎我无法在同一个 plot() 调用中执行此操作。 ax0 = df_can_t.plot(kind='scatter', x='Ye
这有效: plot(t, pa_iso, '-rs', 'MarkerEdgeColor', 'k', 'MarkerFaceColor', 'r', 'MarkerSize', 5); plot(t
我想根据 x/y 轴上的点数缩放 matplotlib.pyplot.Axes.scatter 图的 markersize。 import matplotlib.pyplot as plt impor
我是一名优秀的程序员,十分优秀!