Visualization memo by Python

Purpose

Read and analyze binary files output in Fortran 90 with Python. Describe links and cautions that were helpful in development. It will be edited sequentially in the future.

The person who writes

Usually Fortran 90 and IDL (Interactive Data Language). Python is not very familiar. Migrating to Python instead of licensed IDL.

Personal development policy

--Pylab is hard to get caught in Google search. Import numpy and matplotlib directly. --from [package] import * is not done as much as possible. --I want to use it on the server, so basically I use it on the console.

environment

Python 3.6.0 |Anacoda 4.3.0 on OS X El Capitan

Python in general

Naming convention

Follow PEP8. Understanding [Python Coding Standards] PEP8 It is recommended that the variable name / function name is snake_case. Fortran is case insensitive, so it looks good when you have the same kind of naming convention. Summary of English words often used in programming [Updated from time to time] Foreigners talk: how to name classes and methods in English Reference information for successful method naming

Module information

How to display the version of numpy or scipy Python Tips: I want to find out the location of library modules

variable

[Python pass by value and pass by reference] (https://python.dogrow.net/?p=274) [Passing by value, passing by reference, passing by reference] (https://qiita.com/ur_kinsk/items/949dabe975bdc1affb82) Is it okay for the default to pass the reference value? [Determine if variable is defined in Python] (https://stackoverflow.com/questions/1592565/determine-if-variable-is-defined-in-python) [Four arithmetic operations-numerical values-Introduction to Python] (http://www.pythonweb.jp/tutorial/num/index2.html)

loop

Avoid multiple loops. itertools.product Avoid multiple loops in Python

System call

17.5. Subprocess — Subprocess Management

sort

[Sort HOW TO] (https://docs.python.jp/3/howto/sorting.html) [Get index of array after sorting with numpy] (https://python.dogrow.net/?p=274)

Input / output

Fortran output files are unformatted output (data) and formatted output (parameters, etc.).

Unformatted output

The scipy.io.FortranFile was useful when reading Fortran unformatted output. The contents are composed of numpy.fromfile etc. scipy.io.FortranFile However, as officially noted, it outputs with direct access or stream in Fortran. You don't have to worry about endianness if you read it with numpy.fromfile.

Formatted output

Read normally. Use str.split, str.splitlines, etc. Pay attention to the newline character at the end of the line. [4.7. Text sequence type — str] (http://docs.python.jp/3.6/library/stdtypes.html#text-sequence-type-str)

NumPy

Array element specification

When specifying in a range, it is different from IDL and it seems to be wrong.

import numpy as np
np.arange(0,10)
# array([0, 1, 2, 3, 4, 5, 6, 7, 8, 9])
np.arange(0,10)[3]
# 3
np.arange(0,10)[0:3]
# array([0, 1, 2])When specified in a range, the rightmost element is not included.

General

Array shape conversion Reshape looks good.

Array generation related routine

List of array index related functions

List of math functions

List of logical operation related functions

Difference calculation

Since it is basically an analysis, it is implemented obediently if it is not too slow. 2. Tips for Numpy array numpy.roll

If you want to implement various things in Python, speed up Numba etc. Numba

matplotlib Basically, pyplot is enough.

tutorial

Pyplot tutorial

Introduction to matplotlib

Anatomy of Matplotlib -- tutorial developed for the SciPy conference This seems to be the most understandable. I want to read it properly.

Global settings: rcParams

The equivalent of IDL's System Variable (! P. ~,! X. ~, Etc.) is like rcParams. As the equivalent of the startup file (.idl_startup), matplotlibrc should be set. The location is below.

import matplotlib as mpl
mpl.matplotlib_fname()

For the time being, set the following.

backend      : TkAgg  #The drawing window of matplotlib is treated as a separate application.
interactive  : True  # show()Display the plot without writing each one.
xtick.top            : True  # draw ticks on the top side
ytick.right          : True  # draw ticks on the right side
xtick.direction      : in    # direction: in, out, or inout
ytick.direction      : in    # direction: in, out, or inout
xtick.minor.visible  : True  # visibility of minor ticks on x-axis
ytick.minor.visible  : True  # visibility of minor ticks on y-axis

When creating a diagram as a batch job, enter the following settings.

import matplotlib.pyplot as plt
plt.rcParams['interactive'] = False

The modified rcParams can be returned with rc_file_defaults ().

import matplotlib as mpl
mpl.rc_file_defaults()

rcParams can also be reset to defaults with matplotlib.rcdefaults (), Note that this will also return the settings changed in matplotlibrc. rc_file_defaults () will make matplotlibrc applied from the default value.

General appearance

It seems good to write the settings you always want to use in matplotlibrc.

Format the graph of matplotlib

matplotlib: Format the figure for your dissertation

Plot black and white graphs for papers with matplotlib or pylab

The appearance of the graph is also decided by the style sheet.

Changes in matplotlib 2.0 There are a lot of figures and it is helpful.

4 techniques for making diagrams for papers and presentation materials with matplotlib I don't use it much, but bar charts and legends.

1D data

Try all the markers that can be used with matplotlib-python

[How to do a scatter plot with empty circles in Python?] (https://stackoverflow.com/questions/4143502/how-to-do-a-scatter-plot-with-empty-circles-in-python)

Log scale display / grid display

2D data

pcolor is slow as the data gets bigger. Use pcolor mesh.

matplotlib.pyplot.pcolormesh

matplotlib.pyplot.colorbar

Plot of contour lines and other things with matplotlib

There seems to be something called pcolorfast.

Color

Colormap Reference Colormap Normalization

font

How to change fonts in matplotlib (python)?

Placement

Increase the font size of the graph with matplotlib Tight Layout guide [What to do if you get a tight_layout error on Mac OS X](http://myenigma.hatenablog.com/entry/2015/10/09/223629 # What to do if you get a tight_layout error on Mac-OSX] )

output

matplotlib.figure.Figure.savefig Save high resolution images of Matplotlib

window

How do I close all pyplot windows (including ones from previous script executions)?

Differences between pyplot, figure and axes

stack overflow: Understanding matplotlib: plt, figure, ax(arr)? AnatomyOfMatplotlib-Part1-Figures_Subplots_and_layouts.ipynb Official description of the role of Pyplot and Axes Basic knowledge of matplotlib that I wanted to know early, or the story of an artist who can adjust the appearance

Apparently, originally, figure manages the window, and axes manages each plot in the window. pyplot seems to be a wrapper that makes it script-like and easy to use.

Recommended Posts

Visualization memo by Python
Python memo
python memo
Python memo
python memo
Python memo
Visualization memo by pandas, seaborn
Python memo
Python memo
Interval scheduling learning memo ~ by python ~
[Python] Memo dictionary
python beginner memo (9.2-10)
python beginner memo (9.1)
★ Memo ★ Python Iroha
[Python] EDA memo
Python 3 operator memo
[My memo] python
Python3 metaclass memo
[Python] Basemap memo
Python beginner memo (2)
[Python] Numpy memo
[Learning memo] Basics of class by python
Python & Machine Learning Study Memo ④: Machine Learning by Backpropagation
Primality test by Python
Python class (Python learning memo ⑦)
My python environment memo
python openCV installation (memo)
Python module (Python learning memo ④)
Communication processing by Python
Python Data Visualization Libraries
[Scientific / technical calculation by Python] Histogram, visualization, matplotlib
Python test package memo
[Python] Memo about functions
Various Python visualization tools
python regular expression memo
Binary search (python2.7) memo
[My memo] python -v / python -V
Python3 List / dictionary memo
[Memo] Python3 list sort
Python Tips (my memo)
[Python] Memo about errors
DynamoDB Script Memo (Python)
Python basic memo --Part 2
python recipe book Memo
Basic Python command memo
Python OpenCV tutorial memo
Beamformer response by python
Python basic grammar memo
TensorFlow API memo (Python)
A memo for creating a python environment by a beginner
python useful memo links
Python decorator operation memo
Python basic memo --Part 1
Effective Python Memo Item 3
Logistics visualization with Python
Divisor enumeration Python memo
[Keras] Personal memo to classify images by folder [Python]
Python learning memo for machine learning by Chainer from Chapter 2
Python learning memo for machine learning by Chainer Chapters 1 and 2
[Scientific / technical calculation by Python] Logarithmic graph, visualization, matplotlib
[Scientific / technical calculation by Python] Polar graph, visualization, matplotlib