选择Matplotlib Xticks频率 [英] Choose matplotlib xticks frequency
问题描述
我正在使用字符串作为x标签来绘制数据.我想控制标签频率,以使文本轴不会过载.在下面的示例中,我只想每 3 个刻度看到一个标签:a-d-g-j.
我可以做到的一种方法是,每隔n个元素用2个空字符串替换my_xticks元素.但是我敢肯定,它提供了一种更清洁的方法.
代码如下:
将 numpy 导入为 np导入matplotlib.pyplot作为plty = np.array([4,4,4,5,5,6,5,5,4,4,4])x = np.arange(y.shape [0])my_xticks=['a','b','c','d','e','f','g','h','i','j','k']plt.xticks(x, my_xticks)plt.plot(x, y)
您快到了!以所需的频率采样xticks的第一和第二个参数!
该问题与
I am plotting data with strings as x labels. I'd like to control the label frequency to not overload the axis with text. In the example below i'd like to only see a label every 3 tick: a-d-g-j.
One way I could do that is to replace the my_xticks elements by 2 empty strings every n elements. But i'm sure it gives a cleaner way to do that.
Here is the code:
import numpy as np
import matplotlib.pyplot as plt
y=np.array([4,4,4,5,5,6,5,5,4,4,4])
x = np.arange(y.shape[0])
my_xticks=['a','b','c','d','e','f','g','h','i','j','k']
plt.xticks(x, my_xticks)
plt.plot(x, y)
You're almost there! Sample the first and second arguments of xticks at the desired frequency!
The question is a duplicate from Changing the "tick frequency" on x or y axis in matplotlib?
import numpy as np
import matplotlib.pyplot as plt
y = np.array([4,4,4,5,5,6,5,5,4,4,4])
x = np.arange(y.shape[0])
my_xticks = np.array(['a','b','c','d','e','f','g','h','i','j','k'])
frequency = 3
plt.plot(x, y)
plt.xticks(x[::frequency], my_xticks[::frequency])
这篇关于选择Matplotlib Xticks频率的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!