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

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

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

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

データ構造

データ構造とは、データの集まりをコンピュータの中で効果的に扱うために、一定の形式に系統立てて格納する形式を指します。(配列/連想配列/木構造など)

文字コード

文字コードとは、文字や記号をコンピュータ上で使用するために用いられるバイト表現を指します。

Python

Pythonは、コードの読みやすさが特徴的なプログラミング言語の1つです。 強い型付け、動的型付けに対応しており、後方互換性がないバージョン2系とバージョン3系が使用されています。 商用製品の開発にも無料で使用でき、OSだけでなく仮想環境にも対応。Unicodeによる文字列操作をサポートしているため、日本語処理も標準で可能です。

Q&A

解決済

1回答

976閲覧

pythonで折線を描きたい

samankoar

総合スコア7

Matplotlib

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

データ構造

データ構造とは、データの集まりをコンピュータの中で効果的に扱うために、一定の形式に系統立てて格納する形式を指します。(配列/連想配列/木構造など)

文字コード

文字コードとは、文字や記号をコンピュータ上で使用するために用いられるバイト表現を指します。

Python

Pythonは、コードの読みやすさが特徴的なプログラミング言語の1つです。 強い型付け、動的型付けに対応しており、後方互換性がないバージョン2系とバージョン3系が使用されています。 商用製品の開発にも無料で使用でき、OSだけでなく仮想環境にも対応。Unicodeによる文字列操作をサポートしているため、日本語処理も標準で可能です。

0グッド

1クリップ

投稿2020/05/25 15:19

編集2020/05/25 23:56

前提・実現したいこと

pythonで折れ線グラフを描きたいのですが、何故か2本の線が出てきます
■■特にエラーメッセージが出ていないが、正解であろう折線のもの意外にゼロのとこに水平線が出ています

データは以下のようです

international_couples_1965_2015.info() <class 'pandas.core.frame.DataFrame'> RangeIndex: 51 entries, 0 to 50 Data columns (total 9 columns): Year 51 non-null float64 Year_J 51 non-null float64 Total_marriages_couple 51 non-null float64 International_couple_Number 51 non-null float64 Japanese groom and foreign bride 51 non-null float64 Japanese bride and foreign groom 51 non-null float64 Total% 51 non-null float64 Japanese groom and foreign bride 51 non-null float64 Japanese bride and foreign groom 51 non-null float64 dtypes: float64(9) memory usage: 3.7 KB

該当のソースコード

