[Python] matplotlib: Format the diagram for your dissertation

Here is a summary of the commands used when creating diagrams for science papers with matplotlib's pyplot. I used it mainly as a diagram for physical papers such as Physical review, but it can probably be used in other fields as well. Assuming a two-column paper, The width of the figure is 8 cm for one column and 16 cm for two columns.

Module import

import matplotlib.pyplot as plt

I used to import pylab, but pylab seems to be taking some of the numpy and matplotlib functions. The above is used because numpy is imported separately.

Overall settings: rcParams

You can use rcParams in your code to change diagram parameters in bulk without having to modify each diagram.

plt.rcParams['font.family'] ='sans-serif'#Font to use
plt.rcParams['xtick.direction'] = 'in'#x-axis scale line pointing inward('in')Or outward('out')Or bidirectional('inout')
plt.rcParams['ytick.direction'] = 'in'#y-axis scale line pointing inward('in')Or outward('out')Or bidirectional('inout')
plt.rcParams['xtick.major.width'] = 1.0#Line width of x-axis main scale line
plt.rcParams['ytick.major.width'] = 1.0#Line width of y-axis main scale line
plt.rcParams['font.size'] = 8 #Font size
plt.rcParams['axes.linewidth'] = 1.0#Axis line width edge linewidth. Enclosure thickness

The font'sans-serif'is a character without a "whisker", and the characters usually used in the figures of papers are often without a beard. The font size is 8 because it is about the same size as the caption description.

Settings for each figure

--Figure size setting

plt.figure(figsize=(3.14,3.14))

The size of the figure is specified in inches and the variables are (width, height). 3.14 inches is about 8 cm. It is good to adjust the height without changing the width.

--Specify the number of digits of the numerical value of the axis:

plt.gca().yaxis.set_major_formatter(plt.FormatStrFormatter('%.3f'))#Display of 3 digits after the y-axis decimal point

--Express the numbers on the axis without using offsets (such as + 1.05e9)

plt.gca().xaxis.get_major_formatter().set_useOffset(False)

--Make the numbers on the axis an integer

plt.gca().yaxis.set_major_locator(ticker.MaxNLocator(integer=True))

--Specify the number of axis scales:

plt.locator_params(axis='y',nbins=6)#y-axis, within 6 pieces.

--Specify the orientation of the axis scale and the position of the frame:

plt.gca().yaxis.set_tick_params(which='both', direction='in',bottom=True, top=True, left=True, right=True)

--Use the following at the end of the command in the figure:

plt.tight_layout()#The graphs do not overlap and fit within the set size of the figure.
plt.savefig('figname.pdf', transparent=True)
plt.savefig('figname.png', transparent=True, dpi=300)

The format of the figure changes by changing the extension. By specifying transparent, the background becomes transparent. dpi is the dot density of the bitmap image. After saving the figure, specify it to the output size such as width 8 cm with TeX or word and use it.

--Color map

plt.contour(X, Y, Z, cmap='viridis') #Draw contour lines with viridis color scheme

For the color scheme, we recommend viridis, which is easy to distinguish even for people with color vision deficiency.

[Memo] We will update it from time to time.

References

Recommended Posts

[Python] matplotlib: Format the diagram for your dissertation
Code for checking the operation of Python Matplotlib
[Python 2/3] Parse the format string
[python] How to use the library Matplotlib for drawing graphs
See python for the first time
What is the python underscore (_) for?
The first step in Python Matplotlib
Command for the current directory Python
Introducing the BOT framework Minette for Python
[Python] Set the graph range with matplotlib
Use DeepL with python (for dissertation translation)
Python Master RTA for the time being
Add 95% confidence intervals on both sides to the diagram with Python / Matplotlib
Launch the Discord Python bot for 24 hours.
MongoDB for the first time in Python
Pandas of the beginner, by the beginner, for the beginner [Python]
The road to updating Splunkbase with your own Splunk app for Python v2 / v3
Get coordinate values and keyboard input values by clicking on the python / matplotlib diagram
CERTIFICATE_VERIFY_FAILED in Python 3.6, the official installer for macOS
[Python] I tried substituting the function name for the function name
The story of low learning costs for Python
Created a Python wrapper for the Qiita API
vprof --I tried using the profiler for Python
[Python] How to change the date format (display format)
Wagtail is the best CMS for Python! (Perhaps)
Upgrade the Azure Machine Learning SDK for Python
Make a histogram for the time being (matplotlib)
Use logger with Python for the time being
I tried python programming for the first time.
Tips for hitting the ATND API in Python
Python: Prepare a serializer for the class instance:
[Python] I searched for the longest Pokemon Shiritori
Image processing? The story of starting Python for
Note for formatting numbers with python format function
Don't use readlines () in your Python for statement!
[Python] Predict the appropriate rent for an apartment
Call Polly from the AWS SDK for Python
String to Unicode Escape Sequence Format for Python
python: Use your own class for numpy ndarray
2016-10-30 else for Python3> for:
python [for myself]
Python string format
Get an Access Token for your service account with the Firebase Admin Python SDK
#Python basics (#matplotlib)
My matplotlib (python)
Python string format
format in python
Create your own Big Data in Python for validation
What I got into Python for the first time
Make your python CUI application an app for mac
I tried Python on Mac for the first time.
Run with CentOS7 + Apache2.4 + Python3.6 for the time being
[Python] Measures and displays the time required for processing
Steps to install the latest Python on your Mac
[Python] 2 Create a risk-return map for your asset portfolio
HoloViews may become the standard for Python visualization tools
I tried python on heroku for the first time
Get the result in dict format with Python psycopg2
Use the CASA Toolkit in your own Python environment
Information for controlling the motor with Python on RaspberryPi
Electron is the best solution for Python multi-platform development