![](/img/trans.png)
[英]How to change separation between tick labels and axis labels in Matplotlib
[英]How to Change Axis Tick Labels in dynamic tkinter chart using matplotlib
我在matplotlib的画布上有一个图表,该图表将被相当频繁地更改,并且我无法更改轴标签,相反,我只是在主要网格线上获得了默认的数字标签。 这是一个简化的示例:
import matplotlib.pyplot as plt
matplotlib.use('TkAgg')
from matplotlib.backends.backend_tkagg import FigureCanvasTkAgg, NavigationToolbar2TkAgg
from matplotlib.figure import Figure
#import matplotlib.animation as animation
#from matplotlib import style
import numpy as np
import Tkinter as tk
import ttk
def customplot(f):
try:
f.clf()
#plt.clf()
#ax.clear()
#f.delaxes(ax)
except:
None
try:
ax=ax
except:
ax=f.add_subplot(111)
ax.scatter(np.random.uniform(size=3),np.random.uniform(size=3))
plt.xticks([1,2,3],['one','two','three']) #THIS LINE!!!!???
class My_GUI:
def __init__(self,master):
self.master=master
self.f = Figure(figsize=(5,5), dpi=100)
self.canvas1=FigureCanvasTkAgg(self.f,self.master)
self.updatechartbutton=tk.Button(master=master,text='update plot',command=self.drawcustomplot)
self.canvas1.get_tk_widget().pack(side="top",fill='x',expand=True)
#self.canvas1.mpl_connect('pick_event',self.onpick)
self.toolbar=NavigationToolbar2TkAgg(self.canvas1,master)
self.toolbar.update()
self.toolbar.pack(side='top',fill='x')
self.updatechartbutton.pack(side='top')
def drawcustomplot(self):
customplot(self.f)
plt.xticks([1,2,3],['one','two','three'])
self.canvas1.show()
root=tk.Tk()
gui=My_GUI(root)
root.mainloop()
这段代码只是使用画布小部件启动了一个tkinter,该小部件包含了该人物,然后在按下按钮时对其进行了更新。
您会在customplot
函数中注意到,我尝试将plt.xticks
设置为无效。 我意识到这可能与使用pyplot声明它们没有正确转换为tkinter的更改有关,但是我不确定如何正确执行。 在此先感谢您的帮助!
您必须使用ax
(AxesSubPlot对象)来代替plt
,但这种情况会发生变化:
plt.xticks([1,2,3],['one','two','three'])
至
ax.set_xticks([1,2,3])
ax.set_xticklabels(['one','two','three'])
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.