RubberDuckDebugging
RubberDuckDebugging

Reputation: 57

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

I want to plot a plot with a an upper x-axis without the labeling, but including the major and minor ticks.

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 I am missing the major (outside) x-axis ticks at the top.

Upvotes: 1

Views: 424

Answers (1)

Sheldore
Sheldore

Reputation: 39052

Add the last line to your code. Moreover, you do not need the tick_top() line

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

Upvotes: 1

Related Questions