仅格式化选定的刻度标签

时间:2017-01-29 19:21:46

标签: python-3.x matplotlib

我是matplotlib的新手,并试图找出如何更改格式化选择的x刻度标签。为简单起见,我附上了简单的代码和图表。如何更改最后x刻度标签的字体颜色(本例中为5.0)?

 if openingFromPush {
            let viewController = storyboard.instantiateViewController(withIdentifier: "ViewController") as! ViewController
            viewController.hidesBottomBarWhenPushed = true
            if let nvc = tabBarController.viewControllers?[0] as? UINavigationController {
                nvc.pushViewController(viewController, animated: false)
            }

            window?.rootViewController = tabBarController
        }

enter image description here

1 个答案:

答案 0 :(得分:3)

您可以通过ax.get_xticklabels()获取xticklabels。这将返回matplotlib.text.Text个实例的列表。然后,您可以选择其中一个并使用text.set_color("red")为其着色。

ax.get_xticklabels()[-2].set_color("red")

enter image description here

问题在于,它并不直观地清楚哪个元素是我们正在寻找的元素。在这种情况下,它是倒数第二,因为在列表的最末端有一个空的ticklabel。这可能需要测试一下,或在设置之前打印出来。此外,如果调整绘图的大小,使得更多的标签出现在轴上,格式化的标签可能会突然携带与之前不同的数字。这种情况需要更多的工作来解释。

<小时/> 当然,从颜色开始,您可以更改您喜欢的text实例的每个属性,

ax.get_xticklabels()[-2].set_color("white")
ax.get_xticklabels()[-2].set_fontsize(14)
ax.get_xticklabels()[-2].set_weight("bold")
ax.get_xticklabels()[-2].set_bbox(dict(facecolor="red", alpha=0.9))

enter image description here

相关问题