微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

混合水平和垂直刻度

如何解决混合水平和垂直刻度

我有下面显示的示例图。如何使第一个和第三个 x 轴水平刻度,第二个和第四个刻度垂直?

import matplotlib.pyplot as plt

x = [1,2,3,4]
y = [1,4,9,6]
labels = ['Frogs','Hogs','Bogs','Slogs']

plt.plot(x,y)
plt.xticks(x,labels,rotation='vertical')
plt.margins(0.2)
plt.subplots_adjust(bottom=0.15)
plt.show()

enter image description here

解决方法

不确定是否有自动执行此操作的方法,但您可以为每个刻度“手动”执行此操作:

import matplotlib.pyplot as plt

x = [1,2,3,4]
y = [1,4,9,6]
labels = ['Frogs','Hogs','Bogs','Slogs']

plt.plot(x,y)
plt.xticks(x,labels,rotation='vertical')
plt.margins(0.2)
plt.subplots_adjust(bottom=0.15)
plt.show()

# getting the labels from the axis
ticks_labels = plt.gca().get_xticklabels()

# rotating first and third ticks
ticks_labels[0].set_rotation('horizontal')
ticks_labels[2].set_rotation('horizontal')

Graph with the horizontal labels

ticks_labels 列表中的每一项都是一个 Text artist,因此您几乎可以更改所有内容(大小、颜色、位置等)

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。