0

I am still getting my feet with python, so apologies if this is a very simple question.

I have an output file which contains 5 columns, as follows:

Depth Data#1 Data#2 Data#3 Standard_deviation

These columns contain 500 values, if this makes any difference.

What I am trying to do is simply plot data#1, data#2, and data#3 (on the x axis) against depth (on the y axis). I would like data#1 to be blue, and data#2 and data#3 to each be red.

The figsize I would like is (14,6).

I don't want the column containing standard deviation to be plotted here. If it is simpler, I can simply remove that column from the output.

Thanks in advance for any help!

3 Answers 3

2

As the question only regard plotting I am assuming you know how to read the data from the file. As for the plotting what you need is the following:

import matplotlib.pyplot as plt

#Create a figure with a certain size
plt.figure(figsize = (14, 6))

#Plot x versus y
plt.plot(data1, depth, color = "blue")
plt.plot(data2, depth, color = "red")
plt.plot(data3, depth, color = "red")

#Save the figure
plt.savefig("figure.png", dpi = 300, bbox_inches = "tight")

#Show the figure
plt.show()

The option bbox_inches = "tight" in savefig results in removing all the excess white boundaries of the figure.

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

Comments

2

With nearly everything with matplotlib, the way I go about it if i don't know how to do it already, is to just scan through the Gallery to find something that looks similar to what i want to do, and then alter the code there already.

This one has most of what you want in it:

enter image description here

http://matplotlib.org/examples/style_sheets/plot_fivethirtyeight.html

"""
This shows an example of the "fivethirtyeight" styling, which
tries to replicate the styles from FiveThirtyEight.com.
"""


from matplotlib import pyplot as plt
import numpy as np

x = np.linspace(0, 10)

with plt.style.context('fivethirtyeight'):
    plt.plot(x, np.sin(x) + x + np.random.randn(50))
    plt.plot(x, np.sin(x) + 0.5 * x + np.random.randn(50))
    plt.plot(x, np.sin(x) + 2 * x + np.random.randn(50))


plt.show()

It does unfortunately have a load of extra stuff in it you don't want, but the part you should pick up on is that plt.plot(...) can just be called multiple times to plot multiple lines.

Then it's just a case of applying this;

from matplotlib import pyplot    

#Make some data
depth = range(500)
allData = zip(*[[x, 2*x, 3*x] for x in depth])

#Set out colours
colours = ["blue", "red", "red"]


for data, colour in zip(allData, colours):
    pyplot.plot(depth, data, color=colour)

pyplot.show()

enter image description here

3 Comments

+1 scanning through the gallery is also my first step:)
Thank you for your reply. I didn't even know the gallery existed, but I will defer to it from now on. Cheers!
I find the gallery is really helpful, as a. i can never be bothered to remember the matplotlib boilerplate crap, so i nearly always jsut take it from an example, or previous stuff i've written (i normally go with my other stuff as i much prefer the matplotlib OO approach to it's functionatl matlab style.) and b. it quite often gets new stuff added to it which you might not have known about.
1

its matplotlibs basics:

import pylab as pl

data = pl.loadtxt("myfile.txt")

pl.figure(figsize=(14,6))
pl.plot(data[:,1], data[:,0], "b")
pl.plot(data[:,2], data[:,0], "r")
pl.plot(data[:,3], data[:,0], "r")

pl.show()

7 Comments

Thank you for your reply. This looks very simple.
I have run this script and it works well. I have a follow up question if that is okay? Is there a simple way to invert the y-axis so that 0 is at the top and the highest value is at the bottom?
You can set the x and y-values that should be included in the plotting figure. This is done using xlim and ylim. For example, if you only want the x-axis to include the values 2 to 8 you need to use: pl.xlim(2, 8). If you want to invert a plotting axis you simply revert the numbers: pl.xlim(8, 2). This of course also works for the y-axis using ylim.
@TheDude A lto of the things like this in matplotlib have the option to either just call them with xlim(8,2) or with xlim(xmin=8, xmax=2). Honestly, I'd really recommend the more verbose option, as it's more helpful (IMO) to people unfamiliar to matplotlib (or whatever framework you happen to be talking about). Also, once they understand the syntax, they can drop the keywords and then just call xlim(8,3) as they please - if by that point that's what they want.
But also file reading was required, which apparently was easiest using numpy and also matplotlib includes numpy so it doesn't make a big difference - only the namespaces change, which, certainly, might be confusing.
|

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.