質問をすることでしか得られない、回答やアドバイスがある。

15分調べてもわからないことは、質問しよう!

新規登録して質問してみよう
ただいま回答率
85.48%
Matplotlib

MatplotlibはPythonのおよび、NumPy用のグラフ描画ライブラリです。多くの場合、IPythonと連携して使われます。

Python 3.x

Python 3はPythonプログラミング言語の最新バージョンであり、2008年12月3日にリリースされました。

Q&A

解決済

2回答

26234閲覧

各凡例ラベルを1つのみ表示させたい

bkts94441

総合スコア18

Matplotlib

MatplotlibはPythonのおよび、NumPy用のグラフ描画ライブラリです。多くの場合、IPythonと連携して使われます。

Python 3.x

Python 3はPythonプログラミング言語の最新バージョンであり、2008年12月3日にリリースされました。

0グッド

0クリップ

投稿2018/05/22 02:17

前提・実現したいこと

各motion3個の線を表示させつつ(計10個の線)、凡例ラベルの同名で表示されているものを1つにまとめたい

発生している問題・エラーメッセージ

こちらの画像の凡例ラベルの[motion0],[motion2],[motion4]を一つだけ表示させたいのですが、以下の様に表示されてしまいます。同色の線を描画しつつ、同名のmotionを1つのみ表示させるには、どの箇所に変更点を加えれば良いかわからず、質問させていただきました。どうか、ご教示よろしくお願い致します。
イメージ説明

該当のソースコード

長く見づらくて申し訳ありませんが、ご覧いただけると幸いです。よろしくお願い致します。

#plot_Duplication.py import pandas as pd import matplotlib.pyplot as plt import os #####RuntimeWarning エラー回避 import numpy as np np.seterr(divide='ignore', invalid='ignore') ##### #出力処理 def Output(): motions = ["0","2","4"] speedes02 = ["0","1","2"] speedes4 = ["1","2","3","4"] TF = ["Time","Freq"] tp_list = ["pickup","torque"] Time_features = ["std","cf"] Freq_features = ["Rotation2_section2"] Date = input("日付入力:") mkdir = "//../Date" read_path = "//../filedata" os.chdir(read_path) for sample in samples: for tf in TF: for tp in tp_list: if tf in "Time": for feature in Time_features: plt.figure(figsize=(Xsize,Ysize)) plt.grid() title_name = "Duplicated_"+feature for motion in motions: if motion in "4": for speed in speedes4: df = pd.read_csv("Timedata.csv") df = df.query("motion_type in ["+motion+"] & speed_setting in ["+speed+"]") Time_feature_plot(df,feature,tf,tp,title_name,motion,speed) save=tp+"_"+sample+"_"+feature+"_duplicated.png" else: for speed in speedes02: df = pd.read_csv("Timedata.csv") df = df.query("motion_type in ["+motion+"] & speed_setting in ["+speed+"]") Time_feature_plot(df,feature,tf,tp,title_name,motion,speed) save=tp+"_"+sample+"_"+feature+"_duplicated.png" graph_label(title_name,feature) plt.savefig(mkdir+"/"+save) elif tf in "Freq": for feature in Freq_features: plt.figure(figsize=(Xsize,Ysize)) plt.grid() title_name = "Duplicated_"+feature for motion in motions: if motion in "4": for speed in speedes4: df = pd.read_csv("Freqdata.csv") df = df.query("motion_type in ["+motion+"] & speed_setting in ["+speed+"]") Freq_feature_plot(df,feature,tf,tp,title_name,motion,speed) save=tp+"_"+sample+"_"+feature+"_duplicated.png" else: for speed in speedes02: df = pd.read_csv("Freqdata.csv") df = df.query("motion_type in ["+motion+"] & speed_setting in ["+speed+"]") Freq_feature_plot(df,feature,tf,tp,title_name,motion,speed) save=tp+"_"+sample+"_"+feature+"_duplicated.png" graph_label(title_name,feature) plt.savefig(mkdir+"/"+save) #Timefeature def Time_feature_plot(df,feature,tf,tp,title_name,motion,speed): ot = df["operating_time"] fp = df[feature+"_section2_"+tp] if motion in "0": plt.plot(ot,fp,linestyle="solid",label=motion+"motion",color="red") if motion in "2": plt.plot(ot,fp,linestyle="solid",label=motion+"motion",color="blue") if motion in "4": plt.plot(ot,fp,linestyle="solid",label=motion+"motion",color="green") #Freqfeature def Freq_feature_plot(df,feature,tf,tp,title_name,motion,speed): ot = df["operating_time"] fp = df[feature] if motion in "0": plt.plot(ot,fp,linestyle="solid",label="motion"+motion,color="red") if motion in "2": plt.plot(ot,fp,linestyle="solid",label="motion"+motion,color="blue") if motion in "4": plt.plot(ot,fp,linestyle="solid",label="motion"+motion,color="green") if __name__ == "__main__": #グラフサイズ手動設定 """ Xsize = int(input("グラフサイズX軸...")) Ysize = int(input("グラフサイズY軸...")) plt.rcParams["font.size"] = int(input("ラベルサイズ...")) """ #グラフサイズ自動設定 Xsize = 10 Ysize = 10 plt.rcParams["font.size"] = 16 plt.rcParams["xtick.labelsize"] plt.rcParams["ytick.labelsize"] HDnumber = [] samples = [] #読み込むcsvファイル some_csv = int(input("読み込み数...")) for csv_cnt in range(some_csv): HDnumber = input("読み込みデータ番号:") samples.append(HDnumber) Output()

