python matplotlib.pyplot.xticks() yticks() (设置x或y轴对应显示的标签)
程序员文章站
2022-03-10 21:24:03
...
from matplotlib\pyplot.py
def xticks(ticks=None, labels=None, **kwargs):
"""
Get or set the current tick locations and labels of the x-axis.
获取或设置x轴的当前刻度位置和标签。
Call signatures::
locs, labels = xticks() # Get locations and labels
获取位置和标签
xticks(ticks, [labels], **kwargs) # Set locations and labels
设置位置和标签
Parameters
----------
ticks : array_like
A list of positions at which ticks should be placed. You can pass an empty list to disable xticks.
应当放置刻度的位置列表。 您可以传递一个空列表来禁用xticks。
labels : array_like, optional
A list of explicit labels to place at the given *locs*.
放置在给定* locs *处的显式标签的列表。
**kwargs
:class:`.Text` properties can be used to control the appearance of the labels.
Text属性可以用来控制标签的外观。
Returns
-------
locs
An array of label locations.
labels
A list of `.Text` objects.
Notes
-----
Calling this function with no arguments (e.g. ``xticks()``) is the pyplot equivalent of calling `~.Axes.get_xticks` and `~.Axes.get_xticklabels` on the current axes.
Calling this function with arguments is the pyplot equivalent of calling `~.Axes.set_xticks` and `~.Axes.set_xticklabels` on the current axes.
pyplot等效于在当前轴上调用`〜.Axes.get_xticks`和`〜.Axes.get_xticklabels`的不带参数的函数(例如``xticks()``)。
使用参数调用此函数的pyplot等效于在当前轴上调用〜.Axes.set_xticks和〜.Axes.set_xticklabels。
Examples
--------
Get the current locations and labels:
>>> locs, labels = xticks()
Set label locations:
>>> xticks(np.arange(0, 1, step=0.2))
Set text labels:
>>> xticks(np.arange(5), ('Tom', 'Dick', 'Harry', 'Sally', 'Sue'))
Set text labels and properties:
>>> xticks(np.arange(12), calendar.month_name[1:13], rotation=20)
Disable xticks:
>>> xticks([])
"""
示例
plt.xticks(time_extract, time_string, rotation=45, fontsize=6, verticalalignment='top', fontweight='light')
# -*- coding: utf-8 -*-
"""
@File : 20200226_绘制平均识别数-时间图.py
@Time : 2020/2/26 10:04
@Author : Dontla
@Email : aaa@qq.com
@Software: PyCharm
"""
import re
from decimal import Decimal
import numpy as np
import matplotlib.pyplot as plt
keyword_20200225 = {'15:41': (10900, 16.94), '16:23': (11000, 16.98), '16:30': (8640, 16.40), '16:32': (7483, 16.88),
'16:39': (6482, 16.96), '16:44': (6026, 16.08), '16:55': (5887, 16.22), '16:57': (5190, 15.64),
'17:00': (4236, 15.80),
'17:05': (3153, 15.78), '17:08': (2550, 15.30), '17:10': (2016, 15.00), '17:16': (1790, 15.30),
'17:21': (2755, 16.16), '17:24': (3300, 16.84),
'17:28': (2929, 14.88), '17:32': (2507, 16.82), '17:38': (2484, 16.56), '17:47': (3018, 16.96),
'17:50': (2857, 16.94), '17:57': (2387, 16.52),
'18:00': (2012, 15.86), '18:04': (1663, 15.82), '18:10': (1200, 16.04), '18:14': (967, 15.64),
'18:18': (748, 14.98), '18:21': (623, 14.80),
'18:24': (485, 14.14), '18:26': (399, 13.08), '18:29': (316, 13.68), '18:31': (251, 13.36),
'18:33': (223, 12.96), '18:35': (200, 12.88),
'18:37': (160, 12.40), '18:38': (144, 11.96), '18:39': (129, 11.56), '18:40': (107, 11.02),
'18:41': (89, 11.10), '18:42': (72, 11.08),
'18:43': (59, 12.18), '18:44': (45, 11.22), '18:45': (38, 11.46), '18:46': (31, 11.26),
'18:47': (26, 10.68), '18:48': (21, 8.84),
'18:49': (15, 6.72), '18:50': (11, 5.60), '18:51': (9, 2.40), '18:52': (7, 0.56),
'18:53': (5, 0.00)}
time_string = []
time_extract = []
Illumination_extract = []
mean_dectect_num = []
for key in keyword_20200225:
time_string.append(key)
# 貌似用这个比re.findall()提取数字好用?
hour, minute = np.fromstring(key, dtype=int, sep=':')
time_extract.append(round(hour + minute / 60, 2))
Illumination_extract.append(keyword_20200225[key][0])
mean_dectect_num.append(keyword_20200225[key][1])
# print(time_string)
# ['15:41', '16:23', '16:30', '16:32', '16:39', '16:44', '16:55', '16:57', '17:00', '17:05', '17:08', '17:10', '17:16', '17:21', '17:24', '17:28', '17:32', '17:38', '17:47', '17:50', '17:57', '18:00', '18:04', '18:10', '18:14', '18:18', '18:21', '18:24', '18:26', '18:29', '18:31', '18:33', '18:35', '18:37', '18:38', '18:39', '18:40', '18:41', '18:42', '18:43', '18:44', '18:45', '18:46', '18:47', '18:48', '18:49', '18:50', '18:51', '18:52', '18:53']
# print(time_extract)
# [15.68, 16.38, 16.5, 16.53, 16.65, 16.73, 16.92, 16.95, 17.0, 17.08, 17.13, 17.17, 17.27, 17.35, 17.4, 17.47, 17.53, 17.63, 17.78, 17.83, 17.95, 18.0, 18.07, 18.17, 18.23, 18.3, 18.35, 18.4, 18.43, 18.48, 18.52, 18.55, 18.58, 18.62, 18.63, 18.65, 18.67, 18.68, 18.7, 18.72, 18.73, 18.75, 18.77, 18.78, 18.8, 18.82, 18.83, 18.85, 18.87, 18.88]
# print(Illumination_extract)
# [10900, 11000, 8640, 7483, 6482, 6026, 5887, 5190, 4236, 3153, 2550, 2016, 1790, 2755, 3300, 2929, 2507, 2484, 3018, 2857, 2387, 2012, 1663, 1200, 967, 748, 623, 485, 399, 316, 251, 223, 200, 160, 144, 129, 107, 89, 72, 59, 45, 38, 31, 26, 21, 15, 11, 9, 7, 5]
# 还有一种优雅一点的方法:
# time_extract = [round(np.fromstring(key, dtype=int, sep=':')[0] + np.fromstring(key, dtype=int, sep=':')[1] / 60, 2) for
# key in keyword_20200225]
# print(time_extract)
# [15.68, 16.38, 16.5, 16.53, 16.65, 16.73, 16.92, 16.95, 17.0, 17.08, 17.13, 17.17, 17.27, 17.35, 17.4, 17.47, 17.53, 17.63, 17.78, 17.83, 17.95, 18.0, 18.07, 18.17, 18.23, 18.3, 18.35, 18.4, 18.43, 18.48, 18.52, 18.55, 18.58, 18.62, 18.63, 18.65, 18.67, 18.68, 18.7, 18.72, 18.73, 18.75, 18.77, 18.78, 18.8, 18.82, 18.83, 18.85, 18.87, 18.88]
# 绘制光照度散点图
plot1 = plt.plot(time_extract, Illumination_extract, '*', label='Illumination/lx')
# 绘制平均识别个数散点图
plot2 = plt.plot(time_extract, [i * 700 for i in mean_dectect_num], 'o', label='mean dectect num × 7 × 10^2/pcs')
plt.xticks(time_extract, time_string, rotation=45, fontsize=6, verticalalignment='top', fontweight='light')
# 限制绘制上下限
# plt.ylim(-100, 12000)
plt.xlabel('Time')
plt.ylabel('Illumination & mean detect num')
plt.legend(loc=3) # 指定legend的位置,读者可以自己help它的用法
plt.title('2020/02/25 cloudy day Scatter')
plt.show()
结果: