繁体   English   中英

Matplotlib 顶部的 x 轴,没有 x 轴标签,没有大刻度标签,但需要外部大刻度和小刻度

[英]Matplotlib x-axis at the top, no x axis label, no major tick labels, but outer major and minor tick wanted

我想绘制一个没有标签的上 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)

在此处输入图片说明 我错过了顶部的主要(外部)x 轴刻度。

将最后一行添加到您的代码中。 此外,您不需要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')

在此处输入图片说明

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM