[英]matplotlib - how do I set number of minor grid lines to 6 rather than 4?
I have a graph which shows the number of weeks along the x axis;我有一个图表,显示沿 x 轴的周数; how can I have 6 intermediate minor grid lines to reference the days of the week?我怎样才能有 6 条中间次要网格线来参考一周中的日子?
By default it seems to do 4 minor lines but how do I increase that?默认情况下,它似乎做了 4 次次要行,但我该如何增加呢?
Assuming every week starts on Monday, is it possible to label the minor lines with 'T', 'W', 'T', 'F', 'S', 'S'?假设每周从星期一开始,label 是否可以使用“T”、“W”、“T”、“F”、“S”、“S”的次要行?
from matplotlib import pyplot
import numpy as np
# x-axis values
weeks = np.arange(0,8)
# y-axis values
cm = np.flip(np.arange(94,102))
pyplot.minorticks_on()
pyplot.grid(which='major', linestyle='-', linewidth='1', color='red')
pyplot.ylabel("cm")
pyplot.xlabel("weeks")
pyplot.plot(weeks, cm, 'go')
pyplot.grid(True, 'both')
pyplot.show()
You can use a MultipleLocator
to position the ticks at multiples of 1/7
.您可以使用MultipleLocator
到 position 的刻度为1/7
的倍数。 And a formatter that sets the correct letter depending on the fraction of 7 used.还有一个格式化程序,它根据使用的 7 的分数设置正确的字母。
from matplotlib import pyplot as plt
from matplotlib.ticker import MultipleLocator
import numpy as np
weeks = np.arange(0, 8)
cm = np.flip(np.arange(94, 102))
fig, ax = plt.subplots()
ax.plot(weeks, cm, 'go')
ax.grid(which='major', linestyle='-', linewidth='1', color='red')
ax.set_ylabel("cm")
ax.set_xlabel("weeks")
ax.minorticks_on()
ax.xaxis.set_minor_locator(MultipleLocator(1 / 7))
ax.xaxis.set_minor_formatter(lambda x, pos: 'MTWTFSS'[int(round(x * 7)) % 7])
ax.tick_params(axis='x', which='minor', labelsize=5)
ax.grid(True, which='both')
plt.show()
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.