Using the matplotlib to plot - python

I try to analyze the open data,and I tried to plot the scatter figure, but encounter the problem is always show the error.
import pandas as pd
import numpy as np
import matplotlib.pyplot as plt
# 讀入 csv 文字檔
csv_file = ("../ff0002fiac-4.csv")
data = pd.read_csv(csv_file,names=['a','b','c','d','e','f'])
print(data.head(5))
#df=pd.DataFrame(data)
years=data['a']
people=data['b']
print(years)
print(people)
data.plot(kind='line',x=years,y=people)
plt.show()
I expect to show the scatter figure, but the result is error.
Here is the data:
a b c d e f
0 100 3.56 120905 89608 72562 6686
1 101 3.43 118800 90229 73645 7858
2 102 3.47 116210 90236 73148 9170
3 103 3.17 105977 82889 68020 7949
4 104 3.36 121654 95517 77258 10049
and show the error below
KeyError: '[100 101 102 103 104 105 106] not in index'

From the pandas.DataFrame.plot documentation, the x and y parameters should be labels or positions. You're probably meaning to do this:
data.plot(kind='line',x='a',y='b')

Related

Ploting a mathematical function in python

i want to plot the data which is shown below and compere it to a function which gives me the theoretical plot. I am able to plot the data with its uncertainty, but i am struguling to plot the mathematical function function which gives me the theoretical plot.
amplitude uncertainty position
5.2 0.429343685 0
12.2 1.836833144 1
21.4 0.672431409 2
30.2 0.927812481 3
38.2 1.163321108 4
44.2 1.340998136 5
48.4 1.506088975 6
51 1.543016526 7
51.2 1.587229032 8
49.8 1.507327436 9
46.2 1.400355669 10
40.6 1.254401849 11
32.5 0.995301462 12
24.2 0.753044487 13
14 0.58 14
7 0.29 15
here is my code so far:
import matplotlib.pyplot as plt
import pandas as pd
import numpy as np
data = pd.read_excel("Verdier_6.xlsx")
verdier = data.values
frekvens = verdier [:,3]
effektresonans = verdier [:,0]
usikkerhet = verdier [:,1]
x = np.arange(0,15,0.1)
p= 28.2
r=0.8156
v= 343.8
f= 1117
y=p*np.sqrt(1+r**2+2*r*np.cos(((2*np.pi)/(v/f))*x))
plt.plot(x,y)
plt.plot(frekvens, effektresonans)
plt.errorbar(frekvens, effektresonans, usikkerhet, fmt = "o")
plt.title("")
plt.xlabel("Posisjon, X [cm]")
plt.ylabel("Amplitude, U [mV] ")
plt.grid()
plt.show()
And here is here is a image of the plot with only experimental data shown above:
and here is an image of how my experimental and theoretical plot look:
and here is an image of how the experimental and theoretical plot should look:

for loop to plot multiple graph in one diagram

I am trying to plot multiple graphs in one diagram. I am planning to do it with a for loop.
x = df1['mrwSmpVWi']
c = df['c']
a = df['a']
b = df['b']
y = (c / (1 + (a) * np.exp(-b*(x))))
for number in df.Seriennummer:
plt.plot(x,y, linewidth = 4)
plt.title("TEST")
plt.xlabel('Wind in m/s')
plt.ylabel('Leistung in kWh')
plt.xlim(0,25)
plt.ylim(0,1900)
plt.show()
The calculation doesn't work I just get dots in the diagram and I get 3 different diagrams.
This is the df:
Seriennummer c a b
0 701085 1526 256 0.597
1 701086 1193 271 0.659
2 701087 1266 217 0.607
Does someone know what I did wrong?
[![enter image description here][1]][1]
Df1 has about 500,000 rows. This is a part of df1:
Seriennummer mrwSmpVWi mrwSmpP
422 701087.0 2.9 25.0
423 701090.0 3.9 56.0
424 701088.0 3.2 22.0
425 701086.0 4.0 49.0
426 701092.0 3.7 46.0
427 701089.0 3.3 0.0
428 701085.0 2.4 4.0
429 701091.0 3.6 40.0
430 701087.0 2.7 11.0
431 701090.0 3.1 23.0
432 701086.0 3.6 35.0
The expected output schould be a diagram with multiple logitic graphs. Something like that: [![enter image description here][2]][2]
EDIT:
I guess you are using matplotlib. You can use something like
import matplotlib.pyplot as plt
# some calculations for x and y ...
fig, ax = plt.subplots(ncols=1,nrows=1)
for i in range(10):
ax.plot(x[i],y[i])
plt.show()
Further information can be found on the matplotlib subplots documentation>
https://matplotlib.org/api/_as_gen/matplotlib.pyplot.subplots.html
Because you problem is related to the pandas data frames, try something like
for number in df.Seriennummer:
x = df1.loc['Seriennummer'==number]['mrwSmpVWi']
y = (c['Seriennummer'==number] / (1 + (a['Seriennummer'==number]) * np.exp(-b['Seriennummer'==number]*(x))))
plt.plot(x,y, linewidth = 4)

