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

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

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

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

Q&A

1回答

2858閲覧

グラフが表示されない理由がわかりません。

valley

総合スコア0

Python

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

1グッド

0クリップ

投稿2021/05/27 08:04

編集2021/05/27 11:22

pythonで実行すると文字が出てきてグラフとして表示されません。なぜでしょうか。

Mu1= [0. 0.]
Sigma1= [[2. 0. ]
[0. 0.5]]
Mu2= [5. 0.]
Sigma2= [[ 2.5 -1.5]
[-1.5 2.5]]
Traceback (most recent call last):
File "report7.py", line 181, in <module>
plt.show()
File "/home/g2021048/.local/lib/python3.8/site-packages/matplotlib/pyplot.py", line 378, in show
return _backend_mod.show(*args, **kwargs)
File "/home/g2021048/.local/lib/python3.8/site-packages/matplotlib/backend_bases.py", line 3575, in show
manager.show() # Emits a warning for non-interactive backend.
File "/home/g2021048/.local/lib/python3.8/site-packages/matplotlib/backends/backend_gtk3.py", line 393, in show
self.canvas.draw()
File "/home/g2021048/.local/lib/python3.8/site-packages/matplotlib/backends/backend_gtk3agg.py", line 70, in draw
backend_agg.FigureCanvasAgg.draw(self)
File "/home/g2021048/.local/lib/python3.8/site-packages/matplotlib/backends/backend_agg.py", line 406, in draw
self.figure.draw(self.renderer)
File "/home/g2021048/.local/lib/python3.8/site-packages/matplotlib/artist.py", line 74, in draw_wrapper
result = draw(artist, renderer, *args, **kwargs)
File "/home/g2021048/.local/lib/python3.8/site-packages/matplotlib/artist.py", line 51, in draw_wrapper
return draw(artist, renderer, *args, **kwargs)
File "/home/g2021048/.local/lib/python3.8/site-packages/matplotlib/figure.py", line 2780, in draw
mimage._draw_list_compositing_images(
File "/home/g2021048/.local/lib/python3.8/site-packages/matplotlib/image.py", line 132, in _draw_list_compositing_images
a.draw(renderer)
File "/home/g2021048/.local/lib/python3.8/site-packages/matplotlib/artist.py", line 51, in draw_wrapper
return draw(artist, renderer, *args, **kwargs)
File "/home/g2021048/.local/lib/python3.8/site-packages/matplotlib/_api/deprecation.py", line 431, in wrapper
return func(*inner_args, **inner_kwargs)
File "/home/g2021048/.local/lib/python3.8/site-packages/matplotlib/axes/_base.py", line 2921, in draw
mimage._draw_list_compositing_images(renderer, self, artists)
File "/home/g2021048/.local/lib/python3.8/site-packages/matplotlib/image.py", line 132, in _draw_list_compositing_images
a.draw(renderer)
File "/home/g2021048/.local/lib/python3.8/site-packages/matplotlib/artist.py", line 51, in draw_wrapper
return draw(artist, renderer, *args, **kwargs)
File "/home/g2021048/.local/lib/python3.8/site-packages/matplotlib/collections.py", line 1012, in draw
super().draw(renderer)
File "/home/g2021048/.local/lib/python3.8/site-packages/matplotlib/artist.py", line 51, in draw_wrapper
return draw(artist, renderer, *args, **kwargs)
File "/home/g2021048/.local/lib/python3.8/site-packages/matplotlib/collections.py", line 406, in draw
renderer.draw_markers(
TypeError: must be real number, not str


python

1#!/usr/bin/env python3 2#coding:utf-8 3# 4# パターンが2次元連続変数で 5# 多変量正規分布(Multivariate Normal Distribution) でモデル化した場合の 6# 識別境界面 7# 8from mpl_toolkits.mplot3d import Axes3D 9import numpy as np 10import matplotlib.pyplot as plt 11import matplotlib.tri as tri 12import matplotlib.cm as cm 13import functools 14import scipy.stats as st 15import scipy.special as sp 16import math 17import sys 18from mpl_toolkits.axes_grid1 import make_axes_locatable 19 20 21class Norm2D: 22 """ 23 2変量正規分布 (2 dimensional normal distribution) 24 """ 25 def __init__(self, X): 26 self.__X = np.copy(X) 27 self.__Mu = np.mean(self.__X, axis=0) 28 self.__Sigma = np.cov(self.__X, rowvar=False, bias=True) 29 self.__dim = len(self.__Mu) 30 self.__N = len(self.__X) 31 32 def get_N(self): 33 """ 学習データ数の取得 """ 34 return self.__N 35 36 def get_X(self): 37 """ 学習データの取得 """ 38 return self.__X 39 40 def get_param(self): 41 """ 正規分布パラメタの取得 """ 42 return self.__Mu, self.__Sigma 43 44 def pdf(self, x): 45 """ 与えられた分布の(x)における確率密度値を求める""" 46 return st.multivariate_normal.pdf(x, mean=self.__Mu, cov=self.__Sigma) 47 48 def sampling(self, N): 49 """ 与えられた分布に従ってN点サンプリングする""" 50 return st.multivariate_normal.rvs(mean=self.__Mu, cov=self.__Sigma, size=N) 51 52 def this_likelihood(self, x): 53 """ パターンxが与えられた時の与えられた分布での尤度を求める """ 54 L = 1.0 55 for n in range(len(x)): 56 L *= self.pdf(x[n]) 57 return L 58 59 def this_log_likelihood(self, x): 60 """ パターンxが与えられた時の与えられた分布での対数尤度を求める """ 61 logL = 0.0 62 for n in range(len(x)): 63 logL += log(self.pdf(x[n])) 64 return logL 65 66 67# 例題2 68# x(ω1) = {(0, 4√2), (4, 4√2), (2, 4√2-1), (2, 4√2+1)} 69# x(ω2) = {(0, 0), (4, 0), (2, -1), (2, 1), (2(1-√3), 0), (2(1+√3), 0), (2, -√3), (2, √3)} 70""" 71x1 = np.array([[0.0, 4.0*np.sqrt(2.0)], 72 [4.0, 4.0*np.sqrt(2.0)], 73 [2.0, 4.0*np.sqrt(2.0)-1.0], 74 [2.0, 4.0*np.sqrt(2.0)+1.0]]) 75x2 = np.array([[0.0, 0.0], 76 [4.0, 0.0], 77 [2.0,-1.0], 78 [2.0, 1.0], 79 [2.0*(1.0-np.sqrt(3.0)), 0], 80 [2.0*(1.0+np.sqrt(3.0)), 0], 81 [2.0,-np.sqrt(3.0)], 82 [2.0, np.sqrt(3.0)]]) 83# Set the drow range 84x = y = np.arange(-2, 18, 0.1) 85""" 86 87# 88# 例題3 89# x(ω1) = {(-2, 0), (0, 1), (0, -1), (2, 0)} 90# x(ω2) = {(3, 2), (4, -1), (6, 1), (7, -2)} 91x1 = np.array([[-2.0, 0.0], 92 [0.0, 1.0], 93 [0.0,-1.0], 94 [2.0, 0.0]]) 95x2 = np.array([[3.0, 2.0], 96 [4.0,-1.0], 97 [6.0, 1.0], 98 [7.0,-2.0]]) 99# Set the drow range 100x = y = np.arange(-5, 10, 0.1) 101 102# 103# Create Distributions 104# 105dist1 = Norm2D(x1) 106dist2 = Norm2D(x2) 107 108# 109# Show Mu and Sigma 110# 111Mu1, Sigma1 = dist1.get_param() 112print('Mu1=', Mu1); 113print('Sigma1=', Sigma1) 114 115Mu2, Sigma2 = dist2.get_param() 116print('Mu2=', Mu2) 117print('Sigma2=', Sigma2) 118 119 120# 121# Create the drawing mesh 122# 123X, Y = np.meshgrid(x, y) 124pos = np.dstack((X,Y)) 125 126# 127# Calc. pdf 128# 129Z1 = dist1.pdf(pos) 130Z2 = dist2.pdf(pos) 131 132Z = np.log(np.fmax(Z1, Z2)) # Logarithmic expression 133Zdiff = Z1 - Z2 134 135maxZ = np.max(Z) 136minZ = np.min(Z) 137#print('max Z=', maxZ) 138#print('min Z=', minZ) 139 140minZ = -20 # Clip the lowest Z value 141 142 143# 144# Init. a graph 145# 146fig = plt.figure(figsize=(7,5)) 147 148ax = fig.add_subplot(111, aspect='equal') 149ax.grid() 150plt.xlabel('x') 151plt.ylabel('y') 152 153 154# 155# draw the contour 156# 157levels = np.linspace(minZ, maxZ, 50) 158cs = ax.contourf(X, Y, Z, levels=levels, cmap=cm.inferno, extend='both') 159 160# 161# draw the boundary 162# 163ax.contour(X, Y, Zdiff, levels=[-1.0e-300, 1.0e-300], colors='r', linestyles='-') 164 165# 166# data 167# 168ax.scatter(dist1.get_X()[:,0], dist1.get_X()[:,1], s=40, c='red', marker='o', alpha=0.5, linewidths='2') 169ax.scatter(dist2.get_X()[:,0], dist2.get_X()[:,1], s=40, c='blue', marker='o', alpha=0.5, linewidths='2') 170 171# 172# draw a colorbar 173# 174divider = make_axes_locatable(ax) # create the same size divider as the contour graph 175cax = divider.append_axes('right', size='5%', pad=0.1) # append 5% space to the right-end 176fig.colorbar(cs, cax=cax, format='%.1f') # show a colorbar 177 178# 179# Draw 180# 181plt.show()
SumsumN👍を押しています

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

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

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

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

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

ppaul

2021/05/27 08:45

report7.pyにバグがあるからです。原因を質問したいならreport7.pyのコードを質問に追加しましょう。
valley

2021/05/27 09:04

ソースコード追加しました。よろしくおねがいします。
jbpb0

2021/05/27 09:39

pythonのコードの一番最初の行のすぐ上に ```python だけの行を追加してください また、pythonのコードの一番最後の行のすぐ下に ``` だけの行を追加してください 現状、コードがとても読み辛いです 質問にコードを載せる際に上記をやってくれたら、他人がコードを読みやすくなり、コードの実行による現象確認もやりやすくなるので、回答されやすくなります
valley

2021/05/27 11:22

すみません、、ありがとうございます。改善しました。
guest

回答1

0

TypeError: must be real number, not str

python

1ax.scatter(...linewidths='2')

↓ 修正 (二ヶ所)

python

1ax.scatter(...linewidths=2)

投稿2021/05/28 04:07

編集2021/05/28 04:08
jbpb0

総合スコア7651

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

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

あなたの回答

tips

太字

斜体

打ち消し線

見出し

引用テキストの挿入

コードの挿入

リンクの挿入

リストの挿入

番号リストの挿入

表の挿入

水平線の挿入

プレビュー

まだベストアンサーが選ばれていません

会員登録して回答してみよう

アカウントをお持ちの方は

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

ただいまの回答率
85.48%

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

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

質問する

関連した質問