国产一级a片免费看高清,亚洲熟女中文字幕在线视频,黄三级高清在线播放,免费黄色视频在线看

打開APP
userphoto
未登錄

開通VIP,暢享免費電子書等14項超值服

開通VIP
Matplotlib:設(shè)置坐標(biāo)軸范圍,刻度,位置,自定義刻度名稱,添加數(shù)據(jù)標(biāo)簽

在使用matplotlib模塊時畫坐標(biāo)圖時,往往需要對坐標(biāo)軸設(shè)置很多參數(shù),這些參數(shù)包括橫縱坐標(biāo)軸范圍、坐標(biāo)軸刻度大小、坐標(biāo)軸名稱等

  • xlim():設(shè)置x坐標(biāo)軸范圍
  • ylim():設(shè)置y坐標(biāo)軸范圍
  • xlabel():設(shè)置x坐標(biāo)軸名稱
  • ylabel():設(shè)置y坐標(biāo)軸名稱
  • xticks():設(shè)置x軸刻度
  • yticks():設(shè)置y軸刻度
#創(chuàng)建數(shù)據(jù)x = np.linspace(-5, 5, 100)y1 = np.sin(x)y2 = np.cos(x)#創(chuàng)建figure窗口,figsize設(shè)置窗口的大小plt.figure(num=3, figsize=(8, 5))#畫曲線1plt.plot(x, y1)#畫曲線2plt.plot(x, y2, color='blue', linewidth=5.0, linestyle='--')#設(shè)置坐標(biāo)軸范圍plt.xlim((-5, 5))plt.ylim((-2, 2))#設(shè)置坐標(biāo)軸名稱plt.xlabel('xxxxxxxxxxx')plt.ylabel('yyyyyyyyyyy')#設(shè)置坐標(biāo)軸刻度my_x_ticks = np.arange(-5, 5, 0.5)#對比范圍和名稱的區(qū)別#my_x_ticks = np.arange(-5, 2, 0.5)my_y_ticks = np.arange(-2, 2, 0.3)plt.xticks(my_x_ticks)plt.yticks(my_y_ticks)#顯示出所有設(shè)置plt.show()

import matplotlib.pyplot as pltimport numpy as npx = np.linspace(-3, 3, 50)y1 = 2*x + 1y2 = x**2plt.figure()plt.plot(x, y2)plt.plot(x, y1, color='red', linewidth=1.0, linestyle='--')plt.xlim((-1, 2))plt.ylim((-2, 3))plt.xlabel('I am x')plt.ylabel('I am y')plt.show()

x = np.linspace(-3, 3, 50)y1 = 2*x + 1y2 = x**2plt.figure()plt.plot(x, y2)plt.plot(x, y1, color='red', linewidth=1.0, linestyle='--')plt.xlim((-1, 2))plt.ylim((-2, 3))plt.xlabel('I am x')plt.ylabel('I am y')new_ticks = np.linspace(-1, 2, 5)print(new_ticks)plt.xticks(new_ticks)plt.yticks([-2, -1.8, -1, 1.22, 3],[r'$really\ bad$', r'$bad$', r'$normal$', r'$good$', r'$really\ good$'])plt.show()

設(shè)置坐標(biāo)軸–邊框

  • gca():獲取當(dāng)前坐標(biāo)軸信息
  • .spines:設(shè)置邊框
  • .set_color:設(shè)置邊框顏色:默認(rèn)白色
  • .spines:設(shè)置邊框
  • .xaxis.set_ticks_position:設(shè)置x坐標(biāo)刻度數(shù)字或名稱的位置
  • .yaxis.set_ticks_position:設(shè)置y坐標(biāo)刻度數(shù)字或名稱的位置
  • .set_position:設(shè)置邊框位置
x = np.linspace(-3, 3, 50)y1 = 2*x + 1y2 = x**2plt.figure()plt.plot(x, y2)plt.plot(x, y1, color='red', linewidth=1.0, linestyle='--')plt.xlim((-1, 2))plt.ylim((-2, 3))new_ticks = np.linspace(-1, 2, 5)plt.xticks(new_ticks)plt.yticks([-2, -1.8, -1, 1.22, 3],['$really\ bad$', '$bad$', '$normal$', '$good$', '$really\ good$'])ax = plt.gca()#設(shè)置上邊和右邊無邊框ax.spines['right'].set_color('none')ax.spines['top'].set_color('none')plt.show()


調(diào)整坐標(biāo)軸:

