日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當(dāng)前位置: 首頁 > 编程资源 > 编程问答 >内容正文

编程问答

9、10、11、12、13_添加标注 (Annotations)、添加网格线(Grid Lines)、显示中文字体、保存图形(saving Figures)、高质量矢量图输出

發(fā)布時(shí)間:2024/9/27 编程问答 26 豆豆
生活随笔 收集整理的這篇文章主要介紹了 9、10、11、12、13_添加标注 (Annotations)、添加网格线(Grid Lines)、显示中文字体、保存图形(saving Figures)、高质量矢量图输出 小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,幫大家做個(gè)參考.

9.添加標(biāo)注 (Annotations)
10.添加網(wǎng)格線(Grid Lines)
11.顯示中文字體
12.保存圖形(saving Figures)
13.高質(zhì)量矢量圖輸出

9.添加標(biāo)注 (Annotations)

有時(shí)候我們對(duì)某點(diǎn)如3?sin(3?pi/4)的值特別感興趣。

import numpy as np print(3 * np.sin(3 * np.pi / 4))

2.121320343559643

如果想在圖標(biāo)上標(biāo)出這一點(diǎn),可以使用annotate函數(shù)執(zhí)行此操作。

import numpy as np import matplotlib.pyplot as plt X = np.linspace(-2 * np.pi, 3 * np.pi, 70, endpoint=True) F1 = np.sin(X) F2 = 3 * np.sin(X) ax = plt.gca() plt.xticks( [-6.28, -3.14, 3.14, 6.28],[r'$-2\pi$', r'$-\pi$', r'$+\pi$', r'$+2\pi$']) plt.yticks([-3, -1, 0, +1, 3]) x = 3 * np.pi / 4 plt.scatter([x,],[3 * np.sin(x),], 50, color ='blue') plt.annotate(r'$(3\sin(\frac{3\pi}{4}),\frac{3}{\sqrt{2}})$',xy=(x, 3 * np.sin(x)),xycoords='data',xytext=(+20, +20),textcoords='offset points',fontsize=16,arrowprops=dict(facecolor='blue')) plt.plot(X, F1, label="$sin(x)$") plt.plot(X, F2, label="$3 sin(x)$") plt.legend(loc='lower left') plt.show()

我們必須提供有關(guān)annotate參數(shù)的一些信息。

參數(shù)含義
xycoordinates of the arrow tip
xytextcoordinates of the text location

我們示例的xy和xytext位置在數(shù)據(jù)坐標(biāo)中。 我們還可以選擇其他坐標(biāo)系系統(tǒng)。 可以為xy和xytext的坐標(biāo)系指定字符串值,賦值給xycoords和textcoords。 默認(rèn)值為“data”:

字符串值坐標(biāo)系統(tǒng)
‘figure points’Points from the lower left of the figure
‘figure pixels’Pixels from the lower left of the figure
‘figure fraction’Fraction of figure from lower left
‘a(chǎn)xes points’Points from lower left corner of axes
‘a(chǎn)xes pixels’Pixels from lower left corner of axes
‘a(chǎn)xes fraction’Fraction of axes from lower left
‘data’Use the coordinate system of the object being annotated (default)
‘polar’(theta, r) if not native ‘data’ coordinates

此外,還可以指定箭頭的屬性。 為此,我們必須為參數(shù)arrowprops提供一個(gè)箭頭屬性字典:

arrowprops key描述
widthThe width of the arrow in points
headwidthThe width of the base of the arrow head in points
headlengthThe length of the arrow head in points
shrinkFraction of total length to shrink from both ends
**kwargsany key for matplotlib.patches.Polygon, e.g., facecolor

在以下示例中,我們將更改上一示例的箭頭的外觀:

import numpy as np import matplotlib.pyplot as pltX = np.linspace(-2 * np.pi, 3 * np.pi, 70, endpoint=True) F1 = np.sin(X) F2 = 3 * np.sin(X) ax = plt.gca() plt.xticks([-6.28, -3.14, 3.14, 6.28],[r'$-2\pi$', r'$-\pi$', r'$+\pi$', r'$+2\pi$']) plt.yticks([-3, -1, 0, +1, 3]) x = 3 * np.pi / 4 plt.scatter([x, ], [3 * np.sin(x), ], 50, color='blue') plt.annotate(r'$(3\sin(\frac{3\pi}{4}),\frac{3}{\sqrt{2}})$',xy=(x, 3 * np.sin(x)),xycoords='data',xytext=(+20, +20),textcoords='offset points',fontsize=16,arrowprops=dict(facecolor='blue', headwidth=10, headlength=10, width=2, shrink=0.1)) plt.plot(X, F1, label="$sin(x)$") plt.plot(X, F2, label="$3 sin(x)$") plt.legend(loc='lower left') plt.show()

