【问题标题】:Way to change only the width of marker in scatterplot but not height?仅更改散点图中标记的宽度而不更改高度的方法?
【发布时间】:2019-09-13 11:55:53
【问题描述】:

我想制作一个标记类型为矩形(不是正方形)的散点图,这样宽度大于高度。使用“s”我可以控制标记的整体大小,但它在两个维度上都会增加。

我不能直接传递高度和宽度,因为这些是分散的未知属性。

import numpy as np
import matplotlib.pyplot as plt

fig, ax = plt.subplots()
ax.scatter(np.arange(1,6), np.random.normal(size=5), marker='s', s=16)

【问题讨论】:

  • marker="$▬$" ?
  • 这适用于形状,但不能很好地控制高度和宽度的比例。

标签: python matplotlib height marker scatter


【解决方案1】:

试试下面的 sn-p。

import numpy as np
import matplotlib.pyplot as plt
width = 60
height = 30
verts = list(zip([-width,width,width,-width],[-height,-height,height,height]))
fig, ax = plt.subplots()
ax.scatter(np.arange(1,6), np.random.normal(size=5), marker=(verts,0),s=40)

这里,参数s 改变了散点的大小。绘制的矩形保持比例width/height

输出:

更新

自 matplotlib 3.2x 起,(verts, 0) 的使用已被弃用。工作代码应改为

fig, ax = plt.subplots()
ax.scatter(np.arange(1,6), np.random.normal(size=5), marker=verts, s=40)

【讨论】:

  • 使用 matplot3.1 它抱怨“'list' 和 'int' 的实例之间不支持'
猜你喜欢
  • 2016-07-29
  • 2020-10-27
  • 1970-01-01
  • 1970-01-01
  • 2012-10-15
  • 1970-01-01
  • 1970-01-01
  • 2016-09-11
  • 2021-12-24
相关资源
最近更新 更多