--2022-05-17 14:07:26-- https://drive.google.com/uc?id=1eGAsTN1HBpJAkeVM57_C7ccp7hbgSz3_Resolving drive.google.com (drive.google.com)... 142.250.152.139, 142.250.152.113, 142.250.152.138, ...Connecting to drive.google.com (drive.google.com)|142.250.152.139|:443... connected.HTTP request sent, awaiting response... 303 See OtherLocation: https://doc-0k-9o-docs.googleusercontent.com/docs/securesc/ha0ro937gcuc7l7deffksulhg5h7mbp1/01nvri1mcp3jq2m4dmirg17tsr3ic3no/1652796375000/02847987870453524430/*/1eGAsTN1HBpJAkeVM57_C7ccp7hbgSz3_ [following]Warning: wildcards not supported in HTTP.--2022-05-17 14:07:26-- https://doc-0k-9o-docs.googleusercontent.com/docs/securesc/ha0ro937gcuc7l7deffksulhg5h7mbp1/01nvri1mcp3jq2m4dmirg17tsr3ic3no/1652796375000/02847987870453524430/*/1eGAsTN1HBpJAkeVM57_C7ccp7hbgSz3_Resolving doc-0k-9o-docs.googleusercontent.com (doc-0k-9o-docs.googleusercontent.com)... 142.250.159.132, 2607:f8b0:4001:c58::84Connecting to doc-0k-9o-docs.googleusercontent.com (doc-0k-9o-docs.googleusercontent.com)|142.250.159.132|:443... connected.HTTP request sent, awaiting response... 200 OKLength: 20659344 (20M) [application/x-font-ttf]Saving to: ‘TaipeiSansTCBeta-Regular.ttf’TaipeiSansTCBeta-Re 100%[===================>] 19.70M --.-KB/s in 0.1s 2022-05-17 14:07:27 (172 MB/s) - ‘TaipeiSansTCBeta-Regular.ttf’ saved [20659344/20659344]
加入 matplotlib 字型的行列!
In [4]:
fm.fontManager.addfont('TaipeiSansTCBeta-Regular.ttf')mpl.rc('font',family='Taipei Sans TC Beta')
3. 來畫函數的圖形
這一段是我們耍寶之後, 要回到原本設定的。平常是不用做這種事。
In [5]:
saved_state=mpl.rcParams.copy()
Python 畫圖的指令, 我們最需要學只有兩個:
plt.plot
plt.scatter
[1] 我們先來試 plt.plot
In [6]:
plt.plot([1,37,25,68])
Out[6]:
[]
這其實就是把 y 座標為 1, 37, 25, 68 的點, 補上 x 座標 0, 1, 2, 3 連起來的折線圖。這是 Python 最基本的畫圖法。
如果 x 座標也自己給的話, 也就是比較完整的畫圖方式是
plt.plot(X, Y)
其中 X 是點的 x 座標; Y 是點的 y 座標。
比如說, 最常見的是, 我們畫一個函數, 就會決定 x 的範圍、要有多少個點, 再把我們要畫的函數畫出來。比方說我們想畫:
x 的範圍是 -5 到 5, 取 200 個點。
In [7]:
x=np.linspace(-5,5,200)y=np.sinc(x)
In [8]:
plt.plot(x,y)
Out[8]:
[]
我們來試試加上中文。
In [9]:
plt.title('畫個可愛的 sinc(x) 函數')plt.plot(x,y)
Out[9]:
[]
Python 其實是以耍寶見長的程式語言, 我們來耍寶一下。
In [10]:
plt.xkcd()
Out[10]:
In [11]:
plt.plot(x,y)
Out[11]:
[]
你當然可以一直這麼耍寶下去, 但如果不要 xkcd 風了, 可以這麼作。
In [12]:
mpl.rcParams.update(saved_state)
/usr/lib/python3.7/_collections_abc.py:841: MatplotlibDeprecationWarning: The datapath rcparam was deprecated in Matplotlib 3.2.1 and will be removed two minor releases later. self[key] = other[key]/usr/lib/python3.7/_collections_abc.py:841: MatplotlibDeprecationWarning: The savefig.frameon rcparam was deprecated in Matplotlib 3.1 and will be removed in 3.3. self[key] = other[key]/usr/lib/python3.7/_collections_abc.py:841: MatplotlibDeprecationWarning: The text.latex.unicode rcparam was deprecated in Matplotlib 3.0 and will be removed in 3.2. self[key] = other[key]/usr/lib/python3.7/_collections_abc.py:841: MatplotlibDeprecationWarning: The verbose.fileo rcparam was deprecated in Matplotlib 3.1 and will be removed in 3.3. self[key] = other[key]/usr/lib/python3.7/_collections_abc.py:841: MatplotlibDeprecationWarning: The verbose.level rcparam was deprecated in Matplotlib 3.1 and will be removed in 3.3. self[key] = other[key]
.. _california_housing_dataset:California Housing dataset--------------------------**Data Set Characteristics:** :Number of Instances: 20640 :Number of Attributes: 8 numeric, predictive attributes and the target :Attribute Information: - MedInc median income in block group - HouseAge median house age in block group - AveRooms average number of rooms per household - AveBedrms average number of bedrooms per household - Population block group population - AveOccup average number of household members - Latitude block group latitude - Longitude block group longitude :Missing Attribute Values: NoneThis dataset was obtained from the StatLib repository.https://www.dcc.fc.up.pt/~ltorgo/Regression/cal_housing.htmlThe target variable is the median house value for California districts,expressed in hundreds of thousands of dollars ($100,000).This dataset was derived from the 1990 U.S. census, using one row per censusblock group. A block group is the smallest geographical unit for which the U.S.Census Bureau publishes sample data (a block group typically has a populationof 600 to 3,000 people).An household is a group of people residing within a home. Since the averagenumber of rooms and bedrooms in this dataset are provided per household, thesecolumns may take surpinsingly large values for block groups with few householdsand many empty houses, such as vacation resorts.It can be downloaded/loaded using the:func:`sklearn.datasets.fetch_california_housing` function... topic:: References - Pace, R. Kelley and Ronald Barry, Sparse Spatial Autoregressions, Statistics and Probability Letters, 33 (1997) 291-297
/usr/local/lib/python3.7/dist-packages/seaborn/distributions.py:2619: FutureWarning: `distplot` is a deprecated function and will be removed in a future version. Please adapt your code to use either `displot` (a figure-level function with similar flexibility) or `histplot` (an axes-level function for histograms). warnings.warn(msg, FutureWarning)