我搜索了多篇帖子,其中的解决方案可能不相关,也可能不相关,但仍然尝试过,但无法这样做。
我当前的绘图确实绘制了该数字,但它没有显示该年的所有值,我希望它是一个整数。
我的 matplotlib 代码是:
import matplotlib.pyplot as plt
year, value = 0, 0.0
year = [1997,1998,1999,2000,2001,2002,2003,2004,2005,2006,2007,2008,2009,2010,2011,2012,2013,2014, 2015, 2016]
value = [26242.5,27906.7,29192.7,30407.5,1564.9,2313.5,2989.4,3668.6,22082.6,29964.2,30481.6,35981.7,39597.6,38659.6, 34325.9,6913.5,35780.6,39111.5,37116.5,35385.5]
plt.plot(year, value)
plt.title('A - Value of apples vs Year')
plt.xlabel('Year')
plt.ylabel('Value of apples')
plt.grid(True)
plt.show()
您可以将
years
列表指定为您期望的 xticks,方法是将它们传递给 plt.xticks
docs。我也随意使用 rotation=45
旋转文本,否则刻度标签会重叠。
虽然这不是你问题的一部分,但我会注意到,
year, value = 0, 0.0
行几乎肯定没有达到你的预期。看起来您想要初始化将由年份和值变量引用的每个列表的类型(int 和 float),但这不是必要的或不可能的。对两个列表的重新分配只会覆盖您原来的分配。如果我误解了你的意图,请继续纠正我。
import matplotlib.pyplot as plt
#year, value = 0, 0.0 -> this line is not doing anything
year = [1997,1998,1999,2000,2001,2002,2003,2004,2005,2006,2007,2008,2009,2010,2011,2012,2013,2014, 2015, 2016]
value = [26242.5,27906.7,29192.7,30407.5,1564.9,2313.5,2989.4,3668.6,22082.6,29964.2,30481.6,35981.7,39597.6,38659.6, 34325.9,6913.5,35780.6,39111.5,37116.5,35385.5]
plt.plot(year, value)
####
plt.xticks(year, rotation=45)
####
plt.title('A - Value of apples vs Year')
plt.xlabel('Year')
plt.ylabel('Value of apples')
plt.grid(True)
plt.show()
根据上述更改绘制的图形:
我制作了一个用户界面,用户可以在其中输入数字,但是当我尝试绘制累积成本时,为什么它显示我的第 0 年价值成本为 0 美元,而不是为用户输入的初始资本支出成本。这是代码:
CostComparisonApp 类(tk.Toplevel): def init(自身,父级): super().init(父级) self.parent =parent # 存储对父窗口的引用 self.title('成本对比分析') self.create_cost_comparison_interface()
def create_cost_comparison_interface(self):
# Define the user interface for cost comparison
ttk.Label(self, text='Truck-Shovel System CAPEX ($):').grid(row=0, column=0)
ttk.Label(self, text='SMIPCC System CAPEX ($):').grid(row=0, column=1)
self.capex_ts_entry = ttk.Entry(self)
self.capex_smipcc_entry = ttk.Entry(self)
self.capex_ts_entry.grid(row=1, column=0)
self.capex_smipcc_entry.grid(row=1, column=1)
ttk.Label(self, text='Truck-Shovel System OPEX ($/hr): ').grid(row=2, column=0)
ttk.Label(self, text=' SMIPCC System OPEX ($/hr): ').grid(row=2, column=1)
self.opex_ts_entry = ttk.Entry(self)
self.opex_smipcc_entry = ttk.Entry(self)
self.opex_ts_entry.grid(row=3, column=0)
self.opex_smipcc_entry.grid(row=3, column=1)
# Additional inputs
ttk.Label(self, text='Working Hours per Year:').grid(row=4, column=0)
self.work_hours_entry = ttk.Entry(self)
self.work_hours_entry.grid(row=4, column=1)
ttk.Label(self, text='Discount Rate (%):').grid(row=5, column=0)
self.discount_rate_entry = ttk.Entry(self)
self.discount_rate_entry.grid(row=5, column=1)
ttk.Label(self, text='Mine Life (years):').grid(row=6, column=0)
self.mine_life_entry = ttk.Entry(self)
self.mine_life_entry.grid(row=6, column=1)
# Input fields for equipment replacement years
ttk.Label(self, text='Truck-Shovel Replacement Years (comma-separated):').grid(row=7, column=0)
self.ts_replacement_years_entry = ttk.Entry(self)
self.ts_replacement_years_entry.grid(row=7, column=1)
ttk.Label(self, text='SMIPCC Replacement Years (comma-separated):').grid(row=8, column=0)
self.smipcc_replacement_years_entry = ttk.Entry(self)
self.smipcc_replacement_years_entry.grid(row=8, column=1)
# Plot button
ttk.Button(self, text='Plot Costs', command=self.plot_costs).grid(row=9, column=0, columnspan=2)
# Buttons for calculation and going back
#ttk.Button(self, text='Calculate and Plot', command=self.plot_costs).grid(row=10, column=0, columnspan=2)
ttk.Button(self, text='Back', command=self.go_back).grid(row=30, column=0, columnspan=2)
def plot_costs(self):
# Retrieve values from entries
capex_ts = float(self.capex_ts_entry.get())
opex_ts = float(self.opex_ts_entry.get())
capex_smipcc = float(self.capex_smipcc_entry.get())
opex_smipcc = float(self.opex_smipcc_entry.get())
work_hours = float(self.work_hours_entry.get())
discount_rate = float(self.discount_rate_entry.get()) / 100
mine_life = int(self.mine_life_entry.get())
# Initialize cumulative cost arrays with initial CAPEX values entered by the user
ts_cumulative_costs = [capex_ts]
smipcc_cumulative_costs = [capex_smipcc]
# Calculate cumulative costs for each year, starting from year 1
for year in range(1, mine_life + 1):
discount_factor = (1 + discount_rate) ** year
# Calculate yearly discounted OPEX
discounted_opex_ts = opex_ts * work_hours * discount_factor
discounted_opex_smipcc = opex_smipcc * work_hours * discount_factor
# Update cumulative costs
ts_cumulative_cost = ts_cumulative_costs[-1] + capex_ts - discounted_opex_ts
smipcc_cumulative_cost = smipcc_cumulative_costs[-1] + capex_smipcc - discounted_opex_smipcc
ts_cumulative_costs.append(ts_cumulative_cost)
smipcc_cumulative_costs.append(smipcc_cumulative_cost)
# Retrieve user input for equipment replacement years from the UI
ts_replacement_years_str = self.ts_replacement_years_entry.get()
smipcc_replacement_years_str = self.smipcc_replacement_years_entry.get()
ts_replacement_years = [int(year) for year in ts_replacement_years_str.split(',') if year.strip()]
smipcc_replacement_years = [int(year) for year in smipcc_replacement_years_str.split(',') if year.strip()]
# Add replacement costs for the specified years and add opex to the total cost
for year in ts_replacement_years:
if 1 <= year <= mine_life:
replacement_cost = capex_ts * (1 + discount_rate) ** year
ts_cumulative_costs[year] -= replacement_cost
ts_cumulative_costs[year] -= discounted_opex_ts # Add opex to total cost for replacement year
for year in smipcc_replacement_years:
if 1 <= year <= mine_life:
replacement_cost = capex_smipcc * (1 + discount_rate) ** year
smipcc_cumulative_costs[year] -= replacement_cost
smipcc_cumulative_costs[year] -= discounted_opex_smipcc # Add opex to total cost for replacement year
# Create a figure for the plot
fig = Figure(figsize=(6, 4), dpi=100)
plot = fig.add_subplot(1, 1, 1)
# Plotting the cumulative costs, including year 0
plot.plot(range(mine_life + 1), ts_cumulative_costs, label='Truck-Shovel System')
plot.plot(range(mine_life + 1), smipcc_cumulative_costs, label='SMIPCC System')
plot.set_xlabel('Year')
plot.set_ylabel('Cumulative Cost ($ M)')
plot.set_title('Cumulative Cost Comparison Over Mine Life')
plot.legend()
plot.grid(True)
# Embedding the plot in the Tkinter window
canvas = FigureCanvasTkAgg(fig, master=self) # 'self' refers to the Tkinter window
canvas_widget = canvas.get_tk_widget()
canvas_widget.grid(row=12, column=0, columnspan=2)
canvas.draw()