Matplotlib 网格样式和标题未显示
Matplotlib grid styles and titles not showing
经过大量 运行 尝试让 matplotlib 图显示在特定的 Tkinter 框架中,但网格样式和标题没有显示。情节创建与一个功能相关联,该功能与一个可以正常工作的按钮相关联。当单独绘制图表或调用 plt.show() 时,该样式有效。当整个事物绑定到 class 并以这种方式创建时,它也有效。
有人知道为什么这不起作用吗?
def create_all_graph():
recruiter = submit_entry.get()
# Gather data for marketing calls
rec_filter = df_tgr_year["recruiter"] == recruiter
x_week = df_tgr_year.loc[rec_filter, ["week"]]
# Marketing Calls
mc_y_metric = df_tgr_year.loc[rec_filter, ["marketing_calls"]]
# plot graph
mc_figure = Figure(figsize=(4, 3), dpi=100)
mc_plot = mc_figure.add_subplot(111)
mc_plot.plot(x_week, mc_y_metric, color="#159cff", linewidth=2, marker='h', markerfacecolor="#FFFFFF",
markeredgewidth=2, markersize=5)
mc_canvas = FigureCanvasTkAgg(mc_figure, master=graph_frame)
mc_canvas.draw()
mc_canvas.get_tk_widget().grid(row=1, column=0)
plt.ylim(ymin=0)
plt.xlim(xmin=0)
plt.title("Marketing Calls")
plt.xlabel("Week")
plt.ylabel("Marketing Calls")
plt.minorticks_on()
plt.grid(which="major", linestyle="-", linewidth="0.5", color="#d7eeff")
plt.grid(which="minor", linestyle="-", linewidth="0.5", color="#d7eeff")
plt.tight_layout()
触发这个问题的代码块也很有趣,但让我们下次再谈。
Matplotlib has two interfaces. The first is an object-oriented (OO) interface. In this case, we utilize an instance of axes.Axes in order to render visualizations on an instance of figure.Figure.
The second is based on MATLAB and uses a state-based interface. This is encapsulated in the pyplot module. See the pyplot tutorials for a more in-depth look at the pyplot interface.
您的图表未显示标签和其他样式,因为您使用面向对象的界面进行绘图并使用基于状态的界面设置样式。将所有内容切换到面向对象的界面。
在这里,我已将您的标签切换为面向对象的界面,并将您的代码与 a tutorial to make a minimal reproducible example 相结合。
import tkinter
from matplotlib.backends.backend_tkagg import FigureCanvasTkAgg
from matplotlib.figure import Figure
from matplotlib.axes import Axes
graph_frame = tkinter.Tk()
graph_frame.wm_title("Embedding in Tk")
x_week = [1, 2, 3]
mc_y_metric = [10, 20, 15]
# plot graph
mc_figure = Figure(figsize=(4, 3), dpi=100)
mc_plot: Axes = mc_figure.add_subplot(111)
mc_plot.plot(x_week, mc_y_metric, color="#159cff", linewidth=2, marker='h', markerfacecolor="#FFFFFF",
markeredgewidth=2, markersize=5)
mc_plot.set_title("Marketing Calls")
mc_plot.set_xlabel("Week")
mc_plot.set_ylabel("Marketing Calls")
mc_figure.tight_layout()
mc_canvas = FigureCanvasTkAgg(mc_figure, master=graph_frame)
mc_canvas.draw()
mc_canvas.get_tk_widget().grid(row=1, column=0)
tkinter.mainloop()
经过大量 运行 尝试让 matplotlib 图显示在特定的 Tkinter 框架中,但网格样式和标题没有显示。情节创建与一个功能相关联,该功能与一个可以正常工作的按钮相关联。当单独绘制图表或调用 plt.show() 时,该样式有效。当整个事物绑定到 class 并以这种方式创建时,它也有效。
有人知道为什么这不起作用吗?
def create_all_graph():
recruiter = submit_entry.get()
# Gather data for marketing calls
rec_filter = df_tgr_year["recruiter"] == recruiter
x_week = df_tgr_year.loc[rec_filter, ["week"]]
# Marketing Calls
mc_y_metric = df_tgr_year.loc[rec_filter, ["marketing_calls"]]
# plot graph
mc_figure = Figure(figsize=(4, 3), dpi=100)
mc_plot = mc_figure.add_subplot(111)
mc_plot.plot(x_week, mc_y_metric, color="#159cff", linewidth=2, marker='h', markerfacecolor="#FFFFFF",
markeredgewidth=2, markersize=5)
mc_canvas = FigureCanvasTkAgg(mc_figure, master=graph_frame)
mc_canvas.draw()
mc_canvas.get_tk_widget().grid(row=1, column=0)
plt.ylim(ymin=0)
plt.xlim(xmin=0)
plt.title("Marketing Calls")
plt.xlabel("Week")
plt.ylabel("Marketing Calls")
plt.minorticks_on()
plt.grid(which="major", linestyle="-", linewidth="0.5", color="#d7eeff")
plt.grid(which="minor", linestyle="-", linewidth="0.5", color="#d7eeff")
plt.tight_layout()
触发这个问题的代码块也很有趣,但让我们下次再谈。
Matplotlib has two interfaces. The first is an object-oriented (OO) interface. In this case, we utilize an instance of axes.Axes in order to render visualizations on an instance of figure.Figure.
The second is based on MATLAB and uses a state-based interface. This is encapsulated in the pyplot module. See the pyplot tutorials for a more in-depth look at the pyplot interface.
您的图表未显示标签和其他样式,因为您使用面向对象的界面进行绘图并使用基于状态的界面设置样式。将所有内容切换到面向对象的界面。
在这里,我已将您的标签切换为面向对象的界面,并将您的代码与 a tutorial to make a minimal reproducible example 相结合。
import tkinter
from matplotlib.backends.backend_tkagg import FigureCanvasTkAgg
from matplotlib.figure import Figure
from matplotlib.axes import Axes
graph_frame = tkinter.Tk()
graph_frame.wm_title("Embedding in Tk")
x_week = [1, 2, 3]
mc_y_metric = [10, 20, 15]
# plot graph
mc_figure = Figure(figsize=(4, 3), dpi=100)
mc_plot: Axes = mc_figure.add_subplot(111)
mc_plot.plot(x_week, mc_y_metric, color="#159cff", linewidth=2, marker='h', markerfacecolor="#FFFFFF",
markeredgewidth=2, markersize=5)
mc_plot.set_title("Marketing Calls")
mc_plot.set_xlabel("Week")
mc_plot.set_ylabel("Marketing Calls")
mc_figure.tight_layout()
mc_canvas = FigureCanvasTkAgg(mc_figure, master=graph_frame)
mc_canvas.draw()
mc_canvas.get_tk_widget().grid(row=1, column=0)
tkinter.mainloop()