|
|
|||||||||||||||||||||||||||||
|
pylab_examples_Examples 60_scatter_demo2. |
H.Kamifuji . |
|
さまざまなマーカーの色とサイズの散布図のデモ。 この事例は、Windows10_1909 で Python 3.9.0 環境では、動作しません。( datafile = cbook.get_sample_data('goog.npy') が削除されたのか? )
"""
Demo of scatter plot with varying marker colors and sizes.
"""
import numpy as np
import matplotlib.pyplot as plt
import matplotlib.cbook as cbook
# Load a numpy record array from yahoo csv data with fields date,
# open, close, volume, adj_close from the mpl-data/example directory.
# The record array stores python datetime.date as an object array in
# the date column
datafile = cbook.get_sample_data('goog.npy')
try:
# Python3 cannot load python2 .npy files with datetime(object) arrays
# unless the encoding is set to bytes. However this option was
# not added until numpy 1.10 so this example will only work with
# python 2 or with numpy 1.10 and later
price_data = np.load(datafile, encoding='bytes').view(np.recarray)
except TypeError:
price_data = np.load(datafile).view(np.recarray)
price_data = price_data[-250:] # get the most recent 250 trading days
delta1 = np.diff(price_data.adj_close)/price_data.adj_close[:-1]
# Marker size in units of points^2
volume = (15 * price_data.volume[:-2] / price_data.volume[0])**2
close = 0.003 * price_data.close[:-2] / 0.003 * price_data.open[:-2]
fig, ax = plt.subplots()
ax.scatter(delta1[:-1], delta1[1:], c=close, s=volume, alpha=0.5)
ax.set_xlabel(r'$\Delta_i$', fontsize=15)
ax.set_ylabel(r'$\Delta_{i+1}$', fontsize=15)
ax.set_title('Volume and percent change')
ax.grid(True)
fig.tight_layout()
plt.show()
|
![]() Python 3.11.2 見直しました。上記のコードでは、下記のエラーが発生します。 In a future release, get_sample_data will automatically load numpy arrays. Set np_load to True to get the array and suppress this warning. Set asfileobj to False to get the path to the data file and suppress this warning. datafile = cbook.get_sample_data('goog.npy') Traceback (most recent call last): File "_:\scatter_demo2.py", line 12, in <module> datafile = cbook.get_sample_data('goog.npy') ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ File "C:\Users\_____\AppData\Local\Programs\Python\Python311\Lib\site-packages\matplotlib\cbook\__init__.py", line 541, in get_sample_data return path.open('rb') ^^^^^^^^^^^^^^^ File "C:\Users\_____\AppData\Local\Programs\Python\Python311\Lib\pathlib.py", line 1044, in open return io.open(self, mode, buffering, encoding, errors, newline) ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ FileNotFoundError: [Errno 2] No such file or directory: 'C:\\Users\\_____\\AppData\\Local\\Programs\\Python\\Python311\\Lib\\site-packages\\matplotlib\\mpl-data\\sample_data\\goog.npy' matplotlib 内部のエラーのようです。matplotlib の改修(先祖帰りバグの改修)を待つしかない。 Python 3.11.6 (matplotlib 3.7.1) では、下記のようなエラーがあり、実行できない。
M:\______\scatter_demo2.py:12: MatplotlibDeprecationWarning:
In a future release, get_sample_data will automatically load numpy arrays.
Set np_load to True to get the array and suppress this warning.
Set asfileobj to False to get the path to the data file and suppress this warning.
datafile = cbook.get_sample_data('goog.npy')
Traceback (most recent call last):
File "M:\______\scatter_demo2.py", line 12, in
Python 3.12.0 (matplotlib 3.8.1) では、下記のようなエラーがあり、実行できない。Traceback (most recent call last): File "E:\______\scatter_demo2.py", line 12, inPython 3.11.6 (matplotlib 3.7.1) 及び Python 3.12.0 (matplotlib 3.8.1) で、見直し中、新しいサンプル(lines-bars-and-markers-scatter-demo2-py) を見つけ、下記のコードで、正常に実行できました。
"""
=============
Scatter Demo2
=============
Demo of scatter plot with varying marker colors and sizes.
"""
import matplotlib.pyplot as plt
import numpy as np
import matplotlib.cbook as cbook
# Load a numpy record array from yahoo csv data with fields date, open, high,
# low, close, volume, adj_close from the mpl-data/sample_data directory. The
# record array stores the date as an np.datetime64 with a day unit ('D') in
# the date column.
price_data = cbook.get_sample_data('goog.npz')['price_data']
price_data = price_data[-250:] # get the most recent 250 trading days
delta1 = np.diff(price_data["adj_close"]) / price_data["adj_close"][:-1]
# Marker size in units of points^2
volume = (15 * price_data["volume"][:-2] / price_data["volume"][0])**2
close = 0.003 * price_data["close"][:-2] / 0.003 * price_data["open"][:-2]
fig, ax = plt.subplots()
ax.scatter(delta1[:-1], delta1[1:], c=close, s=volume, alpha=0.5)
ax.set_xlabel(r'$\Delta_i$', fontsize=15)
ax.set_ylabel(r'$\Delta_{i+1}$', fontsize=15)
ax.set_title('Volume and percent change')
ax.grid(True)
fig.tight_layout()
plt.show()
Python 3.11.6 (matplotlib 3.7.1) では、下記のようなエラーがあり、実行できない。goog.npy は、共通品で評価するも、エラー。 Python 3.11.6 と Python 3.12.0 の差異があるようだ!
M:\______\scatter_demo2_2.py:17: MatplotlibDeprecationWarning:
In a future release, get_sample_data will automatically load numpy arrays.
Set np_load to True to get the array and suppress this warning.
Set asfileobj to False to get the path to the data file and suppress this warning.
price_data = cbook.get_sample_data('goog.npz')['price_data']
Traceback (most recent call last):
File "M:\______\scatter_demo2_2.py", line 17, in
Python 3.12.0 (matplotlib 3.8.1) では、正常実行です。![]() |
|
pylab_examples_Examples code: scatter_demo2.py lines-bars-and-markers-scatter-demo2-py |
|