Too many xticks in the histogam - python

I am trying to read data from a txt file containing 200 values and then trying to plot a histogram based on the data. However, there are too many values on the xticks, and I do not know how to fix this.
My code:
import math
import numpy as np
import matplotlib.pyplot as plt
f=open('LedData.rtf',"r")
lines=f.readlines()
result=[]
for x in lines:
result.append(x.split(',')[1])
f.close()
plt.hist(result)
plt.xticks(rotation = 'vertical')
plt.show
Histogram : Here is an image for reference.
Thanks for any help.

Related

How to read only part of a CSV file?

I have the following code in which I read CSV files and get a graph plotted:
import numpy as np
import matplotlib.pyplot as plt
import scipy.odr
from scipy.interpolate import interp1d
plt.rcParams["figure.figsize"] = (15,10)
def readPV(filename="HE3.csv",d=32.5e-3):
t=np.genfromtxt(fname=filename,delimiter=',',skip_header=1, usecols=0)
P=np.genfromtxt(fname=filename,delimiter=',',skip_header=1, usecols=1)
V=np.genfromtxt(fname=filename,delimiter=',',skip_header=1, usecols=2,filling_values=np.nan)
V=V*np.pi*(d/2)**2
Vi= interp1d(t[~np.isnan(V)],V[~np.isnan(V)],fill_value="extrapolate")
V=Vi(t)
return P,V,t
P,V,t=readPV(filename="HE3.csv")
fig = plt.figure()
ax = fig.add_subplot(1,1,1)
ax.plot(V,P,'ko')
ax.set_xlabel("Volume")
ax.set_ylabel("Pressure")
plt.show()
From this code, the following graph is made:
The CSV file has several data points in one column, separated by commas; I want to know how to pick a range of columns to read, instead of all of them.

CSV file matplotlib.pyplot graphing error

I am using pandas to import a csv to my notebook, and I changed any blank data column to a blank space. When I use plt.plot to make a graph of the data it turns out with a bunch of black lines on the x and y axis. Below is my code and graph:
import matplotlib.pyplot as plt
import numpy as np
%matplotlib inline
apo2data = pd.read_csv('/Users/lilyloyer/Desktop/Apo2excel.csv')
apo2data.isnull()
data = apo2data.fillna(" ")
teff=data['Teff (K)']
grav=data['logg_seis']
plt.plot(teff, grav, 'ro')

How to make sure matplotlib line graph is correct?

I need to plot an accurate line graph through matplotlib but I only get a y=x graph. And the y-axis tick values are jumbled up.
import numpy as np
import matplotlib.pyplot as plt
title = "Number of Flats Constructed"
data = np.genfromtxt('C:\data/flats-constructed-by-housing-and-development-board-annual.csv',
skip_header=1,
dtype=[('year','i8'),('flats_constructed','U50')], delimiter=",",
missing_values=['na','-'],filling_values=[0])
x = data['year']
y = data['flats_constructed']
plt.title('No. of Flats Constructed over the Years')
#plt.plot(data['year'], data['flats_constructed'])
plt.plot(x, y)
plt.show()
I received a y=x graph instead of a jagged graph reflecting the values.
Actual output
Sample of expected output
Your mistake is at ('flats_constructed','U50').
Give it as ('flats_constructed','i8') itself. You read it as string when you gave U50.
from io import StringIO
import numpy as np
s = StringIO(u"1977,30498\n1978,264946\n1979,54666\n1980,54666")
data = np.genfromtxt(s, dtype=[('myint','i8'),('myfloat','i8')], delimiter=",",skip_header=0)
data
plt.plot(data['myint'],data['myfloat'])
plt.show()

How to plot a Histogram using python

I am new with Python and I would like to have a histogram of the values that I have in a text file. But I get a Histogram with only the first value of my text file (See figure)
enter image description here
My file text is as follows:
2.52549
2.52821
4.69718
4.81173
4.85834
4.87637
3.03348
3.21677
3.28373
3.11296
3.11947
3.14685
3.19684
3.19533
3.20035
3.36578
3.3899
3.40696
3.47841
3.54231
3.54343
5.20521
5.23496
5.23317
6.1397
6.18261
6.18782
6.1184
6.20701
6.25631
And my code as follows
import matplotlib.pyplot as plt
import numpy as np
import plotly.plotly as py
from pylab import show,hist
gaussian_numbers = np.random.randn(1000)
data_Distanz = np.genfromtxt('data_Distance.txt')
x = data_Distanz[0]
plt.hist(x)
plt.title("Histogram")
plt.xlabel("Distance in m")
plt.ylabel("Amplitude")
plt.savefig('Histogram.png', bbox_inches='tight')
show()
I Would appreciate your help. Thanks in advance!

Contour plot from csv file with row being axis

I am trying to make a contour plot from a csv file. I would like the first column to be the x axis, the first row (with has values) to be the y, and then the rest of the matrix is what should be contoured, see the basic example in the figure below.
Simple table example
What I am really struggling is to get that first row to be the y axis, and then how to define that set of values so that they can be called into the contourf function. Any help would be very much appreciated as I am very new to python and am really don't know where to start with this problem.
import numpy as np
import matplotlib.pyplot as plt
import matplotlib.dates as mdates
import csv
import pandas as pd
import numpy as np
from csv import reader
from matplotlib import cm
f = pd.read_csv('/trialforplot.csv',dayfirst=True,index_col=0)
x = f.head()
y = f.columns
X,Y = np.meshgrid(x,y)
z=(x,y)
z=np.array(z)
Z=z.reshape((len(x),len(y)))
plt.contour(Y,X,Z)
plt.colorbar=()
plt.xlabel('Time')
plt.ylable('Particle Size')
plt.show()
I'm stuck at defining the z values and getting my contour plot plotting.

Categories

Resources