0
0

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 1 year has passed since last update.

条件ごとにヒストグラムを出力

Posted at

0の値だけ出力

import pandas as pd
import matplotlib.pyplot as plt

# CSVファイルを読み込む
csv_file_path = 'hairetu.csv'  # 自分のファイルパスに置き換えてください
data = pd.read_csv(csv_file_path)

# 3列目が0の行を抽出
zero_values_rows = data[data.iloc[:, 2] == 0]

# 抽出した行の6列目の値を取得
zero_values_for_histogram = zero_values_rows.iloc[:, 5]

# ヒストグラムをプロット
plt.hist(zero_values_for_histogram, bins=10, edgecolor='black')  # ビン数やエッジカラーは適宜調整可能
plt.xlabel('Value')
plt.ylabel('Frequency')
plt.title('Histogram of 6th Column for Rows with 3rd Column = 0')
plt.show()

1の値を出力

# 3列目が8の行を抽出
one_values_rows = data[data.iloc[:, 2] == 1]

# 抽出した行の6列目の値を取得
one_values_for_histogram = one_values_rows.iloc[:, 5]

# ヒストグラムをプロット
plt.hist(one_values_for_histogram, bins=10, edgecolor='black')  # ビン数やエッジカラーは適宜調整可能
plt.xlabel('Value')
plt.ylabel('Frequency')
plt.title('Histogram of 6th Column for Rows with 3rd Column = 0')
plt.show()

二つの値を取得して重ねる

import numpy as np
import matplotlib.pyplot as plt

# ヒストグラムをプロット
plt.hist(zero_values_for_histogram, bins=10, alpha=0.5, label='Column 3 = 0', edgecolor='black')
plt.hist(one_values_for_histogram, bins=10, alpha=0.5, label='Column 3 = 1', edgecolor='black')

# ラベルやタイトルの設定
plt.xlabel('Value')
plt.ylabel('Frequency')
plt.title('Histogram of Values from Selected Rows')
plt.legend(loc='upper right')

# プロットを表示
plt.show()

# プロットを保存
plt.savefig('histogram.png')
0
0
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
0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?