0

I have borrowed some code from another source and I want to edit the figure produced. Here is the relevant (i think) code from the script.

import gtk #the gui toolkit we'll use:
from matplotlib.figure import Figure
from Tkinter import *
from matplotlib.backends.backend_gtkagg import FigureCanvasGTKAgg as FigureCanvas

#create a window to put the plot in
win = gtk.Window()
#connect the destroy signal (clicking the x in the corner)
win.connect("destroy", quit_app)
win.set_default_size(600,500)

#create a plot:
fig = Figure()
ax = fig.add_subplot(111,xlabel='Time Step', ylabel='Temp (deg C)', axisbg='black')
ax.set_ylim(0,100) # set limits of y axis.

canvas = FigureCanvas(fig) #put the plot onto a canvas
win.add(canvas) #put the canvas in the window

#show the window
win.show_all()
win.set_title("ready to receive data");
line, = ax.plot(times,yvals, color='red')

while(1):
  line.set_ydata(yvals) # draw the line
  fig.canvas.draw() # update the Canvas
  win.set_title("Temp: "+str(yvals[49])+" deg C")

I don't know whether or not all the code above is necessary - but that is all the 'plot' related code I could find.

So anyway the code works in my program perfectly.

There are TWO tasks I would like to create: (1) What I want is to include that 'str(yvals[49])' variable, which is currently being displayed in the title of the window, to be displayed in large font underneath the plot. So I think I need to make the window size a little bigger to accompany the text but not sure how to print it.

(2) I manged to change the background of the plot itself to black that plots a red line. But how can I change the background of the window itself to all black and the x/y axis to red as well.

Thanks!!

1 Answer 1

0

(1) What you are most probably looking for it the matplotlib text command. Have a look at http://matplotlib.org/users/pyplot_tutorial.html section working with text. It might be convenient to create two separate axes, so you can truly put the text below the whole figure. Maybe it is enough to place the text at xlabel position?

import matplotlib.pyplot as plt
plt.xlabel('yourtext')

(2) There are already good answers out there that might help you: e.g. How to change the pylab window background color? As for the color of the axis Changing the color of the axis, ticks and labels for a plot in matplotlib

Have also a look at Matplotlib figure facecolor (background color) in case you want to save the figure.

Sign up to request clarification or add additional context in comments.

2 Comments

I am not using pyplot - at no point do I import it - so no that code will not work. To change the x-axis to display a variable that keeps updating I would need to put it in the while loop. I think I need to modify this line somehow: ax = fig.add_subplot(111,xlabel='Time Step', ylabel='Temp (deg C)', axisbg='black')
You import Figure, though. Later you create ax. So there should be ax.text(''), but by not explicitely importing pyplot, there will be no ax.xlabel.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.