python matplotlib.pyplot.xticks() yticks() (设置x或y轴对应显示的标签)
生活随笔
收集整理的這篇文章主要介紹了
python matplotlib.pyplot.xticks() yticks() (设置x或y轴对应显示的标签)
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
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_likeA list of positions at which ticks should be placed. You can pass an empty list to disable xticks.應當放置刻度的位置列表。 您可以傳遞一個空列表來禁用xticks。labels : array_like, optionalA 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-------locsAn array of label locations.labelsA 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 : sxana@qq.com @Software: PyCharm """ import re from decimal import Decimalimport numpy as np import matplotlib.pyplot as pltkeyword_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()結果:
參考文章1:python matplotlib Text類
參考文章2:Python利用matplotlib.pyplot繪圖時如何設置坐標軸刻度
總結
以上是生活随笔為你收集整理的python matplotlib.pyplot.xticks() yticks() (设置x或y轴对应显示的标签)的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: numpy np.sort()函数(指定
- 下一篇: python matplotlib绘图