在带有辅助轴的 tkinter GUI 中绘制 Pandas DataFrame 并设置参数

问题描述 投票:0回答:1

我有一个 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()
pandas dataframe matplotlib tkinter axis
1个回答
0
投票

下面是一个有两个 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()

结果:

© www.soinside.com 2019 - 2024. All rights reserved.