【问题标题】:axes of matplotlib graph does not go in numerical order when plotting certain values绘制某些值时,matplotlib 图的轴不按数字顺序排列
【发布时间】:2019-08-09 07:23:47
【问题描述】:

matplotlib绘制的图表的轴在绘制某些点后变得不按数字顺序

只是想编写一个程序来绘制和连接图表上的点以掌握matplotlibtkinter 的窍门

from tkinter import *
import matplotlib.pyplot as plt
root = Tk()
x_points = []
y_points = []
def plot():
    global x_points
    global y_points
    x = entry_x.get()
    y = entry_y.get()
    x_points.append(x)
    y_points.append(y)
    plt.plot(x_points,y_points)
    plt.show()



prompt_x = Label(root, text = 'enter the x coordinate')
prompt_y = Label(root, text = 'enter the y coordinate')
button = Button(text = 'click to plot point', command = plot)
entry_x = Entry()
entry_y = Entry()
prompt_x.grid(row = 0)
prompt_y.grid(row = 1)
entry_x.grid(row = 0, column = 1)
entry_y.grid(row = 1, column = 1)
button.grid(row=2)


plt.show()
root.mainloop()

按顺序绘制点:

(1,2)
(2,5)
(1.5,3)

我希望它形成一个triangle, 但导致线条只是改变颜色并将轴拧紧

【问题讨论】:

  • 如果你像这样绘制:plt.plot([1, 2, 1.5], [2, 5, 3]);plt.show(),你会得到图表上的线。 “拧轴”是什么意思?请附上你得到的图表。

标签: python matplotlib


【解决方案1】:

很确定你的小部件返回一个字符串,如果你想正确地绘制它,你需要将它转换为数字类型。

x = float(entry_x.get())
y = float(entry_y.get())

【讨论】:

    猜你喜欢
    • 2022-01-04
    • 1970-01-01
    • 1970-01-01
    • 2021-12-03
    • 2018-07-02
    • 2021-03-27
    • 2022-11-30
    • 1970-01-01
    • 2014-07-23
    相关资源
    最近更新 更多