我有一個運行一些硬體并不斷生成資料的樹莓派。每天,我都會在 pandas 資料框中收集資料,然后它會發送一封摘要電子郵件。該電子郵件需要包含一個漂亮的圖表,顯示一段時間內的資料。在我的主機(最新的 MacOS)上測驗效果很好。但是,pi 輸出空白圖表。坐標軸、標簽、顏色以及除繪圖本身之外的所有內容。只是一個空圖表。兩臺機器都運行 matplotlib 3.5.1。請幫我弄清楚為什么這些圖沒有在一臺機器上渲染,但在另一臺機器上就很好。
#!/usr/bin/env python
import dill
import pandas
import datetime
from datetime import timedelta
import matplotlib.pyplot as plt
import matplotlib.dates as mdates
import subprocess
class Report():
def __init__(self, datafile):
# Saved dataframes
self.file = datafile
def runReport(self):
# Open data
saveIn = open(self.file, 'rb')
data = dill.load(saveIn)
# Close file
saveIn.close()
# Alias dataframe
systemData = data['systemReadings']
# Declare chart, set output size
fig = plt.figure(figsize = (14, 8.75))
## Plot1
# Create plot1 plot sharing an X-axis with Plot5
plot1 = fig.add_subplot()
# Display y-axis labels alongside Plot5
plot1.yaxis.tick_left()
# Display tick labels to left of tick line
rspine = plot1.spines['left']
# Display y-axis label to the left of the chart
plot1.yaxis.set_label_position("left")
# Y-axis range
plt.ylim((7.0, 8.5))
# Divide y-axis into 10 ticks
plt.locator_params(axis = 'y', nbins = 10)
# Limit x-axis to 00:00 - 23:59 range
plot1.set_xlim([datetime.date(2022,3,6), datetime.date(2022,3,7)])
# Link data and color line
plot1.plot(systemData['Plot1'], color = 'k', label = 'Plot1')
# Shares scale and label with Plot5
## Plot2
# Create Plot2 plot on X-axis with plot1
plot2 = plot1.twinx()
# Display y-axis labels to the right of scale line
rspine = plot2.spines['right']
# Adjust location of axis/labels so they're not on top of the other dataset sharing that side of the chart
rspine.set_position(('axes', 1.05))
# Y-axis range
plt.ylim((-0.05, 1))
# Divide y-axis into 10 tickmarks
plt.locator_params(axis = 'y', nbins = 10)
# Link data and line color
plot2.plot(systemData['Plot2'], color = 'orange', label = 'Plot2')
# Label and label color
plot2.set_ylabel('Plot2', color = 'orange')
## Plo3
# Create Estimated Plot3 plot on same X-axis with plot1
plot3 = plot1.twinx()
# Display ticks on left side of chart
plot3.yaxis.tick_left()
# Display tick labels to left of tick line
rspine = plot3.spines['left']
# Display y-axis label to the left of the chart
plot3.yaxis.set_label_position("left")
# Adjust location of axis/labels so they're not on top of the other dataset sharing that side of the chart
rspine.set_position(('axes', -0.05))
# Y-axis range
plt.ylim((-2, 2))
# Divide y-axis into 20 tick marks
plt.locator_params(axis = 'y', nbins = 20)
# Link data and color line
plot3.plot(systemData['Plot3'], color = 'limegreen', label = 'Plot3')
# Label and label color
plot3.set_ylabel('Plot3', color = 'limegreen')
## Plot4
# Create Plot4 sharing an X-axis with plot1 plot
plot4 = plot1.twinx()
# Display y-axis labels to the right of scale line
rspine = plot4.spines['right']
# Y-axis range
plt.ylim((-0.05, 0.5))
# Divide y-axis in to 10 ticks
plt.locator_params(axis = 'y', nbins = 10)
# Link data and color line
plot4.plot(systemData['Plot4'], color = 'r', label = 'Plot4')
# Label and label color
plot4.set_ylabel('Plot4', color = 'b')
## plot5
# Create Plot5 sharing an X-axis with plot1 plot
plot5 = plot1.twinx()
# Display ticks on left side of chart
plot5.yaxis.tick_left()
# Display tick labels to left of tick line
rspine = plot3.spines['left']
# Display y-axis label to the left of the chart
plot5.yaxis.set_label_position("left")
# Adjust location of axis/labels so they're not on top of the other dataset sharing that side of the chart
rspine.set_position(('axes', -0.05))
# Y-axis range
plt.ylim((7.0, 8.5))
# Display y-axis grid lines
plot5.yaxis.grid()
# Divide y-axis into 10 ticks
plt.locator_params(axis = 'y', nbins = 10)
# Link data and color line
# Raw
plot5.plot(systemData['Plot5'], color = 'r', label = 'Plot5')
# Label and label color
plot5.set_ylabel('Plot5', color = 'r')
## Overall chart formatting
# Format x-axis hour labels
plt.gca().xaxis.set_major_formatter(mdates.DateFormatter('%I:%M %p'))
# Only tick on each hour
plt.gca().xaxis.set_major_locator(mdates.HourLocator(interval = 1))
# Display labels at an angle for space
fig.autofmt_xdate()
# Place legend below chart
fig.legend(loc = 'lower center', ncol = 5)
# Display final chart
plt.show()
report = Report()
report.runReport()
uj5u.com熱心網友回復:
這一行:
# Limit x-axis to 00:00 - 23:59 range
plot1.set_xlim([datetime.date(2022,3,6), datetime.date(2022,3,7)])
由于使用具有該日期資料的示例資料集進行原型設計,然后由于未能完全集成而部分失敗。自我鞭笞將是短暫的。
轉載請註明出處,本文鏈接:https://www.uj5u.com/houduan/447169.html
標籤:Python 苹果系统 matplotlib 树莓派4
