Qiita Teams that are logged in
You are not logged in to any team

Log in to Qiita Team
Community
OrganizationAdvent CalendarQiitadon (β)
Service
Qiita JobsQiita ZineQiita Blog
8
Help us understand the problem. What is going on with this article?
@tg-git

matplotlib in julia:PyPlotメモ

More than 3 years have passed since last update.

juliaでPyPlot(matplotlib)を使った作図をするとき,細かな調整方法についての記載が少ない.
以下,デフォ設定+困った際になんとかしたメモ.

どうしようも無い時でもPyCallからのpyimportで大抵はなんとかなるはず.

参考URL:
https://gist.github.com/gizmaa/7214002#basicplots
http://nanyakan.blogspot.jp/2013/03/matplotlib.html

python=>julia
a.x="red"       =>      a[:x=>"red"] or a["x"=>"red"] 
a{x="red"}      =>      a[:x=>"red"] or a["x"=>"red"] 
True            =>      true
"A"             =>      'A'
ファイルに出力
savefig("./dat.pdf", transparent="True",bbox_inches="tight", pad_inches=0.1)

最初に定義
using PyCall
using PyPlot
#using JuMP
#using LsqFit

#defaultのフォントを設定
fs=9
rc("font",family ="Times New Roman",size=fs) 
rc("font",serif ="Times New Roman",size=fs) 
rc("text",usetex ="true") #TeXでのフォントが普通のになる

#defaultのfigureサイズを設定
cm=1/2.54
rc("figure",figsize =([8,5.5]cm), ) 

default値の変更
rc("figure",figsize =([8,5.5]cm), ) 
rc("lines",linewidth = 0.5)
rc("lines",markersize = 4)
フォルダ・ファイル
f_data=homedir()"/work/dat"
f_fig =homedir()"/work/fig"
f_desk=homedir()"/Desktop"

cd(f_data)
println(pwd())
図のサイズ
figure(figsize=(3,2),)
手書き風
rc("text",usetex ="false")
xkcd()
axhline(0, color="k")
axvline(0, color="k")
log_scale
xscale("log")
yscale("log")
プロットのオプション
plot(datx, daty , "ro-", 
    mec="none", mew=1, mfc="pink", mew=1,markersize=6,
    label="r=$r1_1 A")



legend(loc="best",frameon=0, 
        ncol=1, labelspacing=0.1,
        columnspacing=-0.5, numpoints=1, markerscale=1, 
        handlelength=1.5,fontsize=12,)
#       bbox_to_anchor=(0.83, 0.24))

# 1-line
#legend(loc="best",frameon=0, ncol=1, labelspacing=0.1, columnspacing=-0.5, numpoints=1, markerscale=1,handlelength=1.5,fontsize=fs,)
他のプロット
# ベクトル場
quiver(x,z, datx, datz, datz, color="tomato", pivot="mid",angles="xy",scale_units="xy",scale=0.8,)

cmap=get_cmap("Paired",10)
co(i)=pycall(cmap,PyAny,i)

# co(数字)で色を指定
x=(1:10)
for i=1:20
    n=i/30.0
    plot(x,x.^n,"o-",color=co(n),mfc="w",mec=co(n),mew=1.5)
end

テキスト
annotate( L"text", xy=(0.55,0.44), xycoords="axes fraction", bbox=Dict(:boxstyle=>"round",:ec=>"none", :fc=>"gray", :alpha=>0.2),)

矢印
annotate(L"text_c",
xy=[5.,0.00], xytext=[3.0,-0.05], xycoords="data", textcoords="data",
arrowprops=["arrowstyle"=>"-|>","fc"=>"k","ec"=>"k", "connectionstyle"=>"arc3,rad=0.1"] )

annotate(L"text_c",
xy=[7.5,0.06], xytext=[6.9,0.09], xycoords="data", textcoords="data", 
arrowprops=["arrowstyle"=>"-|>","fc"=>"k","ec"=>"k", "connectionstyle"=>"angle,angleA=0,angleB=90,rad=10",])