fig = plt.figure() ax = fig.add_subplot() year=international_couples_1965_2015['Year'] ax.plot(international_couples_1965_2015['Year'], international_couples_1965_2015['Japanese groom and foreign bride'], label='husband:japanese', color='blue') ax.plot(international_couples_1965_2015['Year'], international_couples_1965_2015['Japanese bride and foreign groom'], label='husband:non-japanese', color='orange') ax.set_xlim(1960,2020,5) ax.set_xlabel('Year') ax.set_ylabel('Marriages Couple Number’) ax.legend() plt.show()

イメージ説明

よろしくお願いします。

修正ソースコード

fig = plt.figure(figsize=(10, 5)) ax1 = fig.add_subplot(1,2,1) ax2 = fig.add_subplot(1,2,2) year=international_couples_1965_2015['Year'] international_couples=international_couples_1965_2015['International_couple_Number'] japanese_couples=international_couples_1965_2015['Total_marriages_couple']-international_couples_1965_2015['International_couple_Number'] ax1.plot(year,international_couples,label='international_couples') ax1.plot(year,japanese_couples,label='japanese_couples') ax1.set_xlim(1960,2020,5) ax1.set_xlabel('Year') ax1.set_ylabel('Marriages Couple Number') ax1.legend() ax2.plot(year, international_couples_1965_2015['Japanese groom and foreign bride'], label='husband:japanese', color='blue') ax2.plot(international_couples_1965_2015['Year'], international_couples_1965_2015['Japanese bride and foreign groom'], label='husband:non-japanese', color='orange') ax2.set_xlim(1960,2020,5) ax2.set_xlabel('Year') ax2.set_ylabel('Marriages Couple Number') ![イメージ説明]![イメージ説明](b102fc303de44e352693c8c78c1c7b68.png) plt.subplots_adjust(wspace=0.6, hspace=0.4) plt.show() plt.tight_layout()

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

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

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

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

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

guest

回答1

0

ベストアンサー

subplotの書き方以外は大丈夫そうな気がしますが。
jupyter notebookとかで何回も同じコードで実施してると前のが記憶されているかもしれないので、カーネルリセットしてやってみてはいかがでしょうか。

python3

1import matplotlib.pyplot as plt 2import pandas as pd 3import numpy as np 4a = np.arange(33).reshape((11, 3)) 5international_couples_1965_2015 = pd.DataFrame(a) 6international_couples_1965_2015.columns = ['Year','Japanese groom and foreign bride','Japanese bride and foreign groom'] 7international_couples_1965_2015['Year'] = [1965+i*5 for i in range(11)] 8 9# ここまで疑似データ作成用 10 11fig = plt.figure() 12ax = fig.add_subplot(1,1,1) # subplotの引数を入れてなかったので、axがNoneになっていました。 13 14# 以後、そのままコピペ 15 16year=international_couples_1965_2015['Year'] 17 18ax.plot(international_couples_1965_2015['Year'], 19 international_couples_1965_2015['Japanese groom and foreign bride'], 20 label='husband:japanese', color='blue') 21ax.plot(international_couples_1965_2015['Year'], 22 international_couples_1965_2015['Japanese bride and foreign groom'], 23 label='husband:non-japanese', color='orange') 24 25 26ax.set_xlim(1960,2020,5) 27ax.set_xlabel('Year') 28ax.set_ylabel('Marriages Couple Number') 29ax.legend() 30plt.show()

sample

投稿2020/05/25 23:42

jeanbiego

総合スコア3966

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

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

samankoar

2020/05/25 23:48

返事ありがとうございました Kernel をRestart & Run Allにしてもまだ同じです。各データで2本の線が出ます
jeanbiego

2020/05/25 23:49

subplotの引数は入れましたか?
jeanbiego

2020/05/25 23:54

あ、Japanese groom and foreign brideとかが2列ずつあるように読めますが、なんで2列あるのでしょうか。 2列ずつあるから2本ずつ出ているのでは…?
samankoar

2020/05/25 23:59

コードを修正しました。2列に読めます?!Japanese groom and foreign brideと年は一つの線でJapanese bride and foreign groomでもう一つの線と(計2本)イメージしている
jeanbiego

2020/05/26 00:01

コードではなく、DataFrameの中身です。 Data columns (total 9 columns):以下に、同じ名前のカラムが2つずつ出てますよね?Japanese groom and foreign brideとgroomが2つずつ。
samankoar

2020/05/26 00:04

Japanese groom and foreign bride は一つの列の名前、Japanese groom and foreign brideももう一つの列の名前。col名にスペースはダメですか?すみません初心者で 汗
jeanbiego

2020/05/26 00:08

col名にスペースとかは無関係です。 たとえば、DataFrameの列がA,B,C,Aと4列ある場合に、AをplotしようとするとAという名前の列が2つあるので2本の線が出ますよという話です。要らない方のAを消すか名前変えるかしないといけません。
samankoar

2020/05/26 01:18

やっとわかりました。プロットする為のコードではなくて、元のデータに同じ名前の列がありました。ありがとうございました。
guest

あなたの回答

tips

太字

斜体

打ち消し線

見出し

引用テキストの挿入

コードの挿入

リンクの挿入

リストの挿入

番号リストの挿入

表の挿入

水平線の挿入

プレビュー

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

ただいまの回答率
85.48%

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

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

質問する

関連した質問