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?

MatPlotClass and table

Posted at

import json
import pandas as pd
import openpyxl
import matplotlib.pyplot as plt
from matplotlib.ticker import ScalarFormatter, FormatStrFormatter
import japanize_matplotlib
from pathlib import Path
import pprint

class MatPlotLogX():
    def __init__(self):
        self.fig, self.ax = plt.subplots()
        
        # x軸とy軸を対数目盛りに設定する。
        self.ax.set_xscale("log")
        self.ax.set_yscale("log")

        # x, y軸のラベルを指数表記から通常の表記に設定(ただしe表記は残る)
        self.ax.xaxis.set_major_formatter(FormatStrFormatter('%g'))
        self.ax.yaxis.set_major_formatter(FormatStrFormatter('%g'))

        # X軸とY軸のラベルを設定
        self.ax.set_title("Acc result")
        self.ax.set_xlabel("frequency[Hz]")
        self.ax.set_ylabel("PSD")

        # グリッドを表示する。
        self.ax.grid(True, "major", linestyle="-", linewidth=.7, color='g')
        self.ax.grid(True, "minor", linestyle="-", linewidth=.3, color='g')

    def set_x_range(self, lower_limit, upper_limit):
        self.ax.set_xlim([lower_limit, upper_limit])

    def set_y_range(self, lower_limit, upper_limit):
        self.ax.set_ylim([lower_limit, upper_limit])

    def set_legend(self):
        self.ax.legend(loc='upper left', bbox_to_anchor=(1, 1))  # グラフの右上外部に配置

    def add_plot(self, x_data, y_data, color=None, label=None):
        # self.ax.plot(x_data, y_data, color=color, label=label)
        self.ax.plot(x_data, y_data, color=color, label=label)
    
    
    def show_figure(self):
        plt.show()

    def save_as_png(self, path):
        self.fig.savefig(path)
        
fig1 = MatPlotLogX()
fig1.set_x_range(10, 10000)
fig1.set_y_range(0.00001, 1)
fig1.add_plot(x_data, y_data, label='test')
fig1.add_plot(x_data2, y_data2, color='red')
fig1.set_legend()
fig1.show_figure()

import matplotlib.pyplot as plt
import japanize_matplotlib
import pandas as pd

if __name__ == '__main__':
    data = {
        'Tokyo': ['27\nHigh', '23\nLow', '27\nHigh\n \n※注意', '24\nMedium', '25\nHigh', '23\nLow', '26\nHigh'],
        'Osaka': ['26\nMedium', '23\nLow', '27\nHigh', '28\nVery High', '24\nMedium', '22\nLow', '27\nHigh'],
        'Osaka': ['26\nMedium', '23\nLow', '27\nHigh', '28\nVery High', '24\nMedium', '22\nLow', '27\nHigh'],
    }

    df = pd.DataFrame(data)

    fig, ax = plt.subplots(figsize=(8, 6))  # 図全体のサイズを調整

    ax.axis('off')
    ax.axis('tight')

    # テーブルの幅を調整 (bboxの4番目の値を変更)
    tb = ax.table(cellText=df.values,
                 colLabels=df.columns,
                 bbox=[0, 0, 1.2, 1.5],  # 3つ目が表の幅、4つ目が表の高さ
                 cellLoc='left',
                 loc='center')

    # ヘッダー行の色とテキストプロパティを設定
    tb[0, 0].set_facecolor('#363636')
    tb[0, 1].set_facecolor('#363636')
    tb[0, 0].set_text_props(color='w')
    tb[0, 1].set_text_props(color='w')
    
    # 各行の高さを増やす
    for i in range(len(df) + 1):  # ヘッダー行を含む
        for j in range(len(df.columns)):
            cell = tb[i, j]
            cell.set_fontsize(12)  # フォントサイズを調整

    # 3行目の高さを0.5に変更
    for j in range(len(df.columns)):
        cell = tb[1, j]  # 3行目の各セル
        cell.set_height(0.05)

    plt.show()

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?