将X轴标签居中放置在p行中

2024-09-29 21:40:43 发布

您现在位置:Python中文网/ 问答频道 /正文

我正在建立一个有两条线的线路图,一条用于高温,另一条用于低温。x轴基于日期时间格式(2014-01-01等)的一整年的天数。然而,我改变了标签,而不是数据,长达数月(一月、二月、三月等)。问题是第一个标签“Jan”在原产地。我想将所有标签向右移动,使它们在刻度之间居中。在

fig, ax = plt.subplots()

plt.plot(x, y1)
plt.plot(x, y2)

# Change x-axis from %y-%m-%d format to %m:
monthsFmt = mdates.DateFormatter('%m')
plt.gca().xaxis.set_major_formatter(monthsFmt)

# Replace numeric x-axis labels (1,2,3, ..., 12) for abbreviations of months ('Jan', 'Feb', 'Mar', etc.):
labels = ['Jan', 'Feb', 'Mar', 'Apr', 'May', 'Jun', 'Jul', 'Aug', 'Sep', 'Oct', 'Nov', 'Dec']
ax.set_xticklabels(labels)

# CODE GOES HERE TO CENTER X-AXIS LABELS...

# Render plot:
plt.show()

这是我要寻找的结果:

enter image description here


Tags: labelsplot低温plt标签axmarjan
3条回答

在绘图时直接使用日期通常是有意义的。因此,可以使用matplotlib.dates定位器来代替手动设置标签的固定位置。如果要在月中定位蜱虫,可以选择该月的15日

matplotlib.dates.MonthLocator(bymonthday=15)

一个完整的例子:

^{pr2}$

enter image description here

使用DavidG发布的帖子中建议的小记号应该可以。下面是一个MWE,我根据您的具体问题进行了调整,强制在每个月的第一天显示主要刻度,并使用小刻度将标签放在主要刻度之间:

import matplotlib as mpl
import matplotlib.pyplot as plt
import numpy as np
import datetime

# Generate some data for example :

yr = 2014
fig, ax = plt.subplots()

x0 = datetime.datetime(yr, 1, 1)
x = np.array([x0 + datetime.timedelta(days=i) for i in range(365)])

y1 = np.sin(2*np.pi*np.arange(365)/365) + np.random.rand(365)/5
y2 = np.sin(2*np.pi*np.arange(365)/365) + np.random.rand(365)/5 - 1

# Draw high and low temperatures lines :

ax.plot(x, y1, color='#c83c34')
ax.plot(x, y2, color='#28659c')
ax.fill_between(x, y2, y1, facecolor='#daecfd', alpha=0.5)

# Force the major ticks position on the first of each month and hide labels:

xticks = [datetime.datetime(yr, i+1, 1) for i in range(12)]
xticks.append(datetime.datetime(yr+1, 1, 1))
ax.set_xticks(xticks)
ax.tick_params(axis='both', direction='out', top=False, right=False)
ax.axis([xticks[0], xticks[-1], -2.5, 1.5])
ax.set_xticklabels([])

# CODE GOES HERE TO CENTER X-AXIS LABELS...

labels = ['Jan', 'Feb', 'Mar', 'Apr', 'May', 'Jun',
          'Jul', 'Aug', 'Sep', 'Oct', 'Nov', 'Dec']
mticks = ax.get_xticks()
ax.set_xticks((mticks[:-1]+mticks[1:])/2, minor=True)
ax.tick_params(axis='x', which='minor', length=0)
ax.set_xticklabels(labels, minor=True)
fig.tight_layout()

plt.show()

结果是: enter image description here

差不多,Jean-Sébastien。除了上个月,一切都很完美。它没有出现。在

你的解决方案做了一些调整:

fig, ax = plt.subplots()

# Draw high and low temperatures lines:
plt.plot(x, y1, color = '#c83c34')
plt.plot(x, y2, color = '#28659c')

# Fill area between lines:
plt.gca().fill_between(x,
                       y2, y1,
                       facecolor='#daecfd',
                       alpha=0.5)

# Force major ticks on a monthly time scale only:
locator = mpl.dates.MonthLocator()
ax.xaxis.set_major_locator(locator)

# Hide major labels and set axis limits:
ax.set_xticklabels([])
ax.tick_params(axis='both', direction='out')
ax.axis(xmin=datetime.datetime(2014, 1, 1),
        xmax=datetime.datetime(2014, 12, 31),
        ymin=-50, ymax=50)

labels = ['Jan', 'Feb', 'Mar', 'Apr', 'May', 'Jun',
          'Jul', 'Aug', 'Sep', 'Oct', 'Nov', 'Dec']

mticks = ax.get_xticks()
ax.set_xticks((mticks[:-1]+mticks[1:])/2, minor=True)
ax.tick_params(axis='x', which='minor', length=0)
ax.set_xticklabels(labels, minor=True)

plt.show()

有一个情节是这样的: enter image description here

有什么线索可以让十二月加入x轴标签吗?总之,非常感谢。在

相关问题 更多 >

    热门问题