0
2

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 3 years have passed since last update.

pygameでグラフ描画

Last updated at Posted at 2021-07-02

日付:2021/06
分類:Python
サンプル

import pygame
from pygame.locals import *
import sys
import math
import random

x0=50
y0=50
wx=500
wy=500

def event(): # =>void
    # イベント処理
    for event in pygame.event.get():#イベントキューからキーボードやマウスの動きを取得
        if event.type == QUIT:      # 閉じるボタンが押されたら終了
            pygame.quit()          # Pygameの終了(ないと終われない)
            sys.exit()             #終了(ないとエラーで終了することになる)

def rgen(m):
    ans=[]
    for i in range(m):
        r=random.randint(1,10)
        ans.append(r)
    #print(ans)
    return ans

# def hist(ar):#ar:int[]=>ans:{i:number : count:number}
#     ans={}
#     for a in ar:
#         if a in ans:
#             ans[a]+=1
#         else:
#             ans[a]=1
#     #print(ans)
#     return ans#ans:{i:number : count:number}

def hist2(ar):#ar:int[]
    ans=[0 for i in range(11) ]
    for a in ar:
        ans[a]+=1
    #print(ans)
    return ans#ans:int[]

#結果のグラフ表示 
def graph(screen, font, ar):
    ymax=max(ar)
    xmax=len(ar)+1
    #print("xmax=",xmax," ymax=",ymax)
    lbl=[str(a) for a in ar]
    #print("lbl=",lbl)
    pygame.draw.rect(screen, (255,0,0), Rect(0,0,500,500), 1)
    for i,a in enumerate(ar):
        my = a
        mx = i
        #1x1化
        sx = mx/xmax
        sy = my/ymax
        gx = sx * 300 + 100
        gy = -sy * 300+ 400
        gy2 = sy * 300
        #pygame.draw.line(screen, Infect, (gx, gy), (gx+4, y0 + wy), 4)
        pygame.draw.rect(screen, (255,0,0), Rect(gx,gy,10,gy2), 2)
        #y座標
        tx = lbl[i][:4]
        txt = font.render(tx, True, (0,0,0))   # 描画する文字列の設定
        screen.blit(txt, [gx, gy-30])# 文字列の表示位置
        #x
        tx = str(i)
        txt = font.render(tx, True, (0,0,0))   # 描画する文字列の設定
        screen.blit(txt, [gx, 405])# 文字列の表示位置

#ポアソン分布計算
def calc_p():
    lmd=5
    k=10
    ar=[]
    for k in range(12):
      ps=(lmd**k)*math.e**(-lmd)/math.factorial(k)
      ar.append(ps)
    return ar

#組み合わせ
def comb(n, r):
    return math.factorial(n) // (math.factorial(n - r) * math.factorial(r))

#2こう分布計算
def calc_2(p,n):
    ar=[]
    for k in range(n+1):
      ans = comb(n,k)*(p**k)*((1-p)**(n-k))
      ar.append(ans)
    return ar

def main():
    pygame.init()                                 # Pygameの初期化
    font = pygame.font.Font(None, 25) 
    screen = pygame.display.set_mode((800, 600))  # 800*600の画面

    #ans = calc_p()#ポアソン
    #ans = calc_2(0.3,5)#2こう分布
    ans  = hist2(rgen(100)) #ランダム

    while (1):
        screen.fill((255,255,255))  # 画面を白に
        graph(screen, font, ans)
        pygame.display.update()     # 画面更新
        event()
if __name__ == "__main__":
    main()
0
2
1

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
2

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?