在tkinter GUI中绘制Pandas DataFrame,并设置辅助轴和参数

epggiuax  于 2023-04-28  发布在  其他
关注(0)|答案(1)|浏览(152)

我有一个DataFrame,我想在一个图中绘制它的两列。我的代码如下:

x = df_Data_7370(start_date, end_date)
fig, ax = plt.subplots(figsize=(5,5))
x.plot(x="Diff_Rollkreis", y="Beugemoment", yticks=[0,2,4,6,8,10], 
xlabel='Diff. Rollkreis-Ø [mm]', ylabel='Beugemoment [Nm]', style='o', color='red', markersize=2, ax=ax)
x.plot(x="Diff_Rollkreis", y="Axialspiel", style='o', color='blue', markersize=2, ax=ax, secondary_y=True)
canvas = FigureCanvasTkAgg(fig, master = master)

canvas.get_tk_widget().place(x=235,y=0)

canvas.draw()

我的问题是-我不知道如何命名第二个y轴-我不知道如何设置主轴和第二个轴的ylim和yticks。我怎么能说python,主轴和第二个轴的ylim和yticks是不同的?
谢谢你帮助

x = df_Data_7370(start_date, end_date)
fig, ax = plt.subplots(figsize=(5,5))
x.plot(x="Diff_Rollkreis", y="Beugemoment", yticks=[0,2,4,6,8,10], xlabel='Diff. Rollkreis-Ø [mm]', ylabel='Beugemoment [Nm]', style='o', color='red', markersize=2, ax=ax)
    x.plot(x="Diff_Rollkreis", y="Axialspiel", style='o', color='blue', markersize=2, ax=ax, secondary_y=True)
canvas = FigureCanvasTkAgg(fig, master = master)
canvas.get_tk_widget().place(x=235,y=0)
canvas.draw()
tct7dpnv

tct7dpnv1#

下面是具有两个y轴的图的示例:

import random
import tkinter as tk
import matplotlib.pyplot as plt
from matplotlib.backends.backend_tkagg import FigureCanvasTkAgg

master = tk.Tk()

x_values = list(range(1, 101, 10))
y1_values = [random.randint(100, 1000) for _ in range(10)]
y2_values = [random.randint(1000, 10000) for _ in range(10)]

fig, ax = plt.subplots(figsize=(10, 5))
ax.plot(x_values, y1_values, color="red", marker="o")
ax.set_xlabel("X Values", fontsize=14)
ax.set_ylabel("Y1 Values", fontsize=14, color="red")

ax2 = ax.twinx()
ax2.plot(x_values, y2_values, color="blue", marker="o")
ax2.set_ylabel("Y2 Values", fontsize=14, color="blue")

canvas = FigureCanvasTkAgg(fig, master=master)
canvas.get_tk_widget().pack()

master.mainloop()

结果是:

相关问题