気になる質問をクリップする

クリップした質問は、後からいつでもMYページで確認できます。

またクリップした質問に回答があった際、通知やメールを受け取ることができます。

バッドをするには、ログインかつ

こちらの条件を満たす必要があります。

guest

回答2

0

提示コードは確認していませんが、一部の凡例のみを表示する例です。
参考:
matplotlibでの凡例(ラベル)の表示場所・形式を変更する
matplotlib.pyplot.legend

Python

1import numpy as np 2import matplotlib.pyplot as plt 3 4xs = np.arange(10) 5handles = [] # A list of Artists (lines) 6labels = ['red','green','blue'] # 簡単のため色=ラベル名とする 7for i in range(6): 8 ys = (xs ** 2) / (i+1) 9 line, = plt.plot( xs, ys, linestyle="solid", label='line{}'.format(i),color=labels[i//2]) 10 if i % 2 == 0: # 同色をまとめる 11 handles.append(line) 12#plt.legend() # そのまま 13plt.legend( handles, labels) # 色でまとめる 14plt.show()

そのまま
イメージ説明

色でまとめる
イメージ説明

投稿2018/05/22 04:23

can110

総合スコア38262

バッドをするには、ログインかつ

こちらの条件を満たす必要があります。

bkts94441

2018/05/22 08:09

can110様 ご回答いただき、ありがとうございます。 今後使用する可能性がありますので、ご参考にさせていただきます。
guest

0

自己解決

ご覧いただいた皆様、申し訳ありません。
グラフ自体の仕様変更することに決定しましたので解決とさせていただきます。

投稿2018/05/22 04:24

bkts94441

総合スコア18

バッドをするには、ログインかつ

こちらの条件を満たす必要があります。

あなたの回答

tips

太字

斜体

打ち消し線

見出し

引用テキストの挿入

コードの挿入

リンクの挿入

リストの挿入

番号リストの挿入

表の挿入

水平線の挿入

プレビュー

15分調べてもわからないことは
teratailで質問しよう!

ただいまの回答率
85.48%

質問をまとめることで
思考を整理して素早く解決

テンプレート機能で
簡単に質問をまとめる

質問する

関連した質問