查看原文
其他

Python可视化30|matplotlib-辅助线(axhline|vlines|axvspa|axhspan)

pythonic生物人 pythonic生物人 2022-09-11

"pythonic生物人"的第75篇分享

本文介绍matplotlib中如何绘制垂直坐标轴的线区域

本文速览

欢迎关注@pythonic生物人


添加x轴y轴垂直辅助线

  • matplotlib.pyplot.axhline(y=0, xmin=0, xmax=1, **kwargs)
  • matplotlib.pyplot.vlines(x, ymin, ymax, colors=None, linestyles='solid', label='', , data=None, \*kwargs)
# y轴垂直线
import matplotlib.pyplot as plt
plt.figure(dpi=120)
plt.style.use('seaborn-whitegrid')


plt.axhline(y=.5#线高
            xmin=0.2,#线起始位置
            xmax=0.75,#线结束位置
            color="#c72e29",
            linestyle='--',#线型
            linewidth=1.5,#线宽
            marker='*',#线两端marker
            markerfacecolor='w',
            markersize=10,#marker大小
         )

# x轴垂直线
plt.vlines(x=1, ymin=0.1, ymax=2,
           lw=1.5,
           colors='r',
           linestyles='--',
           #其他参数,参考matplotlib.pyplot.vlines官网
          )

plt.show()

添加x y轴垂直区域

  • matplotlib.pyplot.axvspan(xmin, xmax, ymin=0, ymax=1, **kwargs)
  • matplotlib.pyplot.axhspan(ymin, ymax, xmin=0, xmax=1, **kwargs)
import matplotlib.pyplot as plt

plt.figure(dpi=100)
plt.plot([1,2,6],lw=1)
plt.axvspan(1.01.2, facecolor='g', alpha=0.3, **dict())#垂直x轴区域
plt.axhspan(4.05.2, facecolor='pink', alpha=0.3, **dict())#垂直y轴区域

参考资料

  • https://matplotlib.org/api/_as_gen/matplotlib.pyplot.hlines.html?highlight=hlines#matplotlib.pyplot.hlines
  • https://matplotlib.org/api/_as_gen/matplotlib.pyplot.vlines.html#matplotlib.pyplot.vlines
  • https://matplotlib.org/api/_as_gen/matplotlib.pyplot.axhline.html#matplotlib.pyplot.axhline
  • https://matplotlib.org/api/_as_gen/matplotlib.pyplot.axhspan.html#matplotlib.pyplot.axhspan
  • https://matplotlib.org/api/_as_gen/matplotlib.pyplot.axvspan.html#matplotlib.pyplot.axvspan

更多好文,欢迎关注@pythonic生物人


同系列文章

Python可视化27|seaborn绘制线型回归图曲线
Python可视化28|matplotlib绘制韦恩图(2-6组数据)
Python可视化29|matplotlib-饼图(pie)


"点赞"、"在看"鼓励下呗

您可能也对以下帖子感兴趣

文章有问题?点此查看未经处理的缓存