Python 在matplotlib中使用条件中的标记

Python 在matplotlib中使用条件中的标记,python,matplotlib,plot,Python,Matplotlib,Plot,我有一个问题。我正在使用stem函数绘制图形。我的问题是变量值中有很多零值。他们在我绘制的图表下面画了一条红线。当值为零时,有没有可能不做标记?多谢各位 keys = list(dictionary.keys()) values = list(dictionary.values()) plt.stem(keys,values,'-ro') plt.show() 一种方法(我不知道是否是matplotlibic方法): 首先过滤掉零值 将格式拆分为两个关键字,以停止绘制底部圆 策划它 结果:

我有一个问题。我正在使用stem函数绘制图形。我的问题是变量值中有很多零值。他们在我绘制的图表下面画了一条红线。当值为零时,有没有可能不做标记?多谢各位

keys = list(dictionary.keys())
values = list(dictionary.values())
plt.stem(keys,values,'-ro')
plt.show()
一种方法(我不知道是否是matplotlibic方法):

  • 首先过滤掉零值
  • 将格式拆分为两个关键字,以停止绘制底部圆
  • 策划它
结果:


我对matplotlib一无所知,但也许你可以在绘图之前先把零值取出来。所以可能类似于
filtered=filter(lambda x:x[1]!=0,dictionary.items())
@Hassan或dict comprehension,这就是我使用的(免责声明:我也不是完全精通matplotlib)@matsjoyce是的,这可能更好+1看起来你明白了。
import matplotlib.pyplot as plt
import random
dictionary = {i: random.choice([0] * 100 + list(range(100))) for i in range(100)}
new_dict = {i: j for i, j in dictionary.items() if j}
keys = list(new_dict.keys())
values = list(new_dict.values())

plt.stem(keys, values, markerfmt='ro', linefmt='-r')
plt.show()