Is it possible to plot a barchart with upper and lower limits of the bins with Pandas,seaborn or Matplotlib

I will like to know how I can go about plotting a barchart with upper and lower limits of the bins represented by the values in the age_classes column of the dataframe shown below with pandas, seaborn or matplotlib. A sample of the dataframe looks like this:
age_classes total_cases male_cases female_cases
0 0-9 693 381 307
1 10-19 931 475 454
2 20-29 4530 1919 2531
3 30-39 7466 3505 3885
4 40-49 13701 6480 7130
5 50-59 20975 11149 9706
6 60-69 18089 11761 6254
7 70-79 19238 12281 6868
8 80-89 16252 8553 7644
9 >90 4356 1374 2973
10 Unknown 168 84 81
If you want a chart like this:
then you can make it with sns.barplot setting age_classes as x and one columns (in my case total_cases) as y, like in this code:
import pandas as pd
import matplotlib.pyplot as plt
import seaborn as sns
df = pd.read_csv('data.csv')
fig, ax = plt.subplots()
sns.barplot(ax = ax,
data = df,
x = 'age_classes',
y = 'total_cases')
plt.show()

How to create a stacked barplot in python [duplicate]

This question already has answers here:
Plotting a stacked Bar Chart
(5 answers)
Closed 4 years ago.
I have a pandas data frame as follows
batsman non_striker partnershipRuns
0 SK Raina A Flintoff 23
1 SK Raina DR Smith 90
2 SK Raina F du Plessis 36
3 SK Raina JA Morkel 14
10 MS Dhoni CK Kapugedera 18
11 MS Dhoni DJ Bravo 51
12 MS Dhoni F du Plessis 27
13 MS Dhoni JA Morkel 12
14 MS Dhoni JDP Oram 6
I am finding it really difficult to create stacked bar plot with either pandas or seaborn. Any help will be appreciated
Using Pandas it is very simple to create a plot. Once you have a dataframe simply call plot like in this example (I used your example data set).
import pandas as pd
import matplotlib.pyplot as plt
#create the dataframe from your data
batsman = ['SK Raina','SK Raina','SK Raina','SK Raina','MS Dhoni','MS Dhoni', 'MS Dhoni','MS Dhoni','MS Dhoni']
non_striker = [ 'A Flintoff', 'DR Smith' ,'du Plessis','JA Morkel', 'Kapugedera', 'DJ Bravo', 'du Plessis', 'JA Morkel', 'JDP Oram']
partnershipRuns = [23,90,36,14,18,51,27,12, 6]
df = pd.DataFrame({'batsman': batsman, 'non_striker': non_striker, 'partnershipRuns': partnershipRuns})
#make your data numerical
df = df.pivot(columns='non_striker',index='batsman').fillna(0)
#plot it
df.plot(kind='bar',stacked=True,legend=False)
plt.show()
This yields something like this:
If your data is not numerical, you have to convert it first.
I have been able to do it in 2 ways
1)
df1=df.pivot(columns='non_striker',index='batsman').fillna(0)
df1.plot(kind='bar',stacked=True,legend=False)
2)
df1=df.groupby(['batsman','non_striker']).sum().unstack().fillna(0)
df1.plot(kind='bar',stacked=True,legend=False)
Both work. We need to create a table between the columns and then plot after filling NAs with 0s

AtributeError: 'module' object has no attribute 'plt' - Seaborn

