gpt4 book ai didi

matplotlib - 有没有办法在 matplotlib 的单个图中使用 hlines() 函数绘制多条水平线?

转载 作者:行者123 更新时间:2023-12-02 16:42:25 26 4
gpt4 key购买 nike

我有一个 pm2_5 数据框数据,我使用 matplotlib 散点图 绘制了这些数据。我想在不同的 y 值 处插入多条水平线,我通过为 y 的每个不同值手动调用 '''ax.axhline''' 函数来实现。有什么方法可以使整个过程自动化?

# making a graph with delineated health levels of pm2.5 in the year 2015
fig, ax=plt.subplots(figsize=(10,7));
pm2_5.plot(kind='scatter',x='S_no',y='pm2_5',c='pm2_5',ax=ax, cmap='tab20b');
ax.axhline(y=150,linestyle ='--')
ax.axhline(y=100,linestyle ='--')
ax.axhline(y=200,linestyle ='--')
ax.axhline(y=300,linestyle ='--')

它应该是这样的: enter image description here

最佳答案

您可以使用列表理解:

[ax.axhline(y=i, linestyle='--') for i in [150,100,200,300]]

关于matplotlib - 有没有办法在 matplotlib 的单个图中使用 hlines() 函数绘制多条水平线?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/61347471/

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