我试图在Python中创建一个色轮,最好使用Matplotlib。以下操作正常:
import numpy as np
import matplotlib as mpl
import matplotlib.pyplot as plt
xval = np.arange(0, 2*pi, 0.01)
yval = np.ones_like(xval)
colormap = plt.get_cmap('hsv')
norm = mpl.colors.Normalize(0.0, 2*np.pi)
ax = plt.subplot(1, 1, 1, polar=True)
ax.scatter(xval, yval, c=xval, s=300, cmap=colormap, norm=norm, linewidths=0)
ax.set_yticks([])
然而,这种尝试有两个严重的缺点。
首先,当将生成的图形保存为矢量(figure_1.svg)时,色轮由621个不同形状组成(如预期),对应于正在绘制的不同(x,y)值。虽然结果看起来像一个圆圈,但实际上不是。我更喜欢使用一个实际的圆,由几个路径点和它们之间的Bezier曲线定义,例如^{
其次(相关地),最后绘制的标记(在2*pi
之前的最后几个)与前几个重叠。在像素渲染中很难看到,但是如果放大基于矢量的渲染,可以清楚地看到最后一张光盘与前几张光盘重叠。
我试着使用不同的标记(.
或|
),但没有一个是围绕第二个问题。
底线:我能用Python/Matplotlib画一个圆吗?这个圆是用合适的向量/Bezier曲线定义的,它的边颜色是根据一个颜色映射(或者,如果不能,任意的颜色梯度)定义的?
我只需要做一个色轮,决定更新rsnape的解决方案,使之与matplotlib 2.1兼容。您可以在极坐标图上绘制极坐标彩色网格,而不是将colorbar对象放置在轴上。
它给出了:
我发现的一种方法是生成一个彩色地图,然后将其投影到极轴上。这是一个有效的例子-它包括一个讨厌的黑客,虽然(明确评论)。我确信有一种方法可以调整限制或者(更难)编写自己的
Transform
来绕过它,但我还没有完全做到这一点。我以为对Normalize
的调用的界限可以做到这一点,但显然不行。这就产生了
如果你想要戒指而不是轮子,在
plt.show()
或plt.savefig
之前使用它这给了
根据注释中的@EelkeSpaak-如果您按照OP将图形保存为SVG,这里有一个处理结果图形的提示:结果SVG图像的小元素是接触的和不重叠的。这会导致某些渲染器(Inkscape、Adobe Reader,可能不是打印的)出现模糊的灰色线条。解决这个问题的一个简单方法是使用Inkscape或Illustrator对每个单独的渐变元素应用一个小的缩放比例(例如120%)。注意,您必须将转换分别应用于每个元素(所提到的软件提供了自动执行此操作的功能),而不是应用于整个图形,否则它不会产生任何效果。
相关问题 更多 >
编程相关推荐