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

Sus*_*han 2 matplotlib scatter-plot horizontal-line

我有一个pm2_5数据帧数据,我使用matplotlib scatterplot. 我想在不同的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 ='--')
Run Code Online (Sandbox Code Playgroud)

它应该是这样的: 在此输入图像描述

Sco*_*ton 6

您可以使用列表理解:

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