MatplotLib线条后的条形图

svgewumm  于 2023-10-24  发布在  其他
关注(0)|答案(1)|浏览(99)

我有下面详细介绍的函数。它可以很好地为我的比较生成条形图和折线图,但是线条在条形图后面。我做错了什么?lineInputbarInputxlabel变量是pd.DataFrame行,其他的是strbool s。

  1. import matplotlib.pyplot as plt
  2. import pandas as pd
  3. def plotLineBarResults(xAxis, lineInput, barInput, lineLabel, barLabel, xlabel, ylabel1,ylabel2, yformat, title,legend = True, grid=True):
  4. fig, ax =plt.subplots()
  5. ax2 = ax.twinx()
  6. ax.set_xticklabels(xAxis,rotation=45, ha="right")
  7. line1 = ax.plot(xAxis,lineInput, label = lineLabel, color = 'red')
  8. line2 = ax2.bar(xAxis, barInput, label = barLabel)
  9. ax.set_title(title)
  10. if legend:
  11. lines = line1+[line2]
  12. labs = [l.get_label() for l in lines]
  13. ax.legend(lines, labs,frameon = False, facecolor = 'white')
  14. if grid:
  15. ax.xaxis.grid(True,linestyle = '--')
  16. ax.yaxis.grid(True)
  17. ax.set_xlabel(xlabel)
  18. ax.set_ylabel(ylabel1)
  19. ax2.set_ylabel(ylabel2)
  20. ax.spines['right'].set_visible(False)
  21. ax.spines['left'].set_visible(False)
  22. ax.yaxis.set_major_formatter(FormatStrFormatter(yformat))
  23. fig.show()
vmjh9lq9

vmjh9lq91#

您可以单独绘制折线图,然后使用zorder将其覆盖在条形图的顶部。以下是如何做到这一点:

  1. import matplotlib.pyplot as plt
  2. from matplotlib.ticker import FormatStrFormatter
  3. def plotLineBarResults(xAxis, lineInput, barInput, lineLabel, barLabel, xlabel, ylabel1, ylabel2, yformat, title,
  4. legend=True, grid=True):
  5. fig, ax1 = plt.subplots()
  6. ax1.set_xlabel(xlabel)
  7. ax1.set_ylabel(ylabel1, color='red')
  8. ax1.bar(xAxis, barInput, label=barLabel, color='blue', alpha=0.7)
  9. ax2 = ax1.twinx()
  10. ax2.plot(xAxis, lineInput, label=lineLabel, color='red', marker='o', linestyle='-', linewidth=2, markersize=6,
  11. zorder=2)
  12. ax2.set_ylabel(ylabel2, color='red')
  13. ax1.set_xticks(range(len(xAxis)))
  14. ax1.set_xticklabels(xAxis, rotation=45, ha="right")
  15. ax1.spines['right'].set_visible(False)
  16. ax1.spines['left'].set_visible(False)
  17. ax2.spines['right'].set_visible(False)
  18. ax2.spines['left'].set_visible(False)
  19. ax1.yaxis.set_major_formatter(FormatStrFormatter(yformat))
  20. if legend:
  21. lines, labels = ax1.get_legend_handles_labels()
  22. lines2, labels2 = ax2.get_legend_handles_labels()
  23. ax2.legend(lines + lines2, labels + labels2, loc='upper left', frameon=False, facecolor='white')
  24. if grid:
  25. ax1.xaxis.grid(True, linestyle='--')
  26. ax1.yaxis.grid(True)
  27. ax1.set_title(title)
  28. plt.tight_layout()
  29. plt.show()

以下是示例/用例:

  1. xAxis = ['A', 'B', 'C', 'D', 'E']
  2. lineInput = [10, 15, 20, 25, 30]
  3. barInput = [5, 12, 8, 18, 10]
  4. lineLabel = 'Line Data'
  5. barLabel = 'Bar Data'
  6. xlabel = 'Categories'
  7. ylabel1 = 'Line Y-Axis'
  8. ylabel2 = 'Bar Y-Axis'
  9. yformat = '%.0f'
  10. title = 'Line and Bar Plot Example'
  11. plotLineBarResults(xAxis, lineInput, barInput, lineLabel, barLabel, xlabel, ylabel1, ylabel2, yformat, title)

这是输出:x1c 0d1x

展开查看全部

相关问题