I am very new to python. I tried to make a plot by loading a .txt file that contains two arrays of numbers. The plot looks fine, but there is an additional line which I am unable to get rid of. I have attached my code here. Please help!
Thanks.
import numpy as np
import matplotlib.pyplot as plt
from numpy import genfromtxt
data= genfromtxt ('PVC_Cs137.txt')
plt.plot(data)
plt.xlim(0,2500)
plt.ylim(0,30000)
plt.xlabel("Channel number")
plt.ylabel("Counts")
plt.show()
Link to the data
I believe that this should work for you:
data= genfromtxt ('PVC_Cs137.txt')
plt.plot(data[:,0], data[:,1])
plt.xlim(0,2500)
plt.ylim(0,30000)
plt.xlabel("Channel number")
plt.ylabel("Counts")
plt.show()
This explicitly tells matplotlib that you want to plot the first index of your data against the second index
Related
I found out how to remove existing lines in a plot in the following code:
import matplotlib.pyplot as plt
fig=plt.Figure()
axes=fig.add_subplot(111)
line,=axes.plot([0,1,2,3])
line.remove()
After doing this, I wanted to know if I could show the removed line again. Thanks!
I would like to know if the behavior of the following code is expected.
The first figure (Series) is saved as I would expect. The second (DataFrame) is not.
If this is not a bug, how can I achieve my (obvious) goal?
import numpy as np
import pandas as pd
import matplotlib.pyplot as plt
fig = plt.figure()
pd.Series(np.random.randn(100)).plot()
fig.savefig('c:\\temp\\plt_series.png')
fig = plt.figure()
pd.DataFrame(np.random.randn(100,2)).plot()
fig.savefig('c:\\temp\\plt_df.png')
After saving the figure, close the current plot using plt.close() to close the current figure, otherwise the old one is still active even if the next plot is being generated. You can also use plt.close('all') to be sure all open figures are closed.
I have +8000 values that I need to plot in python. I tried several snippets that I found but none worked.
My file (.dat) has the following structure :
-7.1441700e-01
-5.5069000e-01
-4.5883200e-01
-5.5877700e-01
-5.7281500e-01
-7.2219800e-01
-4.0725700e-01
-8.7051400e-01
-1.1273190e+00
-1.0572810e+00
-9.7869900e-01
-9.4284100e-01
-8.7326000e-01
-8.7219200e-01
-7.1533200e-01
-5.2352900e-01
-4.7027600e-01 ....
My goal is to obtain something like this:
But for some reason I'm not getting the same result. Thanks in advance !
Assuming you are using numpy and matplotlib, you should be able to load the data directly into a numpy array and then plot that:
import numpy as np
import matplotlib.pyplot as plt
data = np.loadtxt('filename.dat')
plt.plot(data)
plt.show()
If this does not work, post the error messages you are receiving.
I'm trying to get a bigger chart. However, the figure method from matplotlib does not seem to be working properly.
I get a message, which is not an error:
import pandas.io.data as web
import pandas as pd
import matplotlib.pyplot as plt
%matplotlib inline
...
plt.figure(figsize=(20,10))
df2['media']= df2['SPY']*.6 + df2['TLT']*.4
df2.plot()
plt.show()
What's wrong with my code?
You can skip the first plt.figure() and just use the argument figsize:
df2.plot(figsize=(20,10))
See docs.
I am trying to plot some data from a file. The file contains 13 columns, but i want just the first and the fourth column to plot. Also, there are more than one of the file, i want to plot them on the same diagram. I succeeded to show lines on the diagram. I added my code for plotting arrays. The problem is that i want to have different colors for each file, but my code does the same for all. How can i correct it?
Thank you.
# gen_len is an array, same for all files
# gen_number is an array contains information
# of files
colors="bgrcmyk"
index=0
for gen in gen_number:
plt.plot(gen,gen_len,color=colors[index])
index=index+1
plt.savefig('result.png')
plt.show()
A more elegant solution for reading in your files would be to use numpy's genfromtxt, which can import just your desired columns, and also ignore lines starting with a certain character (the comments='#' keyword). I think this code does as you want:
import numpy as np
import matplotlib.pyplot as plt
import sys
colors="bgrcmyk"
for i in range(1,len(sys.argv)):
gen,gen_len=np.genfromtxt(sys.argv[i],usecols=(0,3),unpack=True,comments='#')
plt.plot(gen,gen_len,c=colors[i])
plt.savefig('result.png')