我制作了一個 2 通道資料記錄器,并希望以“人性化”的方式繪制生成的 csv。csv 檔案是這樣的:
hh:mm,m,ch1 , ch2 ,--
15:24,0,61.5,66.0
15:25,1,61.1,66.0
15:26,2,60.0,65.0
15:27,3,58.5,63.0
15:29,4,57.7,62.0
15:30,5,57.2,62.0
15:31,6,55.6,60.0
... ...
代碼是:
import matplotlib.pyplot as plt
import csv
x = []
y1= []
y2 = []
with open('Documentos/valores2.csv','r') as csvfile:
lines = csv.reader(csvfile, delimiter=',')
last = ""
ini = ""
for row in lines:
if len(row) != 4: continue
if ini =="": ini = row[0]
if row[0] == last:
continue
last = row[0]
x.append(int(ini[:2]) (int(row[1]) int(ini[3:5]) ) /60)
y1.append(float(row[2]))
y2.append(float(row[3]))
fig, ax = plt.subplots(1, figsize=(8, 6))
fig.suptitle(' Documentos/valores.csv\nde ' ini[:-1] " a " last[:-1], fontsize = 14)
ax.plot(x, y1, color="red", label="cantero 1")
ax.plot(x, y2, color="green", label="cantero 2")
plt.legend(loc="lower right", title="", frameon=False)
plt.xlabel('hora')
plt.show()
結果:

我想要小時“1”和“6”而不是小時“25”和“30”......!
uj5u.com熱心網友回復:

您擁有無法更改的橫坐標(20、25、30 等),除非您想打破測量順序,因此您需要更改這些數字的顯示方式,并且必須使用matplotlib.ticker.FuncFormatter.
當我們這樣做的時候,我建議也MultipleLocator從同一個模塊中使用,這樣小時數就像小時一樣,并且還引入了次要刻度。
import numpy as np
import matplotlib.pyplot as plt
from matplotlib.ticker import FuncFormatter, MultipleLocator
# we take the hour, modulo 24, and we format starting with 00
formatter24 = FuncFormatter(lambda t,_:".2d"%(t%24))
t = np.linspace(15.5, 38, 301)
y = 12 3*np.sin(6.28*t/20) t
fig, ax = plt.subplots()
ax.plot(t, y)
# change the formatters
ax.xaxis.set_major_formatter(formatter24)
ax.xaxis.set_major_formatter(formatter24)
# change the locators
ax.xaxis.set_major_locator(MultipleLocator(6))
ax.xaxis.set_minor_locator(MultipleLocator(1))
# tailor the minor ticks
ax.xaxis.set_tick_params(which='minor', labelsize=6, colors='gray')
plt.show()
uj5u.com熱心網友回復:
以更簡單的方式,我們可以格式化向量 x 本身:
... ...
formatter24 = lambda x,n : "%.2f"%(x%24)
fig, ax = plt.subplots()
ax.xaxis.set_major_locator(MultipleLocator(1))
ax.xaxis.set_major_formatter(formatter24)
ax.xaxis.set_minor_locator(MultipleLocator(1/6))
ax.set_xlim(int(x[0]), 1 int(x[-1]))
... ...
轉載請註明出處,本文鏈接:https://www.uj5u.com/yidong/413684.html
標籤:
下一篇:通過Java執行python腳本
