日本免费高清视频-国产福利视频导航-黄色在线播放国产-天天操天天操天天操天天操|www.shdianci.com

學(xué)無(wú)先后,達(dá)者為師

網(wǎng)站首頁(yè) 編程語(yǔ)言 正文

python實(shí)現(xiàn)plt?x軸坐標(biāo)按1刻度顯示_python

作者:Dragon水魅 ? 更新時(shí)間: 2022-09-06 編程語(yǔ)言

plt x軸坐標(biāo)按1刻度顯示

import matplotlib.pyplot as plt
from matplotlib.pyplot import MultipleLocator

x_major_locator = MultipleLocator(1)
ax = plt.gca()
ax.xaxis.set_major_locator(x_major_locator)  # x軸按1刻度顯示

# ls_num是你已有的列表數(shù)據(jù),存放縱坐標(biāo)內(nèi)容
plt.plot(ls_num)  # y軸變量
plt.ylabel('count')  # y軸名字
plt.show()

matplotlib設(shè)置坐標(biāo)軸

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

在matplotlib中包含了很多函數(shù),用來(lái)對(duì)這些參數(shù)進(jìn)行設(shè)置。

  • plt.xlim、plt.ylim 設(shè)置橫縱坐標(biāo)軸范圍
  • plt.xlabel、plt.ylabel 設(shè)置坐標(biāo)軸名稱
  • plt.xticksplt.yticks設(shè)置坐標(biāo)軸刻度

以上plt表示matplotlib.pyplot

例子

#導(dǎo)入包
import matplotlib.pyplot as plt
import numpy as np

#創(chuàng)建數(shù)據(jù)
x = np.linspace(-5, 5, 100)
y1 = np.sin(x)
y2 = np.cos(x)

#創(chuàng)建figure窗口
plt.figure(num=3, figsize=(8, 5))
#畫曲線1
plt.plot(x, y1)
#畫曲線2
plt.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)
my_y_ticks = np.arange(-2, 2, 0.3)
plt.xticks(my_x_ticks)
plt.yticks(my_y_ticks)

#顯示出所有設(shè)置
plt.show()

結(jié)果

這里寫圖片描述

原文鏈接:https://blog.csdn.net/qq_43650934/article/details/107084429

欄目分類
最近更新