ax = plt.gca()#設(shè)置上邊和右邊無邊框ax.spines['right'].set_color('none')ax.spines['top'].set_color('none')#設(shè)置x坐標(biāo)刻度數(shù)字或名稱的位置ax.xaxis.set_ticks_position('bottom')#設(shè)置邊框位置ax.spines['bottom'].set_position(('data', 0))plt.show()

ax = plt.gca()#設(shè)置上邊和右邊無邊框ax.spines['right'].set_color('none')ax.spines['top'].set_color('none')#設(shè)置x坐標(biāo)刻度數(shù)字或名稱的位置ax.xaxis.set_ticks_position('bottom')#設(shè)置邊框位置ax.spines['bottom'].set_position(('data', 0))ax.yaxis.set_ticks_position('left')ax.spines['left'].set_position(('data',0))plt.show()

matplotlib.pyplot.text(x, y, s, fontdict=None, withdash=False, **kwargs)

x, y:表示坐標(biāo);s:字符串文本;fontdict:字典,可選;kw:fontsize=12,horizontalalignment=‘center’、ha=’cener’verticalalignment=’center’、va=’center’
#!/usr/bin/python#coding: utf-8import numpy as npimport matplotlib.pyplot as pltx = np.arange(-10, 11, 1)  #形成一個數(shù)組,第三個參數(shù)表示步長,#start,end,stepy = x ** 2plt.plot(x, y)# 第一個參數(shù)是x軸坐標(biāo)# 第二個參數(shù)是y軸坐標(biāo)# 第三個參數(shù)是要顯式的內(nèi)容# alpha 設(shè)置字體的透明度# family 設(shè)置字體# size 設(shè)置字體的大小# style 設(shè)置字體的風(fēng)格# wight 字體的粗細(xì)# bbox 給字體添加框,alpha 設(shè)置框體的透明度, facecolor 設(shè)置框體的顏色plt.text(-3, 20, "function: y = x * x", size = 15, alpha = 0.2)plt.text(-3, 40, "function: y = x * x", size = 15,         family = "fantasy", color = "r", style = "italic", weight = "light",         bbox = dict(facecolor = "r", alpha = 0.2))plt.show()


添加數(shù)據(jù)標(biāo)簽

# -*- coding: utf-8 -*-import timeimport matplotlib.pyplot as pltdef showResult(xList, yList, title, xLabel, yLabel):    plt.plot(xList, yList, 'g*-')    plt.title(title)    plt.xlabel(xLabel)    plt.ylabel(yLabel)    for x, y in zip(xList, yList):        plt.text(x, y+0.3, str(y), ha='center', va='bottom', fontsize=10.5)    plt.savefig('fig'+str(int(time.time()))+'.jpg')    plt.show()x_arr = [1, 2, 3, 4, 5, 6]y_arr = [1, 4, 9, 16, 25, 36]showResult(x_arr, y_arr, 'title', 'x', 'y')

其中

for x, y in zip(xList, yList):plt.text(x, y+0.3, '%.0f'%y, ha='center', va='bottom', fontsize=10.5)

逐個獲取需要標(biāo)注的點的橫縱坐標(biāo) x與 y,然后在位置 (x, y+0.3) 處以 10.5 的字體顯示出 y 的值,‘center’ 和 ‘bottom’ 分別指水平和垂直方向上的對齊方式。

本站僅提供存儲服務(wù),所有內(nèi)容均由用戶發(fā)布,如發(fā)現(xiàn)有害或侵權(quán)內(nèi)容,請點擊舉報。
打開APP,閱讀全文并永久保存 查看更多類似文章
猜你喜歡
類似文章
小白也能看懂的Matplotlib簡明教程
Python 使用matplotlib畫圖添加標(biāo)注、及移動坐標(biāo)軸位置
matplotlib畫圖系列之設(shè)置坐標(biāo)軸(精度、范圍,標(biāo)簽,中文字符顯示)python3
python matplotlib繪圖設(shè)置坐標(biāo)軸刻度、文本
設(shè)置坐標(biāo)軸刻度的位置和樣式
干貨|教你一文掌握:Matplotlib+Seaborn可視化
更多類似文章 >>
生活服務(wù)
分享 收藏 導(dǎo)長圖 關(guān)注 下載文章
綁定賬號成功
后續(xù)可登錄賬號暢享VIP特權(quán)!
如果VIP功能使用有故障,
可點擊這里聯(lián)系客服!

聯(lián)系客服