gpt4 book ai didi

python - 在 matplotlib 中使用条件标记

转载 作者:太空宇宙 更新时间:2023-11-04 01:12:56 25 4
gpt4 key购买 nike

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

keys = list(dictionary.keys())
values = list(dictionary.values())
plt.stem(keys,values,'-ro')
plt.show()

enter image description here

最佳答案

一种实现方式(不知道是不是matplotlibic方式):

  • 首先过滤掉零值
  • 将格式拆分为两个关键字,以阻止绘制底部圆圈
  • 绘制它
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()

结果:

enter image description here

关于python - 在 matplotlib 中使用条件标记,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26535938/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com