简体   繁体   English

matplotlib - 如何将次要网格线的数量设置为 6 而不是 4?

[英]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.

相关问题 使用matplotlib的主要和次要网格线和刻度线 - Major and minor grid lines and ticks using matplotlib 如何独立设置一个情节的水平和垂直,主要和次要网格线? - How to independently set horizontal and vertical, major and minor grid lines of a plot? 设置 matplotlib 颜色条中的次要刻度数 - Set the number of minor ticks in matplotlib colorbar 如何在matplotlib中设置次要刻度的位置 - How can i set the location of minor ticks in matplotlib 无法在matplotlib图中显示较小的网格线 - Cannot get minor grid lines to appear in matplotlib figure matplotlib。 如何在子图之间切换,而不是从头开始重新绘制? - matplotlib. How do I switch between subplots, rather than replotting them from scratch? 如何绘制直方图的密度而非计数? (Matplotlib) - How do I plot my histogram for density rather than count? (Matplotlib) 如何告诉OSX使用brew中的matplotlib,而不是默认值? - How do I tell OSX to use matplotlib from brew, rather than default? 如何优化python代码以读取一批多行而不是一次读取一行? - How do I optimize the python code to read a batch of multiple lines rather than one line at a time? 在 python 中,如何打印不包含某个字符串的行,而不是打印包含某个字符串的行: - In python, how can I print lines that do NOT contain a certain string, rather than print lines which DO contain a certain string:
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM