LoginSignup
2
2

More than 5 years have passed since last update.

MATLAB - boxplotの箱がPDF書き出しするとちゃんと閉じていないバグ

Posted at

現象

以下の例のようにboxplot()を使って箱ひげ図を書きます。

load carsmall

boxplot(MPG,Origin)
title('Miles per Gallon by Vehicle Origin')
xlabel('Country of Origin')
ylabel('Miles per Gallon (MPG)')

gr = allchild(gca)

parts = allchild(gr)

set(findobj(parts,'Type','line'),'LineWidth',1,'Color','k')


print(gcf,'foo.pdf','-dpdf')

このようにMATLABではちゃんと表示されますが.....

download.png

書き出したPDFでは、よく見ると箱の形が変です。四辺がちゃんと閉じていなくて、切れ目が見えてしまいます。これはIllustratorなどで開いてみても分かりますが、線が角のところで途切れています。

Image 003.png

Mathworksに問い合わせて得た解決法

matlabroot()で返されるインストール先に対して

'C:\Program Files\MATLAB\R2017a'

以下のフォルダに、プライベート関数のboxrenderer.mがあります。

C:\Program Files\MATLAB\R2017a\toolbox\stats\stats\private\boxrenderer.m

357-363行目は以下のようになっていますが、

if factorsOnXAxis==1
    x=factor;
    y=response;
else
    x=response;
    y=factor;
end

これを以下のように書き換えます。

if factorsOnXAxis == 1
    x=[factor; factor(2)];
    y=[response; response(2)];
else
    x=[response; response(2)];
    y=[factor; factor(2)];
end

ファイルのアクセス権の問題で、そのまま保存しようとするとエラーが出ます。以下は英語版Windows7の場合の回避法です。

  1. MATLAB editorでboxrenderer.mのタブをクリック
  2. Show in Explorerを選ぶ
  3. boxrenderer.mを右クリックして、Propertiesを表示。
  4. Security pane > Edit ボタンを押す
  5. 上の窓でUsersを選び、下の的で ModifyWrite にチェックを入れ、Applyを押す。
  6. MATLABでboxrenderer.mへの変更を保存する。
  7. 上記のアクセス権の変更を元に戻す。

ご覧のように、PDFで開いても箱の隅がちゃんと閉じて表示されます。

Image 001.png

まとめ

Mathworksで対応してくれましたので、おそらくR2017bの正式リリースで修正されると思いますが、それまでに論文を仕上げたい人もおられるでしょうから、回避法を公開しました。

2
2
2

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
2