I'm very new with these libraries and i'm having troubles while plotting this:
import pandas as pd
import seaborn as sns
import matplotlib.pyplot as plt
import numpy as np
import random
df5 = pd.read_csv('../../../../datos/tiempos-exacto-variando-n-m0.csv', sep=', ', engine='python')
print(df5)
df5['n'] = df5['n'].apply(lambda x: x**2)
sns.jointplot(df5['n'], df5['tiempoTotal'], kind="reg")
sns.plt.show()
And i'm getting this output:
n m tiempoTotal
0 1 0 2274
1 2 0 3370
2 3 0 5709
3 4 0 8959
4 5 0 13354
5 6 0 18503
6 7 0 26329
7 8 0 33859
8 9 0 41110
9 10 0 52710
10 11 0 64364
11 12 0 74142
12 13 0 81072
13 14 0 69332
14 15 0 71027
15 16 0 89721
16 17 0 85459
17 18 0 95217
18 19 0 119210
19 20 0 136888
20 21 0 131903
21 22 0 138395
22 23 0 151222
23 24 0 163542
24 25 0 177236
25 26 0 192475
26 27 0 240162
27 28 0 260701
28 29 0 235752
29 30 0 250835
.. ... .. ...
580 581 0 88306854
581 582 0 89276420
582 583 0 87457875
583 584 0 90807004
584 585 0 87790003
585 586 0 89821530
586 587 0 89486585
587 588 0 88496901
588 589 0 89090661
589 590 0 89110803
590 591 0 90397942
591 592 0 94029839
592 593 0 92749859
593 594 0 105991135
594 595 0 95383921
595 596 0 105155207
596 597 0 114193414
597 598 0 98108892
598 599 0 97888966
599 600 0 103802453
600 601 0 97249346
601 602 0 101917488
602 603 0 104943847
603 604 0 98966140
604 605 0 97924262
605 606 0 97379587
606 607 0 97518808
607 608 0 99839892
608 609 0 100046492
609 610 0 103857464
[610 rows x 3 columns]
---------------------------------------------------------------------------
AttributeError Traceback (most recent call last)
<ipython-input-21-63146953b89d> in <module>()
9 df5['n'] = df5['n'].apply(lambda x: x**2)
10 sns.jointplot(df5['n'], df5['tiempoTotal'], kind="reg")
---> 11 sns.plt.show()
AttributeError: 'module' object has no attribute 'plt'
I'm running this in my Jupyter Notebook with Python 2.7.12. Any ideas?
sns.plt.show() works fine for me using seaborn 0.7.1. Could be that this is different in other versions. However, if you anyways import matplotlib.pyplot as plt you may as well simply use plt.show(), as sns.plt.show() is only working because pyplot is available inside the seaborn namespace.
Well, I ran into this issue as well with Seaborn 0.8.1. Turns out being able to call sns.plt.show() is bad practice and the fact that it worked was a bug which the developer fixed. Unfortunately, there are many tutorials out there that still advise one to use sns.plt.show(). This is how I solved it:
Import plt directly: import matplotlib.pyplot as plt
Before you plot anything, set the default aesthetic parameters: sns.set() - important, because otherwise you won't get the Seaborn palettes.
Replace all calls to sns.plt with plt
As of Seaborn 0.8.1, sns.plt.plot() raises the error module 'seaborn' has no attribute 'plt'.
sns.plot() also raises an error; these methods are not in Seaborn's API.
Dropping the “sns.” to leave “plt.plot()” (as other answers suggest) does work, but only because we've called the sns.set() method in place earlier in the script... i.e. Seaborn is making an aesthetic change: Matplotlib is still the object, which does the plotting, via its plt.plot() method.
This script shows sns.set() in action... if you follow the comments and swap sns.set() between different locations in the script, it changes the appearance of the subplots. They look like Seaborn plots, but Matplotlib is doing the plotting.
Seaborn does of course have a load of its own plot methods (like sns.boxplot(), sns.violinplot() etc) but there is no longer a method sns.plt.plot().
I just want to confirm that I got the same error using Jupyter inside Anaconda (Feb 2018). Got the code from here but the error occured. It turns out that I need to simply add
import matplotlib.pyplot as plt
on top of
import seaborn as sns
and it work just fine using plt.show() instead of sns.plt.show()
Ensure you have updated your python shell as well IDE's like Anaconda.
Like I had a constant error in Spyder (Hosted under Anaconda) with relplot and catplot until I updated Anaconda as well as seaborn (0.90).
Updating via the Anaconda commandline should be pretty straightforward like in my case.

Categories