plt x轴坐标按1刻度显示
1
2
3
4
5
6
7
8
9
10
11
|
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是你已有的列表数据,存放纵坐标内容 plt.plot(ls_num) # y轴变量 plt.ylabel( 'count' ) # y轴名字 plt.show() |
matplotlib设置坐标轴
在使用matplotlib模块时画坐标图时,往往需要对坐标轴设置很多参数,这些参数包括横纵坐标轴范围、坐标轴刻度大小、坐标轴名称等
在matplotlib中包含了很多函数,用来对这些参数进行设置。
-
plt.xlim
、plt.ylim
设置横纵坐标轴范围 -
plt.xlabel
、plt.ylabel
设置坐标轴名称 -
plt.xticks
、plt.yticks
设置坐标轴刻度
以上plt表示matplotlib.pyplot
例子
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
|
#导入包 import matplotlib.pyplot as plt import numpy as np #创建数据 x = np.linspace( - 5 , 5 , 100 ) y1 = np.sin(x) y2 = np.cos(x) #创建figure窗口 plt.figure(num = 3 , figsize = ( 8 , 5 )) #画曲线1 plt.plot(x, y1) #画曲线2 plt.plot(x, y2, color = 'blue' , linewidth = 5.0 , linestyle = '--' ) #设置坐标轴范围 plt.xlim(( - 5 , 5 )) plt.ylim(( - 2 , 2 )) #设置坐标轴名称 plt.xlabel( 'xxxxxxxxxxx' ) plt.ylabel( 'yyyyyyyyyyy' ) #设置坐标轴刻度 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) #显示出所有设置 plt.show() |
结果
以上为个人经验,希望能给大家一个参考,也希望大家多多支持服务器之家。
原文链接:https://blog.csdn.net/qq_43650934/article/details/107084429