我用python中的csv文件创建了一个熊猫数据框。如何在tkinter窗口中显示此数据框。
import pandas as pd
methods = pd.read_csv('metode.csv')
methods = methods.drop(methods.columns[[0]], axis=1)
答案 0 :(得分:0)
这里是创建具有平方根的csv文件的示例;读取csv并将其绘制在tkinter中。要捕获matplotlib图,您必须使用FigureCanvasTkAgg
并将mpl图与带有ax=tk_ax
的tkinter图链接起来,如下例所示。
import pandas as pd
import tkinter as tk
import matplotlib.pyplot as plt
from matplotlib.backends.backend_tkagg import FigureCanvasTkAgg
# create the csv file with square roots
squareroots = [(i, i**0.5) for i in range(0, 100)]
squareroot_df = pd.DataFrame(squareroots, columns=['i', 'i^0.5'])
squareroot_df.to_csv('temp_squareroots.csv', index=False)
# set up the tkinter GUI
root = tk.Tk()
fig, tk_ax = plt.subplots(figsize=(10, 10))
mpl_plot = FigureCanvasTkAgg(fig, root)
mpl_plot.get_tk_widget().pack()
# read the csv file
square_roots_df = pd.read_csv('temp_squareroots.csv')
# plot the values in the data frame and invert the x-axis
mpl_ax = square_roots_df.plot(x='i', y='i^0.5', color='firebrick', ax=tk_ax)
mpl_ax.invert_xaxis()
root.mainloop()
在任何情况下,只需在matplotlib中进行绘图都容易得多:
import pandas as pd
import matplotlib.pyplot as plt
squareroots = [(i, i**0.5) for i in range(0, 100)]
squareroot_df = pd.DataFrame(squareroots, columns=['i', 'i^0.5'])
squareroot_df.to_csv('temp_squareroots.csv', index=False)
square_roots_df = pd.read_csv('temp_squareroots.csv')
ax = square_roots_df.plot(x='i', y='i^0.5', color='firebrick')
ax.invert_xaxis()
plt.show()
注意,我添加了mpl_ax.invert_xaxis()
只是为了显示您可以调整操作mpl_ax
对象的绘图。 (请参见Matplotlib documentation)