Matplotlib x轴在顶部,没有x轴标签,没有大刻度线标签,但是想要外部大,小刻度线

时间:2019-07-18 14:57:19

标签: python matplotlib seaborn

我想绘制一个x轴较高且不带标签但包含主要和次要刻度线的图。

import pandas as pd
import numpy as np
import matplotlib.pyplot as plt
import matplotlib as mpl
import seaborn as sns

perf = np.cumprod(1+np.random.normal(size=250)/100)
df = pd.DataFrame({'underwater': perf/np.maximum.accumulate(perf)-1})


sns.set_context('notebook')
sns.set_style('ticks', rc = {'axes.grid': True, 'axes.spines.left': True, 'axes.spines.right': False, 'axes.spines.bottom': True, 'axes.spines.top': False})

ax = plt.gca()
df.plot.area(ax=ax, label = 'underwater')
ax.xaxis.tick_top()
ax.xaxis.set_ticklabels([])
sns.despine(left=False, bottom=True, right=True, top = False, ax=ax)

enter image description here 我错过了顶部的主要(外部)x轴刻度。

1 个答案:

答案 0 :(得分:0)

将最后一行添加到您的代码中。此外,您不需要tick_top()

ax = plt.gca()
df.plot.area(ax=ax, label = 'underwater')
# ax.xaxis.tick_top() # <----- This line is not needed
ax.xaxis.set_ticklabels([])
sns.despine(left=False, bottom=True, right=True, top = False, ax=ax)

# Add the following line to your code
ax.tick_params('x', length=8, width=2, color='red',  which='major', direction='out')

enter image description here

相关问题