【问题标题】:bar chart saved as html file using mpld3 shows x-axis ticks on top使用 mpld3 保存为 html 文件的条形图在顶部显示 x 轴刻度
【发布时间】:2018-01-13 16:44:43
【问题描述】:

我有一个 python 代码,它使用 mpld3 save_html 方法将条形图存储为 html 文件。但是,当我渲染 html 文件时,条形图在顶部显示 x 轴刻度,如下所示。

我根本不希望显示 刻度线,也没有刻度线标签,但希望 x 轴线位于底部。我以为我已经采取了必要的步骤来防止这种情况发生。请看下面的代码。 我将不胜感激这里的任何帮助。我正在消隐。

 fig1,ax1=plt.subplots()
      dsize=fig1.get_size_inches()    
      fig1.set_size_inches((dsize[0]/2)*(1.5),(dsize[1]/2)*(1.25))

      m_colors=  ['red','green','blue','yellow','black','cyan','aqua','brown','coral','magenta','khaki','indigo','lavender','navy','olive','pink','plum','teal','tan','violet','wheat','orchid']
     bucket=['apple','banana','orange','lemon','pineapple']
     counts=[10,20,5,16,2]
     rects = ax1.bar(bucket,counts,color=m_colors)
     ax1.set_xlabel('Service Types')


ax1.set_ylabel('Anomalous Flow per Service Type')
ax1.tick_params(axis='x',which='both',bottom='off',top='off',labelbottom='off')
ax1.xaxis.set_label_position('bottom') 
for rect in rects:
    height = rect.get_height()
    ax1.text(rect.get_x() + rect.get_width()/2., 1.05*height,
            '%d' % int(height),
            ha='center', va='bottom')
#leg=ax1.legend((rects),(unique))

legend_font_props = FontProperties()
legend_font_props.set_size('xx-small')
leg=ax1.legend((rects),(bucket),loc='upper right',prop=legend_font_props)


mpld3.save_html(fig1,"test.html")

【问题讨论】:

  • 请阅读How to Askminimal reproducible example。如果您在此处需要帮助,请提供这样一个可重现的示例。
  • 感谢您的提示 - 删除了不必要的代码。希望我已经修剪得够多了。
  • 这部分是关于不必要的代码,但更重要的是关于可重复性。您可以通过将问题中的代码复制并粘贴到新文件并运行它来检查您的示例是否完整且可验证。你会发现它不会运行,因此它不是minimal reproducible example
  • 同意 - 谢谢。我会编辑它。

标签: python-3.x matplotlib mpld3


【解决方案1】:

Mpld3 并没有完美地重新创建 matplotlib 图形,它只是试图接近相同的外观。在这种情况下,复制轴参数似乎有问题。

如果换行

ax1.tick_params(axis='x',which='both',bottom='off',top='off',labelbottom='off')
ax1.xaxis.set_label_position('bottom') 

ax1.set_xticks([])

结果会是这样的

这似乎接近你想要的。

【讨论】:

    猜你喜欢
    • 2021-06-18
    • 2020-05-29
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-08-19
    • 2021-07-12
    相关资源
    最近更新 更多