1

Hi I have a code that plots 2D data from a .dat file (I'll call it filename.dat which is just a .dat file with 2 columns of numbers). It works fine, I just have some questions as to how to improve it.

How can I edit my code to make the axes label larger and add a title? This code is not so easy to edit the way I have it written now. I have tried adding the fontsize,title into the plotfile(...) command, but this did not work. Thanks for the help! My code is below.

import numpy as np
import matplotlib.pyplot as plt

#unpack file data

dat_file = np.loadtxt("filename.dat",unpack=True)
plt.plotfile('filename.dat', delimiter=' ',cols=(0,1), names=('x','y'),marker='0')
plt.show()
3
  • This is just an opinion, but I prefer to create a figure first: fig=matplotlib.pyplot.figure(), then get the axes with ax=fig.gca(), and then set different properties of both. Matplotlib has got a great on-line help for everything and loads of examples: matplotlib.org/users/pyplot_tutorial.html Commented Jun 8, 2016 at 17:12
  • @AleksanderLidtke Thanks a lot, that is what I want to do; set different properties for all of the axes. I will try to figure out how to implement that. I was looking for opinions as to how others would go about doing this, so thank you. I am very new to python and assumed my way was not the quickest/best way to do things. Commented Jun 8, 2016 at 17:19
  • No problem. If you search for your problem I'm sure you'll find examples that'll show you how to do whatever it is you're trying to achieve. I make a plot here, set fontsizes of some things etc., could be a starting point (line 80): github.com/AleksanderLidtke/Hawkes-Process/blob/master/… Commented Jun 8, 2016 at 17:22

1 Answer 1

1

I assume you want to add them to the plot.

You can add a title with:

plt.title("MyTitle")

and you add text to the graph with

# the following coordinates need to be determined with experimentation
# put them at a location and move them around if they are in
# the way of the graph
x = 5 
y = 10
plt.text(x,y, "Your comment")

This can help you with the font sizes:

How to change the font size on a matplotlib plot

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

5 Comments

That is very helpful, just one question for you. How can I change the fontsize of the axes? I tried adding fontsize = ... but this gave me an error message. Thanks for your help! Adding the title works excellent!
I expanded the answer to better answer your question.
The command you provided to add text to the graph: plt.text(x,y "your comment") gives me an error message since x is not defined. By the way, I figured out all of the font sizes based on what you provided. Thanks
you have to define what x and y are, say x = 5 y = 10
Oh, I see, that is very helpful. Thanks.

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.