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()
Jeff Faraci
  • 403
  • 13
  • 28
  • 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: http://matplotlib.org/users/pyplot_tutorial.html – Aleksander Lidtke Jun 08 '16 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. – Jeff Faraci Jun 08 '16 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): https://github.com/AleksanderLidtke/Hawkes-Process/blob/master/hawkes.py – Aleksander Lidtke Jun 08 '16 at 17:22

1 Answers1

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

Community
  • 1
  • 1