# ver. up(julia v0.5)で記法が変わった)

annotate(L"text_c",
xy=[0.5,0.20], xytext=[0.2,0.05], xycoords="data", textcoords="data",
arrowprops=Dict("arrowstyle"=>"-|>","fc"=>"k","ec"=>"k", "connectionstyle"=>"arc3,rad=0.1") )

annotate(L"text_c",
xy=[0.5,0.06], xytext=[0.9,0.09], xycoords="data", textcoords="data", 
arrowprops=Dict("arrowstyle"=>"-|>","fc"=>"k","ec"=>"k", "connectionstyle"=>"angle,angleA=0,angleB=90,rad=10",))
error_bar
errorbar(x,y, yerr=e,capsize=1.5,color="m")
plot(x,y,"m.-", label=L"-0.5")
色々定義
    ax = gca() # 現在の図を定義

    xform  = ax[:xaxis][:set_major_formatter]
    xmform = ax[:xaxis][:set_minor_formatter]
    yform  = ax[:yaxis][:set_major_formatter]
    ymform = ax[:yaxis][:set_minor_formatter]

    xloc  = ax[:xaxis][:set_major_locator]
    xmloc = ax[:xaxis][:set_minor_locator]
    yloc  = ax[:yaxis][:set_major_locator]
    ymloc = ax[:yaxis][:set_minor_locator]

    ftick = matplotlib[:ticker][:FormatStrFormatter]
    stick = matplotlib[:ticker][:ScalarFormatter]
    ltick = matplotlib[:ticker][:MultipleLocator]
上記の定義のもとでのフォーマットの変更
# 軸の数字表記
    yform(ftick("%1.1f"))


#軸の肩に指数をつける
    yform(stick(useMathText=true))
    ax[:ticklabel_format](style="sci",axis="y",scilimits=(0,0)) 
    ax[:yaxis][:offsetText][:set_fontsize](14)

#50ごとにsubticksをつける
ax[:xaxis][:set_minor_locator](ltick(50))


ticksの変更色々
ax[:tick_params](axis="y", direction="in", pad=1) # pad で軸との隙間を決める.
subplot
    subplot(4,3,1)
    subplots_adjust(hspace=0.2,wspace=0.1)    
    xticks((2:2:16),())
subplot2_違うサイズのsubp
# 縦に4分割 2:1:1
#sub1
subplot2grid((4,1), (0,0),  rowspan=2)

#sub2
#subplot2grid((6,2), (0,1), rowspan=2)
subplot2grid((4,1), (2,0),  colspan=1)

#sub3
subplot2grid((4,1), (3,0),  colspan=1)

x軸の数字のみを削除
xticks((2:2:16),())
拡大図
figure(figsize=(4,3),)

xx=[1:50]/5
plot(xx,xx.^2)
ax=gca()

using PyCall
zoom= pyimport("mpl_toolkits.axes_grid1.inset_locator")
ax=zoom[:inset_axes](ax, width=1, height=1., loc=2,
                     bbox_to_anchor=(0.18, 0.9),
                     bbox_transform=ax[:figure][:transFigure])
plot(xx,xx.^2,".")

ylim(0,20)
xlim(0,5)
xticks(fontsize=12)
yticks(fontsize=12)
ラベルの位置を変更
# タイトル
title("Mean Field",fontsize=12, x=0.5, y=0.8)

# yラベル
ax = gca()
ax[:yaxis][:set_label_coords](-0.15, -0.0)
関数のオプショナル引数
function plt(datx,daty; xa, xb, line="ro-")
 ###
end

plt(dx, dy ; line="ro-", xa=-7, xb=8 )
8
Help us understand the problem. What is going on with this article?
Why not register and get more from Qiita?
  1. We will deliver articles that match you
    By following users and tags, you can catch up information on technical fields that you are interested in as a whole
  2. you can read useful information later efficiently
    By "stocking" the articles you like, you can search right away
tg-git

Comments

No comments
Sign up for free and join this conversation.
Sign Up
If you already have a Qiita account Login
8
Help us understand the problem. What is going on with this article?