How to make a graph like this with matplotlib.
First, import matplotlib with the name plt. The versions used are Python 3.5 and matplotlib 1.5.3. It should work with matplotlib 2.0.
Import matplotlib
import matplotlib.pyplot as plt
Plot for the time being
plt.plot([0,10,20,30,40,50], [10,20,30,5,25,30])
plt.show()
A graph with borders on the top, bottom, left, and right, which is the default design of matplotlib, is drawn.
You can manipulate borders by calling spines
fromplt.gca ()
. You can operate the corresponding borders with 'right'
, 'left'
,'top'
, and 'bottom'
. You can specify the border you want to operate and erase the border with set_visible (False)
. However, although the border disappeared, the scale did not disappear.
Erase the right and top borders
plt.plot([0,10,20,30,40,50], [10,20,30,5,25,30])
plt.gca().spines['right'].set_visible(False)
plt.gca().spines['top'].set_visible(False)
plt.show()
Similarly, from plt.gca ()
, only the scale specified by yaxis.set_ticks_position ()
is displayed. This time, I want to keep the left and bottom scales, so specify 'left'
and 'bottom'
.
Turn off the right and top scales
plt.plot([0,10,20,30,40,50], [10,20,30,5,25,30])
plt.gca().spines['right'].set_visible(False)
plt.gca().spines['top'].set_visible(False)
plt.gca().yaxis.set_ticks_position('left')
plt.gca().xaxis.set_ticks_position('bottom')
plt.show()
You can set a graph without borders as the default graph by playing with matplotlibrc. For mac, open the ~ / .matplotlib / matplotlibrc configuration file. If you installed Python with Homebrew, you can find it in /usr/local/lib/python3.6/site-packages/matplotlib/mpl-data/matplotlibrc.
Since there is such a part in matplotlibrc, delete # and replace True with False.
# axes.spines.left : True # display axis spines
# axes.spines.bottom : True
# axes.spines.top : True
# axes.spines.right : True
This time, I want to erase the upper and right borders, so delete the # in .top and .right and replace True with False.
# axes.spines.left : True # display axis spines
# axes.spines.bottom : True
axes.spines.top : False
axes.spines.right : False
This will output a graph with the right and top borders removed by default.
Recommended Posts