Reputation: 441
I want to remove the y axis of my matplotlib plot (maybe via pandas DataFrame.plot() method) so that it is invisible and change the x-axis to be a dotted line. The closest I've seen to this is the pyplot.box(False) statement, however it doesn't let me select just the y-axis and I still can't how to edit the x-axis as described. How do I go about doing this?
Upvotes: 3
Views: 2036
Reputation: 39052
Here is one way to do it. I am choosing a sample DataFrame to plot. The trick is to hide the left, right and top axis' spine and turn the lower x-axis into dashed line using the method suggested here by @ImportanceOfBeingEarnest
import pandas as pd
import matplotlib.pyplot as plt
fig, ax = plt.subplots()
df = pd.DataFrame({'pig': [20, 18, 489, 675, 1776],
'horse': [4, 25, 281, 600, 1900]
}, index=[1990, 1997, 2003, 2009, 2014])
ax_ = df.plot.line(ax=ax)
for spine in ['right', 'top', 'left']:
ax_.spines[spine].set_visible(False)
ax_.spines['bottom'].set_linestyle((0,(8,5)))
plt.yticks([])
plt.show()
Upvotes: 3