I am trying to make a scatter plot showing the housing prices in Manhattan using the longitude and latitude from a data set. When creating the scatter plot. The output only shows the extreme values of the longitude although all the other values are grouped in the range -74 to -72 (longitude). I don't know how to set the specific range in the x axis so the longitudes represented show the relevant data from the data set.
x = dataset_noise['Lon']
y = dataset_noise['Lat']
no_of_values = len(dataset_noise['Lon'])
index = np.arange(no_of_values)
plt.figure(figsize=(6,6))
plt.scatter(x, y, cmap=plt.get_cmap("jet"),linewidths=0.5,marker='.',alpha=0.2,label='Prices')
plt.title('House prices in Manhattan')
plt.show()
This is what I coded and the output
You can use the following two functions provided by the mpl :
1.
set_xlim
2.
set_ylim
these functions accept an interval (a list/tuple argument with lower (L) and upper (U) limits) [L, U] or (L, U)
I have a pandas data frame where x axis has continuous values and y axis has ones and zeros. How should I make a bar plot so that the x axis has bins like 5-10 10-15 ,.... and y axis represents the no of ones and zeros =, i.e there are 100 ones when x is between 5-10
I tried the following method
ax = df.plot.bar(x='PAY',Y='ACCEPTED')
Here Y is categorical
I got the error
No numeric data to plot
How do I plot in AITOFF for values of x and y
And then superimpose another set of x and y values with different color. Can you send me the code to plot
I have tried subplot but not working
Hello and thanks in advance. I am starting with a pandas dataframe and I would like like make a 2d plot with a trendline showing the weighteed mean y value with error bars for the uncertainty on the mean. The mean should be weighted by the total number of events in each bin. I start by grouping the df into a "photon" group and a "total" group where "photon" is a subset of the total. In each bin, I am plotting the ratio of photon events to total. On the x axis and y axis I have two unrelated variables "cluster energy" and "perimeter energy".
My attempt:
#make the 2d binning and total hist
energybins=[11,12,13,14,15,16,17,18,19,20,21,22]
ybins = [0,.125,.25,.5,.625,.75,1.,1.5,2.5]
total_hist,x,y,i = plt.hist2d(train['total_energy'].values,train['max_perimeter'].values,[energybins,ybins])
total_hist = np.array(total_hist)
#make the photon 2d hist with same bins
groups = train.groupby(['isPhoton'])
prompt_hist,x,y,i = plt.hist2d(groups.get_group(1)['total_energy'].values,groups.get_group(1)['max_perimeter'].values,bins=[energybins,ybins])
prompt_hist = np.array(prompt_hist)
ratio = np.divide(prompt_hist,total_hist,out=np.zeros_like(prompt_hist),where = total_hist!=0)
#plot the ratio
fig, ax = plt.subplots()
ratio=np.transpose(ratio)
p = ax.pcolormesh(ratio,)
for i in range(len(ratio)):
for j in range(len(ratio[i])):
text = ax.text(j+1, i+1, round(ratio[i, j], 2),ha="right", va="top", color="w")
ax.set_xticklabels(energybins)
ax.set_yticklabels(ybins)
plt.xlabel("Cluster Energy")
plt.ylabel("5x5 Perimeter Energy")
plt.title("Prompt Photon Fraction")
def myBinnedStat(x,v,bins):
means,_,_ = stats.binned_statistic(x,v,'mean',bins)
std,_ ,_= stats.binned_statistic(x,v,'std',bins)
count,_,_ = stats.binned_statistic(x,v,'count',bins)
return [ufloat(m,s/(c**(1./2))) for m,s,c in zip(means,std,count)]
I can then plot an errorbar plot, but I have not been able to plot the errorbar on the same axis as the pcolormesh. I was able to do this with hist2d. I am not sure why that is. I feel like there is a cleaner way to do the whole thing.
This yields a plot
pcolormesh plots each element as a unit on the x axis. That is, if you plot 8 columns, this data will span 0-8 on the x axis. However, you also redefined the x axis ticklabel so that 0-10 is labeled as 11-21.
For your errorbars, you specified x values at 11-21, or so it looks, which is where the data is plotted. But is not labeled since you changed the ticklabels to correspond to pcolormesh.
This discrepancy is why your two plots do not align. Instead, you could use "default" x values for errorbar or define x values for pcolormesh. For example, use:
ax.errorbar(range(11), means[0:11], yerr=uncertainties[0:11])
I am using matplotlib to plot a 2D array but in the plot I am not getting curves, it only shows the axis. Following is my code:
posx = []
posy = []
for i in range(1,37):
posx.append(data[i,0])
posy.append(data[i,1])
for j in range(2,507):
plt.plot(data[0,j],data[i,j])
print(posx,posy)
plt.show()
I have tried plt.plot(data[0,j],data[i,j],'.') which shows me a scatter plot which I don't want.
In your call to plot - plt.plot(data[0,j],data[i,j]), data[0,j] and data[i,j] are single numbers. plt.plot() tries to plot a line, however you are only passing a single x and a single y value. In order plot a line, you need at least 2 values for the x and y.
Your code can be simplified using slice notation which will remove the inner for loop:
for i in range(1,37):
plt.plot(data[0, 2:507], data[i, 2:507])
plt.show()