亚洲在线久爱草,狠狠天天香蕉网,天天搞日日干久草,伊人亚洲日本欧美

為了賬號安全,請及時綁定郵箱和手機立即綁定
已解決430363個問題,去搜搜看,總會有你想問的

x軸python格式上的日期標簽

x軸python格式上的日期標簽

慕婉清6462132 2022-08-25 14:07:46
我的數據看起來像這樣01.03.20    1002.03.20    1004.03.20    1505.03.20    16我想繪制與值的對比,我希望的格式類似于...datesyxaxisMar 01Mar 02Mar 03這是我的代碼:fig, ax = plt.subplots()ax.scatter(x, y, s=100, c='C0')ax.plot(x, y, ls='-', c='C0')# Set the locatorlocator = mdates.MonthLocator()  # every month# Specify the format - %b gives us Jan, Feb...fmt = mdates.DateFormatter('%b-%d')X = plt.gca().xaxisX.set_major_locator(locator)# Specify formatterX.set_major_formatter(fmt)ax.xaxis.set_tick_params(rotation=30)有問題,如 、 ,并且沒有顯示。我如何更改的格式以顯示月份和日期,例如:...x-axisxticksxlabelxlabelMar 01Mar 02Mar 03
查看完整描述

1 回答

?
qq_笑_17

TA貢獻1818條經驗 獲得超7個贊

1)我假設你的軸包含,而不是。然后,在繪圖之前,我會將其轉換如下。xstringdatetime


x=[datetime.strptime(xi, "%d.%m.%y") for xi in x]

2)如果您選擇,則無法將其設置為3月01...因此,將其切換為 .MonthLocatorDayLocator


locator = mdates.DayLocator()

3)這個是可選的,有更清晰的代碼。您不需要 .X


ax.xaxis.set_major_locator(locator)

ax.xaxis.set_major_formatter(fmt)

ax.xaxis.set_tick_params(rotation=30)

示例代碼在這里。


import numpy as np

import matplotlib.pyplot as plt

import matplotlib.dates as mdates

from datetime import datetime


x=["01.03.20", "02.03.20", "04.03.20", "05.03.20"]

x=[datetime.strptime(xi, "%d.%m.%y") for xi in x]

y=[10, 10, 15,16]


fig, ax = plt.subplots()

ax.scatter(x, y, s=100, c='C0')

ax.plot(x, y, ls='-', c='C0')


locator = mdates.DayLocator() 

fmt = mdates.DateFormatter('%b-%d')


ax.xaxis.set_major_locator(locator)

ax.xaxis.set_major_formatter(fmt)

ax.xaxis.set_tick_params(rotation=30)

ax.set_xlim(x[0],x[3])


plt.show()

示例結果在此處。

http://img1.sycdn.imooc.com//630711ce00014d0506550477.jpg

查看完整回答
反對 回復 2022-08-25
  • 1 回答
  • 0 關注
  • 105 瀏覽
慕課專欄
更多

添加回答

舉報

0/150
提交
取消
微信客服

購課補貼
聯系客服咨詢優惠詳情

幫助反饋 APP下載

慕課網APP
您的移動學習伙伴

公眾號

掃描二維碼
關注慕課網微信公眾號