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.
Related
I'm using Geopandas (0.11.1) to plot data on maps. I'm facing an issue with missing_kwds. As some of my values are undefined, I want them to be colored in a specific way. I do that using the missing_kwds option of the plot method.
However, when using it, the shape of the map slightly changes, which is disgraceful when switching quickly from one to the other.
Here is an example.
A map without using missing_kwds :
import geopandas
import matplotlib.pyplot as plt
df = geopandas.read_file(geopandas.datasets.get_path("naturalearth_lowres"))
df.plot()
plt.savefig('world1.png')
A map using missing_kwds :
import geopandas
import matplotlib.pyplot as plt
import numpy as np
df = geopandas.read_file(geopandas.datasets.get_path("naturalearth_lowres"))
df.loc[df.name=="China", 'pop_est'] = np.nan
df.plot(column="pop_est", missing_kwds=dict(color="lightgray"))
plt.savefig('world2.png')
Those are the two resulting maps.
world1.png:
world2.png:
In case the difference isn't clear, here is a GIF that illustrates the shape changes.
Does anyone have an idea how I could solve this issue?
Add plt.gca().set_aspect('equal') after df.plot().
I have a data file that I would like to smooth out. I am not sure on how to do it. I have looked at a few things and nothing really helps. here is my code as is:
import matplotlib.pyplot as plt
import numpy as np
data1 = np.loadtxt('2_Record2308.dat')
plt.title('2_Record2308')
plt.plot(data1)
plt.show()
I know you can do it with the "spicy" thing but I would prefer not to use it.
Trying to create a simple Box Plot using Google Colab for my Intro Python class. It is not appearing as I would like it. You can see my code and output below. I read in a file on NBA statistics, and my box plot would be based on a variable called "SHOT_CLOCK".
So far what I have:
import pandas as pd
from matplotlib import pyplot as plt
df = pd.read_csv('file path')
plt.boxplot(df['SHOT_CLOCK'], vert=False)
plt.title('Box Plot for SHOT_CLOCK')
plt.xlabel('Shot Clock')
plt.show()
Output:
Edit
In your example you are passing a Series object, try this way
plt.figure()
plt.title('Box Plot for SHOT_CLOCK')
plt.xlabel('Shot Clock')
df.boxplot(column='SHOT_CLOCK')
Once you add the following Import to your code it will work:
import matplotlib.pyplot as plt
plt.style.use('classic')
%matplotlib inline
I would like to plot an animated heatmap from a group of DataFrames (for example saved in a dictionary), either as gif or a movie.
For example, say I have the following collection of DFs. I can display all of them one after the other. But I would like to have them all being shown in the same figure in the same way as a GIF is shown (a loop of the heatmaps).
import pandas as pd
import numpy as np
import seaborn as sns
import matplotlib.pyplot as plt
dataframe_collection = {}
for i in range(5):
dataframe_collection[i] = pd.DataFrame(np.random.random((5,5)))
# Here within the same loop just for brevity
sns.heatmap(dataframe_collection[i])
plt.show()
The simplest way is to first create separate png images, and then use a software such as ImageMagick to convert them to an animated gif.
Example to create the png's:
import pandas as pd
import numpy as np
from matplotlib import pyplot as plt
dataframe_collection = {}
for i in range(5):
dataframe_collection[i] = pd.DataFrame(np.random.random((5,5)))
#plt.pcolor(dataframe_collection[i])
sns.heatmap(dataframe_collection[i])
plt.gca().set_ylim(0, len(dataframe_collection[i])) #avoiding problem with axes
plt.axis('off')
plt.tight_layout()
plt.savefig(f'dataframe_{i}.png')
After installing ImageMagick the following shell command creates a gif. If the defaults are not satisfying, use the docs to explore the many options.
convert.exe -delay 20 -loop 0 dataframe_*.png dataframes.gif
See also this post about creating animations and an animated gif inside matplotlib.
Note that Seaborn's heatmap also has some features such as sns.heatmap(dataframe_collection[i], annot=True).
If you're unable to use ImageMagick, you could show a video by quickly displaying single png files, simulating a video.
This and this post contain more explanations and example code. Especially the second part of this answer looks promising.
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