[PYTHON] Create a graph with borders removed with matplotlib

How to make a graph like this with matplotlib.

スクリーンショット 2017-01-30 21.17.49.jpg

Import 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 normally

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. スクリーンショット 2017-01-30 21.08.22.jpg

Erase the right and top borders

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()
スクリーンショット 2017-01-30 21.10.39.jpg

Turn off the right and top scales

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()
スクリーンショット 2017-01-30 21.17.49.jpg

By default, the border is erased

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

Create a graph with borders removed with matplotlib
Draw a loose graph with matplotlib
Band graph with matplotlib
Draw a flat surface with a matplotlib 3d graph
Draw a graph with matplotlib from a csv file
Graph Excel data with matplotlib (1)
Draw a graph with NetworkX
[Python] How to draw a line graph with Matplotlib
Create graph with plotly button
Create a homepage with django
Graph drawing method with matplotlib
Graph Excel data with matplotlib (2)
Create a heatmap with pyqtgraph
I want to manually create a legend with matplotlib
Draw a graph with networkx
Draw a graph with Julia + PyQtGraph (2)
Create a virtual environment with Python!
Draw a graph with Julia + PyQtGraph (1)
Draw a graph with Julia + PyQtGraph (3)
Let's make a graph with python! !!
Create a poisson stepper with numpy.random
Make a nice graph with plotly
Draw a graph with PySimple GUI
Write a stacked histogram with matplotlib
Create a file uploader with Django
Create a poster with matplotlib to visualize multiplication tables that remember multiplication
I want to create a graph with wavy lines omitted in the middle with matplotlib (I want to manipulate the impression)
Create a Python function decorator with Class
[Python] Set the graph range with matplotlib
Build a blockchain with Python ① Create a class
[PyQt] Display a multi-axis graph with QtChart
How to draw a graph using Matplotlib
Create a dummy image with Python + PIL.
Try drawing a normal distribution with matplotlib
[Python] Create a virtual environment with Anaconda
Let's create a free group with Python
Create a GUI app with Python's Tkinter
Create a large text file with shellscript
Create a star system with Blender 2.80 script
Create a virtual environment with Python_Mac version
Create a simple web app with flask
Graph trigonometric functions with numpy and matplotlib
Create a Connecting Nearest Neighbor with NetworkX
Create a web service with Docker + Flask
Create a private repository with AWS CodeArtifact
Create a car meter with raspberry pi
Create a devilish picture with Blender scripts
Create a matrix with PythonGUI (text box)
Write a nice pie chart with matplotlib
Draw a graph with PyQtGraph Part 1-Drawing
(Matplotlib) I want to draw a graph with a size specified in pixels
How to draw a bar graph that summarizes multiple series with matplotlib
Create a Python3 environment with pyenv on Mac and display a NetworkX graph
Create a frame with transparent background with tkinter [Python]
How to draw a 2-axis graph with pyplot
[Python] limit axis of 3D graph with Matplotlib
Read Python csv data with Pandas ⇒ Graph with Matplotlib
A memo that made a graph animated with plotly
Create a LINE BOT with Minette for Python
Draw a graph with PyQtGraph Part 3-PlotWidget settings
Draw a graph by processing with Pandas groupby