Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Editing axis lines in matplotlib

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?

like image 282
Alesi Rowland Avatar asked Jun 22 '26 14:06

Alesi Rowland


1 Answers

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()

enter image description here

like image 54
Sheldore Avatar answered Jun 25 '26 02:06

Sheldore



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!