如何在python图中的特定条上设置标记

  • 本文关键字:设置 python python matplotlib
  • 更新时间 :
  • 英文 :


我有以下代码来生成堆叠条形图。

width=.35
m1_t = pd.DataFrame({
'System':[13.77,40.72,114.59,141.71,174.59, 217.95, 237.26, 261.8,  297.46, 332.25, 366.08, 351.9,404.33,436.06,465.13, 494.56, 497.43, 526.82, 545.34, 563.61, 581.92, 594.94, 610.23, 665.7,  688.82],
'Cloud':[699.52,673.49, 601.76, 577.89,546.8,502.07,484.37,460.50,424.14,387.32,351.01,316.27,312.93,279.82,247.59,214.86,  183.4,182.47,163.05,143.42,123.92,111.27,96.27,39.95,16.98],
'Communication':[942.4,18072,18072,5019.2,  9000,9000,2510.4,5019.2,5019.2,5019.2,5019.2,1256,2510.4,2510.4,2510.4,2510.4,628.8,628.8,628.8,628.8,628.8,144,144,272,272],
'Output_data': [0.589,12.545,12.545,3.137,6.273,6.273,1.569,3.137,3.137,3.137,3.137,0.785,  1.569,1.569,1.569,1.569,0.393,0.393,0.393,0.393,0.393,0.09,0.09,0.17,0.17]})
colors = ['#5DADE2', 'orange', '#D5DBDB', '#273746', '#FFDEAD']
m1_t[['System','Cloud','Communication']].plot(kind='bar', width = width,stacked=True,color=colors,figsize=(6.5, 3))
plt.ylabel("Latency (ms)")
# plt.yscale('log')
plt.ylim(0, 20000)
m1_t['Output_data'].plot(secondary_y=True, color='darkslategrey', marker='o', MarkerSize=2)
ax = plt.gca()
plt.xlim([-width, len(m1_t['Communication']) - width])
plt.xticks(rotation=45, size=2)
ax.set_xticklabels(('1', '2', '3', '4', '5', '6', '7', '8', '9', '10', '11', '12', '13', '14','15', '16', '17', '18', '19','20','21','22','23','24','25'))
plt.ylim(0, 14)
plt.ylabel("Output-data (MB)")
plt.show()

我需要在一个特定的条形图上定义一个圆形标记,以及它的图例(在本例中为条形图17(,就像下面链接中的那个一样https://drive.google.com/file/d/1ER8PFzQLzLSsH25A_jvl9xLlQJ-R320T/view?usp=sharing.我尝试了不同的解决方案,但我无法做到这一点。感谢的帮助

试试这个:

ax.scatter(16, m1_t['Output_data'][16], label='test label', marker='*', c='r')
ax.legend(loc='center right')

--编辑--

更改:

m1_t['Output_data'].plot(secondary_y=True, color='darkslategrey', marker='o', MarkerSize=2)
ax.scatter(16, m1_t['Output_data'][16], label='test label', marker='*', c='r')

至:

m1_t['Output_data'].plot(secondary_y=True, color='darkslategrey', marker='o', MarkerSize=2, zorder=0)
ax.scatter(16, m1_t['Output_data'][16], label='test label', marker='*', c='r', zorder=1)

您要查找的参数是zorder

最新更新