2
4

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 5 years have passed since last update.

物理実験系研究者のJupyter cheatsheet

Last updated at Posted at 2019-07-28

#はじめに. ---最終更新 190801

  • 物理実験系の研究をしています.
  • Jupyterを実験・計算データ解析で使いはじめました.[190601~]
  • (これまでは主にExcelとIgorを使っていました.)
  • 一度使った分析手法(図示方法)を数ヶ月後とかにもう一度使うときがあります.
  • 忘れていることがよくあります.
  • 毎回コードを発掘するorググるのが面倒なのでcheat sheetにまとめます.
  • もっとこう書けばスッキリする等,アドバイスがあれば教えてもらえると嬉しいです.
  • 誰かの役に立てば幸いです.

#For data analysis

Import module

###### Fundamentals ######
import numpy as np
import pandas as pd
import math
import cmath
import random
import matplotlib.pyplot as plt
import seaborn as sns
%matplotlib inline

###### Figure setting ######
sns.set('talk', 'whitegrid', 'bright', font_scale=1.0,
        rc={"lines.linewidth": 2, 'grid.linestyle': '--', "xtick.major.size": 6, "ytick.major.size": 6})

###### Plotly ######
import plotly # only used in jupyter notebook
plotly.offline.init_notebook_mode(connected=False)

###### File operation ######
import glob
import re
import os
import time
import datetime

Figures

- Normal figure

fig = plt.figure(figsize=(6,4), dpi=80)
fig.subplots_adjust(left=0.2, wspace=0.6, bottom=0.15, hspace = 0.6)
fig.suptitle("")

ax1 = fig.add_subplot(1,1,1)
ax1.plot(df["xxx"], df["yyy"], "o-", color='black', linestyle='solid', linewidth=2)
ax1.axis('tight')
ax1.set_xlabel("x_label")
ax1.set_ylabel("$\mathrm{y_{label}}$")
ax1.set_ylim(1,1e5)
ax1.grid(True)

- Twin axis

ax2 = ax1.twinx()
ax2.plot(df["xxx"], df["yyy"], "o-", linewidth=2)
ax2.set_xlabel("x_label")
ax2.set_ylabel("y_label")
ax2.set_yscale('log')

- Plotly

-- Show fig

plotly.offline.iplot_mpl(fig)

- Heatmap

-- seaborn

ax = sns.heatmap(df)
ax.set(xlabel='x position', ylabel='y position')
--- Log color scale
from matplotlib.colors import LogNorm

log_norm = LogNorm(vmin=df.min().min(), vmax=df.max().max())
cbar_ticks = [math.pow(10, i) for i in range(math.floor(math.log10(df.min().min())), 1+math.ceil(math.log10(df.max().max())))]

ax = sns.heatmap(df,
                norm=log_norm,
                cbar_kws={"ticks": cbar_ticks}
                )
ax.set(xlabel='x position', ylabel='y position')

-- Plotly

fig = go.Figure(data=go.Heatmap(z=df.values.tolist()))

fig.update_layout(
    autosize=False,
    width=500,
    height=500,
    title = "Title",
    xaxis=dict(
        title="x_title",
        type='log',
        autorange=True,
        exponentformat='power'
    ),
    yaxis=dict(
        title="y_title"
        type='log',
        autorange=True,
        exponentformat='power'
    )
)

fig.update_yaxes(automargin=True)

fig.show()

3D plot

df = pd.read_csv("matrix_fill.csv", index_col=0)
x = np.array(df.columns, np.float32)
y = np.array(df.index, np.float32)
X,Y = np.meshgrid(x,y)
Z = df
fig = plt.figure()
ax = Axes3D(fig)

#ax.plot_surface(X, Y, Z, rstride=10, cstride=1, cmap='Reds', linewidth=5)
#ax.plot_surface(X, Y, Z, rstride=5, cstride=5)
ax.plot_surface(X, Y, Z, cmap='Reds')
#ax.plot_surface(X, Y, Z, rstride=5)
#ax.plot_surface(X, Y, Z, cstride=1, facecolors='Reds')
#ax.plot_surface(X, Y, Z)

ax.plot_wireframe(X,Y,Z, linewidth=1, color='b')

plt.show()

Data to images

for p in glob.iglob('*.dat'):
    name, ext = os.path.splitext(p)
    df = pd.read_table(p, header = 2)
    df.columns = ["xxx", "yyy"]
    plt.plot(df["xxx"], df["yyy"])
    plt.xlabel("x_label")
    plt.ylabel("y_label")
    plt.savefig("%s.png" % name, transparent=True, bbox_inches="tight")
    plt.close()

Import files

- Extract values

file_list = sorted([p for p in glob.glob('./*/*/**', recursive=True) if re.search('file.dat', p)])
for filename in file_list:
    f = open(filename, "r")
    data_list = f.read().split()
    try:
        a_list.append(data_list[0])
        b_list.append(data_list[1])
        c_list.append(data_list[2][3:])
        print("Success!")
        f.close()
    except:
        print("No such a index")
        a_list.append(np.nan)
        b_list.append(np.nan)
        c_list.append(np.nan)
        f.close()
#print("a:  ", a_list)
#print("b: ", b_list)
#print("c: ", c_list)

- Pandas

df = pd.DataFrame(data_list, dtype=np.float64)
df = pd.read_csv("./file.dat", sep='\t', header=None)

Constants

###### Constants ######
pi = math.pi
sqrt = math.sqrt
exp = cmath.exp
c0 = 2.99792458e8
q0 = 1.60217662e-19
E0 = 8.85418782e-12
m0 = 9.10938356e-31
h = 6.62607015e-34
hbar = 1.054571800e-34
n0 = 3.46

Others

Font check###

import matplotlib.font_manager
matplotlib.font_manager.findSystemFonts(fontpaths=None, fontext='ttf')
import matplotlib as mpl
mpl.matplotlib_fname()
2
4
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
2
4

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?