I am trying to draw histogram but nothing appears in the Figure Window.
My code is below:
import numpy as np
import matplotlib.pyplot as plt
values = [1000000, 1525097, 2050194, 1095638, 1620736, 2145833, 1191277, 1716375, 1286916, 1382555]
plt.hist(values, 10, histtype = 'bar', facecolor = 'blue')
plt.ylabel("Values")
plt.xlabel("Bin Number")
plt.title("Histogram")
plt.axis([0,11,0,220000])
plt.show()
This is the output:
I am trying to achieve this plot
Any help would be much appreciated...
You are confusing what a histogram is. The histogram that can be produced with the given data is as given below.
A histogram basically counts how many given values fall within a given range.
You have given incorrect arguments to the axis() function. The ending value is 2200000 You missed a single zero. Also you have swapped the arguments. Limits of the x axis comes first and then the limits of the Y axis. This is the modified code:
import numpy as np
import matplotlib.pyplot as plt
values = [1000000, 1525097, 2050194, 1095638, 1620736, 2145833, 1191277, 1716375, 1286916, 1382555]
plt.hist(values, 10, histtype = 'bar', facecolor = 'blue')
plt.ylabel("Values")
plt.xlabel("Bin Number")
plt.title("Histogram")
plt.axis([0,2200000,0,11])
plt.show()
This is the histogram generated:
I finally achieved it...
Here is the code:
import numpy as np
import matplotlib.pyplot as plt
values = [1000000, 1525097, 2050194, 1095638, 1620736, 2145833, 1191277, 1716375, 1286916, 1382555]
strategy = [1,2,3,4,5,6,7,8,9,10]
value = np.array(values)
strategies = np.array(strategy)
plt.bar(strategy, values, .8)
plt.ylabel("Values")
plt.xlabel("Bin Number")
plt.title("Histogram")
plt.axis([1,11,0,2200000])
plt.show()
Output:
Related
I plotted some values from my experiment output by the spider(anaconda). I want to change x axis scientific notation from 1e-05 to 1e-06. I googled and could not find a relevant solution to this problem. please help
enter code here
#Import Libraries
import numpy as np
import csv
import matplotlib.pyplot as plt
import pylab
import style
#Handling value error
def isfloat(num):
try:
float(num)
return True
except ValueError:
return False
#import csv file
with open( 'try1.csv', 'r') as i:
file01= list(csv.reader(i,delimiter=','))[2:]
file02=[[float(k) if (isfloat(k)) else 0 for k in j] for j in file01] # Picking the values only
#creating a mumpy array
Data= np.array(file02, dtype=float)
xdata= Data[:,0][::280]
ydata= Data[:,1][::280]
#Plot
plt.figure(1,dpi=800)
plt.title('Force Spectroscopy')
plt.ylabel('Vertical Deflection')
plt.xlabel('Measured Height')
plt.style.use(['seaborn_grid'])
plt.plot(xdata,ydata, color='green',label=('Experimental data'))
#Theoritical Plot
new= -(0.107e-5)*xdata
plt.plot(xdata,new, color= 'purple',label='Theoritical')
#Legend Modification
plt.legend('upper right',fontsize=20)
plt.legend()
Output image of my plot. see the axis notation 1e-5
you can use ticklabel_format() to set the tick label format. You can add the following line -> plt.ticklabel_format(style='sci', axis='x', scilimits=(-6,-6)) to your code to have the x-axis in e-06. Note that the -6 to -6 is telling matplotlib to set the format from e-06 to e-06. More info here
Your modified code sample here to demonstrate the same...
Code
import numpy as np
import matplotlib.pyplot as plt
import pylab
import style
xdata = np.array([1.21, 1.32, 2.54]) * (1e-5)
ydata = [1, 4, 15]
#Plot
plt.figure(1,dpi=800)
plt.title('Force Spectroscopy')
plt.ylabel('Vertical Deflection')
plt.xlabel('Measured Height')
plt.plot(xdata,ydata, color='green',label=('Experimental data'))
#Theoritical Plot
new= -(0.107e-5)*np.array(xdata)
plt.plot(xdata,new, color= 'purple',label='Theoritical')
plt.ticklabel_format(style='sci', axis='x', scilimits=(-6,-6))
#Legend Modification
plt.legend('upper right',fontsize=20)
plt.legend()
Output
I want this plot's y-axis to be centered at 38, and the y-axis scaled such that the 'humps' disappear. How do I accomplish this?
import matplotlib
import matplotlib.pyplot as plt
import numpy as np
import pandas as pd
s=['05/02/2019', '06/02/2019', '07/02/2019', '08/02/2019',
'09/02/2019', '10/02/2019', '11/02/2019', '12/02/2019',
'13/02/2019', '20/02/2019', '21/02/2019', '22/02/2019',
'23/02/2019', '24/02/2019', '25/02/2019']
df[0]=['38.02', '33.79', '34.73', '36.47', '35.03', '33.45',
'33.82', '33.38', '34.68', '36.93', '33.44', '33.55',
'33.18', '33.07', '33.17']
# Data for plotting
fig, ax = plt.subplots(figsize=(17, 2))
for i,j in zip(s,df[0]):
ax.annotate(str(j),xy=(i,j+0.8))
ax.plot(s, df[0])
ax.set(xlabel='Dates', ylabel='Latency',
title='Hongkong to sing')
ax.grid()
#plt.yticks(np.arange(min(df[p]), max(df[p])+1, 2))
fig.savefig("test.png")
plt.show()
I'm not entirely certain if this is what you're looking for but you can adjust the y-limits explicitly to change the scale, i.e.
ax.set_ylim([ax.get_ylim()[0], 42])
Which only sets the upper bound, leaving the lower limit unchanged, this would give you
you can supply any values you find appropriate, i.e.
ax.set_ylim([22, 52])
will give you something that looks like
Also note that the tick labels and general appearance of your plot will differ from what is shown here.
Edit - Here is the complete code as requested:
import matplotlib
import matplotlib.pyplot as plt
import numpy as np
import pandas as pd
df = pd.DataFrame()
s=['05/02/2019', '06/02/2019', '07/02/2019', '08/02/2019',
'09/02/2019', '10/02/2019', '11/02/2019', '12/02/2019',
'13/02/2019', '20/02/2019', '21/02/2019', '22/02/2019',
'23/02/2019', '24/02/2019', '25/02/2019']
df[0]=['38.02','33.79','34.73','36.47','35.03','33.45',
'33.82','33.38','34.68','36.93','33.44','33.55',
'33.18','33.07','33.17']
# Data for plotting
fig, ax = plt.subplots(figsize=(17, 3))
#for i,j in zip(s,df[0]):
# ax.annotate(str(j),xy=(i,j+0.8))
ax.plot(s, pd.to_numeric(df[0]))
ax.set(xlabel='Dates', ylabel='Latency',
title='Hongkong to sing')
ax.set_xticklabels(pd.to_datetime(s).strftime('%m.%d'), rotation=45)
ax.set_ylim([22, 52])
plt.show()
I want to set the x tick density by specifying how many ticks to skip each time. For example, if the x axis is labelled by 100 consecutive dates, and I want to skip every 10 dates, then I will do something like
import numpy as np
import pandas as pd
import matplotlib.pyplot as plt
ts = pd.period_range("20060101", periods=100).strftime("%Y%m%d")
y = np.random.randn(100)
ax = plt.subplot(1, 1, 1)
ax.plot(ts, y)
xticks = ax.get_xticks()
ax.set_xticks(xticks[::10])
plt.xticks(rotation="vertical")
plt.show()
However the output is out of place. Pyplot only picks the first few ticks and place them all in the wrong positions, although the spacing is correct:
What can I do to get the desired output? Namely the ticks should be instead:
['20060101' '20060111' '20060121' '20060131' '20060210' '20060220'
'20060302' '20060312' '20060322' '20060401']
#klim's answer seems to put the correct marks on the axis, but the labels still won't show. An example where the date axis is correctly marked yet without labels:
Set xticklabels also. Like this.
xticks = ax.get_xticks()
xticklabels = ax.get_xticklabels()
ax.set_xticks(xticks[::10])
ax.set_xticklabels(xticklabels[::10], rotation=90)
Forget the above, which doesn't work.
How about this?
import numpy as np
import pandas as pd
import matplotlib.pyplot as plt
ts = pd.period_range("20060101", periods=100).strftime("%Y%m%d")
x = np.arange(len(ts))
y = np.random.randn(100)
ax = plt.subplot(1, 1, 1)
ax.plot(x, y)
ax.set_xticks(x[::10])
ax.set_xticklabels(ts[::10], rotation="vertical")
plt.show()
This works on my machine.
I'm sure there's an easy answer to this and I'm just looking at things wrong, but what's going on with my pyplot histogram? Here's the output; the data contains participants between the ages of 18 and 24, with no fractional ages (nobody's 18.5):
Why are the bins staggered like this? The current width is set to 1, so each bar should be the width of a bin, right? The problem gets even worse when the width is less than 0.5, when the bars look like they're in completely different bins.
Here's the code:
import matplotlib.pyplot as plt
import numpy as np
import pandas as pd
csv = pd.read_csv('F:\Python\Delete\Delete.csv')
age = csv.age
gender = csv.gender
new_age = age[~np.isnan(age)]
new_age_f = new_age[gender==2]
new_age_m = new_age[gender==1]
plt.hist(new_age_f, alpha=.80, label='Female', width=1, align='left')
plt.hist(new_age_m, alpha=.80, label='Male', width=1, align='left')
plt.legend()
plt.show()
Thank you!
plt.hist does not have any argument width. If width is specified, it is given to the underlying patch, meaning that the rectangle is made 1 wide. This has nothing to do with the bin width of the histogram and I would guess there are little to no reasons to ever use width in a histogram call at all.
Instead what you want is to specify the bins. You probably also want to use the same bins for both histogram plots.
import matplotlib.pyplot as plt
import numpy as np; np.random.seed(5)
import pandas as pd
csv = pd.DataFrame({"age" : np.random.randint(18,27, 20),
"gender" : np.random.randint(1,3,20)})
age = csv.age
gender = csv.gender
new_age = age[~np.isnan(age)]
new_age_f = new_age[gender==2]
new_age_m = new_age[gender==1]
bins = np.arange(new_age.values.min(),new_age.values.max()+2)
plt.hist(new_age_f, alpha=.40, label='Female', bins=bins, ec="k")
plt.hist(new_age_m, alpha=.40, label='Male', bins=bins, ec="k")
plt.legend()
plt.show()
I would like to plot an EPSgram (see below) using Python and Matplotlib.
The boxplot function only plots quartiles (0, 25, 50, 75, 100). So, how can I add two more boxes?
I put together a sample, if you're still curious. It uses scipy.stats.scoreatpercentile, but you may be getting those numbers from elsewhere:
from random import random
import numpy as np
import matplotlib.pyplot as plt
from scipy.stats import scoreatpercentile
x = np.array([random() for x in xrange(100)])
# percentiles of interest
perc = [min(x), scoreatpercentile(x,10), scoreatpercentile(x,25),
scoreatpercentile(x,50), scoreatpercentile(x,75),
scoreatpercentile(x,90), max(x)]
midpoint = 0 # time-series time
fig = plt.figure()
ax = fig.add_subplot(111)
# min/max
ax.broken_barh([(midpoint-.01,.02)], (perc[0], perc[1]-perc[0]))
ax.broken_barh([(midpoint-.01,.02)], (perc[5], perc[6]-perc[5]))
# 10/90
ax.broken_barh([(midpoint-.1,.2)], (perc[1], perc[2]-perc[1]))
ax.broken_barh([(midpoint-.1,.2)], (perc[4], perc[5]-perc[4]))
# 25/75
ax.broken_barh([(midpoint-.4,.8)], (perc[2], perc[3]-perc[2]))
ax.broken_barh([(midpoint-.4,.8)], (perc[3], perc[4]-perc[3]))
ax.set_ylim(-0.5,1.5)
ax.set_xlim(-10,10)
ax.set_yticks([0,0.5,1])
ax.grid(True)
plt.show()