我有 matplotlib 圖表的問題,我一直在嘗試多種方法,但似乎無法使其正常作業,我想要一個從左到右的圖表,我的資料如下所示:價格 = [0.03, 0.025, 0.01, 0.01, 0.05, 0.02] 時間 = ['00:00', '00:05', '00:10', '00:15', '00:20', '00:00'] 我希望圖表從左到右,但是當它到達最后一個位置“00:00”時,它又回到了左側。有沒有機會讓它在'00:20'之后創建新的刻度'00:00'?下面是代碼。
import matplotlib.pyplot as plt
import matplotlib.ticker as plticker
stored_prices = [0.03, 0.025, 0.01, 0.01, 0.05, 0.02]
prices_time = ['00:00', '00:05', '00:10', '00:15', '00:20', '00:00']
fig, ax = plt.subplots()
plt.title("X")
plt.tick_params(axis='y', which='both', labelleft=False, labelright=True)
plt.plot(prices_time, stored_prices)
plt.grid()
plt.subplots_adjust(left=0.03, right=0.86)
fig.autofmt_xdate()
loc = plticker.MultipleLocator(base=2) # this locator puts ticks at regular intervals
ax.xaxis.set_major_locator(loc)
print(stored_prices, prices_time)
plt.show() #Preview of chart
這是解釋我的意思的影像,在“00:20”之后,它不會在“00:20”的右側創建“00:00”,而是回傳到已經在左側創建的“00:00”圖的:

uj5u.com熱心網友回復:
由于您的 x 值是等距的(最后一個除外),您可以使用任何等距數字序列來繪制它們。是否以相同的距離顯示最后一個點可能是一個任意決定。
然后你只需要手動設定 x-ticks 并用真實值標記它們:
import matplotlib.pyplot as plt
import matplotlib.ticker as plticker
stored_prices = [0.03, 0.025, 0.01, 0.01, 0.05, 0.02]
prices_time = ['00:00', '00:05', '00:10', '00:15', '00:20', '00:00']
x = range(len(prices_time))
fig, ax = plt.subplots()
plt.title("X")
plt.tick_params(axis='y', which='both', labelleft=False, labelright=True)
plt.plot(x, stored_prices)
plt.grid()
plt.subplots_adjust(left=0.03, right=0.86)
fig.autofmt_xdate()
# loc = plticker.MultipleLocator(base=2)
# this locator puts ticks at regular intervals
# ax.xaxis.set_major_locator(loc)
ax.set_xticks(x)
ax.set_xticklabels(prices_time)
print(stored_prices, prices_time)
plt.show() #Preview of chart

轉載請註明出處,本文鏈接:https://www.uj5u.com/net/317804.html
標籤:Python matplotlib 图表
上一篇:按色調孵化seaborn計數圖