10.添加網(wǎng)格線(Grid Lines)

import numpy as np import matplotlib.pyplot as pltdef f(t):return np.exp(-t) * np.cos(2 * np.pi * t)def g(t):return np.sin(t) * np.cos(1 / (t + 0.1))t1 = np.arange(0.0, 5.0, 0.1) t2 = np.arange(0.0, 5.0, 0.02) plt.plot(t1, g(t1), 'ro', t2, f(t2), 'k') plt.grid(color='b', alpha=0.5, linestyle='dashed', linewidth=1.0) plt.show()

11.顯示中文字體

Matplotlib默認(rèn)是不支持顯示中文字符的。

解決方法: 可以使用 rc 配置(rcParams)來自定義圖形的各種默認(rèn)屬性。

Windows操作系統(tǒng)支持的中文字體和代碼:

配置方式:

plt.rcParams['font.family'] = ['sans-serif'] plt.rcParams['font.sans-serif'] = ['SimHei']

示例如下:

import matplotlib.pyplot as plt # 在jupyter notebook 中,設(shè)置下面兩行來顯示中文 plt.rcParams['font.family'] = ['sans-serif'] # 在 PyCharm 中,只需要下面一行,‘STXingkai’:華文行楷 plt.rcParams['font.sans-serif'] = ['STXingkai'] days = list(range(1,9)) celsius_values = [25.6, 24.1, 26.7, 28.3, 27.5, 30.5, 32.8, 33.1] plt.plot(days, celsius_values) plt.xlabel('日期', size=16) plt.ylabel('攝氏度', size=16) plt.title('溫度變化', size=16) plt.show()

另外的方法:
在每一處使用到中文輸出的地方,都加上一個(gè)字體屬性fontproperties

import matplotlib.pyplot as plt days = list(range(1,9)) celsius_values = [25.6, 24.1, 26.7, 28.3, 27.5, 30.5, 32.8, 33.1] plt.plot(days, celsius_values) plt.xlabel('日期', fontproperties='SimHei', size=16) plt.ylabel('攝氏度', fontproperties='STXingkai', size=16) plt.title('溫度變化', fontproperties='SimHei', size=16) plt.show()

12.保存圖形(saving Figures)

savefig方法可用來保存圖形到文件中:

fig.savefig(“filename.png”)

可以指定分辨率DPI(Dots Per Inch,每英寸點(diǎn)數(shù))和選擇輸出文件格式:

可以采用PNG,JPG,EPS,SVG,PGF和PDF格式生成輸出。

import matplotlib import matplotlib.pyplot as plt fig = plt.figure() plt.plot([0, 1, 2, 3, 4], [0, 3, 5, 9, 11]) plt.xlabel('Months') plt.ylabel('Books Read') plt.show() fig.savefig('books_read.png')

import matplotlib.pyplot as plt import matplotlib.image as mpimg img = mpimg.imread('books_read.png') plt.imshow(img)

13.高質(zhì)量矢量圖輸出

Jupyter Notebook中顯示svg矢量圖的設(shè)置: %config InlineBackend.figure_format = ‘svg’

%matplotlib inline %config InlineBackend.figure_format = 'svg' import matplotlib.pyplot as plt # 在jupyter notebook 中,設(shè)置下面兩行來顯示中文 plt.rcParams['font.family'] = ['sans-serif'] # 在 PyCharm 中,只需要下面一行 plt.rcParams['font.sans-serif'] = ['SimHei'] days = list(range(1,9)) celsius_values = [25.6, 24.1, 26.7, 28.3, 27.5, 30.5, 32.8, 33.1] fig = plt.figure() plt.plot(days, celsius_values) plt.xlabel('日期', size=16) plt.ylabel('攝氏度', size=16) plt.title('溫度變化', size=16) plt.show() fig.savefig('celsius_degrees.svg')

總結(jié)

以上是生活随笔為你收集整理的9、10、11、12、13_添加标注 (Annotations)、添加网格线(Grid Lines)、显示中文字体、保存图形(saving Figures)、高质量矢量图输出的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網(wǎng)站內(nèi)容還不錯(cuò),歡迎將生活随笔推薦給好友。