我正在尝试创建一个简单的散点图。为了这个特定的目的,我想用不同颜色的原点周围的同心圆(如3个区域的靶心)。我想知道,如果有类似axvspan
和axhspan
的内容,但同心着色?
让我举个例子:
import pandas as pd
import numpy as np
import matplotlib.pyplot as plt
x = x = np.linspace(0, 20, 50)
y = np.cos(3*x)
a = 3 # radius 0 to >a
b = 5 # radius a to >b
c = 7 # radius b to c
plt.axvspan(a, b, color='r', alpha = 0.5)
plt.axhspan(a, b, color='y', alpha = 0.5)
plt.scatter(x, y)
plt.show()
我需要同心绿色阴影,而不是水平和垂直阴影,原点为半径a
,黄色从a
到b
,红色来自b
到c
。有任何想法吗?
答案 0 :(得分:1)
这是我的解决方案:
import numpy as np
import matplotlib.pyplot as plt
fig, ax = plt.subplots()
x = np.linspace(0, 20, 50)
y = np.cos(3*x)
a = 3 # radius 0 to >a
b = 5 # radius a to >b
c = 7 # radius b to c
circle1 = plt.Circle((0, 0), a, color='green', alpha=0.3)
circle2 = plt.Circle((0, 0), b, color='yellow', alpha=0.3)
circle3 = plt.Circle((0, 0), c, color='red', alpha=0.3)
ax.add_artist(circle3)
ax.add_artist(circle2)
ax.add_artist(circle1)
plt.scatter(x, y)
plt.axis([-22, 22, -22, 22])
plt.show()
输出: