英文:
Highlight a particular value in X axis
问题
I want to highlight particular values on x axis in Matplotlib.
list1=[1,2,3,4,5,6,7,8,9] - x轴数值
list2=[1,2,23,4,5,6,7,8,9] - y轴数值
list3=[2,3,6,7]
plt.plot(list1, list2)
plt.show()
I want to highlight the x轴标签以及其对应的数值,当list3中的数值存在于list1中时,用红色/绿色或其他颜色突出显示。
英文:
I want to highlight particular values on x axis in Matplotlib .
list1=[1,2,3,4,5,6,7,8,9] - xaxis values
list2=[1,2,23,4,5,6,7,8,9] - y-axis values
list3=[2,3,6,7]
plt.plot(list1,list2)
plt.show()
I want to highlight the xaxis labels as well its corresponding values in any color like red/green or whatever only when list3 value is present in list1
答案1
得分: 1
一个选项,与坐标轴标签无关,可以是为目标值绘制标记:
import numpy as np
m = np.isin(list1, list3)
plt.plot(list1, list2)
plt.plot(np.array(list1)[m], np.array(list2)[m], ls='', marker='x')
输出:
[![enter image description here][1]][1]
如果你还想要标签:
m = np.isin(list1, list3)
ax = plt.subplot()
ax.plot(list1, list2)
ax.plot(np.array(list1)[m], np.array(list2)[m], ls='', marker='x')
S = set(list3)
for tick in ax.get_xticklabels():
if int(tick.get_text()) in S:
tick.set_color('r')
输出:
[![enter image description here][2]][2]
[1]: https://i.stack.imgur.com/QmtYS.png
[2]: https://i.stack.imgur.com/agOOB.png
英文:
One option, independently of the axis labels, could be to plot markers for the target values:
import numpy as np
m = np.isin(list1, list3)
plt.plot(list1, list2)
plt.plot(np.array(list1)[m], np.array(list2)[m], ls='', marker='x')
Output:
If you also want the labels:
m = np.isin(list1, list3)
ax = plt.subplot()
ax.plot(list1, list2)
ax.plot(np.array(list1)[m], np.array(list2)[m], ls='', marker='x')
S = set(list3)
for tick in ax.get_xticklabels():
if int(tick.get_text()) in S:
tick.set_color('r')
Output:
答案2
得分: 0
You could try using matplotlib.pyplot.annotate
or axvspan
. These are 3 possible solutions I could think of.
import matplotlib.pyplot as plt
list1 = [1, 2, 3, 4, 5, 6, 7, 8, 9] # - x-axis values
list2 = [1, 2, 23, 4, 5, 6, 7, 8, 9] # - y-axis values
list3 = [2, 3, 6, 7]
plt.plot(list1, list2)
for value in list3:
index = list1.index(value)
x = list1[index]
y = list2[index]
plt.annotate("X", (x, y), xytext=(x - 0.1, y - 0.25), color="red")
plt.annotate("X", (x, y), xytext=(x - 0.1, -0.5), color="red")
plt.axvspan(list1[index] - 0.1, list1[index] + 0.1, alpha=0.3, color="green")
plt.show()
In this case, in the xytext
, I subtracted 0.1
and 0.25
(or -0.5
) just for better overlapping of the "X" since this method is used for text annotation. In the case of axvspan
, I used +/-0.1
of the coloring range.
Hope it helps!
英文:
You could try using matplotlib.pyplot.annotate
or axvspan
. These are 3 possible solution I could think of.
import matplotlib.pyplot as plt
list1=[1,2,3,4,5,6,7,8,9] #- xaxis values
list2=[1,2,23,4,5,6,7,8,9] #- y-axis values
list3=[2,3,6,7]
plt.plot(list1,list2)
for value in list3:
index = list1.index(value)
x = list1[index]
y = list2[index]
plt.annotate("X",(x,y),xytext=(x-.1,y-.25), color="red")
plt.annotate("X",(x,y),xytext=(x-.1,-0.5), color="red")
plt.axvspan(list1[index]-.1, list1[index]+.1, alpha=0.3, color="green")
plt.show()
In this case in the xytext
I substracted 0.1
and 0.25
(or -0.5
) just for better overlapping of the "X" since this method is used for text annotation. In the case of axvspan
I used +/-0.1
of the coloring range.
Hope it helps!
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论