Please, I need some help, so I am still in the learning phase of the Python programming for Data Science. I tried plotting a function but I am getting this. Please, what can I do? I am using Python 3. I appreciate your insights, explanations.
import pandas as pd
import numpy as np
import matplotlib as mpl
import matplotlib.pyplot as plt
from matplotlib import rcParams
import seaborn as sb
mtcars = pd.read_csv('mtcars.csv')
mtcars.columns = ['Car names','mpg','cyl','disp','hp','drat','wt','qsec','vs','am','gear','carb']
mpg1 = mtcars['mpg']
print(mpg1.plot())
df = mtcars[['mpg','cyl','wt']]
print(df.plot())
When I run to see what the plot is like, this is what I get:
AxesSubplot(0.125,0.11;0.775x0.77)
Please can you explain? How can I see the plot?
Thank you so much
you shouldn't "print" the plot, instead you should just call mpg1.plot() (eventually follpwed by plt.show() if needed)
Related
I am trying to create a plot using seaborn with data from an excel sheet on python on Jupiter.
The code I am using is:
[In]: import numpy as np
[In]:import pandas as pd
[In]:import seaborn as sns
[In]:import matplotlid.pyplot as plt
[In]:%matplotlib
[In]: df =pd.read_excel(r'C:\Users\jcree\OneDrive\Documents\Accounting\Applied_Accounting_Principles\Exercises\Data_Analytics\Workbook1.xlsx.xlsx')
[In]: sns.distplot(df['Item Price'])
This is where the plot should show up but nothing is happening.
I tried using this solution Seaborn plots not showing up but I am unableto get a chart to show.
Any help would be appreciated
I installed Matplotlib via Anaconda from here: https://anaconda.org/conda-forge/matplotlib
I used the very first command in Anaconda prompt.
But when I tried to plot from python (Spyder) as the following, I get the message:
ModuleNotFoundError: No module named 'matplotlib.plot'
import numpy as np
import matplotlib.plot as plt
x = np.linspace(0, 2*np.pi, 100)
y = np.sin(x)
plt.plot(x,y)
I have installed numpy, pandas and such using the same method and they work well.
How can I fix this?
Thank you so much.
matplotlib.pyplot is a state-based interface to matplotlib. pyplot is mainly intended for interactive plots and simple cases of programmatic plot generation. Therefore, whenever trying to work with graphs and what is commonly known and informally often referred as matplotlib you should import matplotlib.pyplot as plt:
import numpy as np
import matplotlib.pyplot as plt
x = np.linspace(0, 2*np.pi, 100)
y = np.sin(x)
plt.plot(x,y)
I am trying to create a factor plot but I am not able to change the kind of it from point to bar. How do we do that?
The codes used are
import numpy as np
import matplotlib.pyplot as plt
import seaborn as sns
%matplotlib inline
sns.catplot('Sex',kind="bar",data=titanic_df)
The seaborn documentation has the exact example you are looking for. Following the documentation, if you run the below lines, it should generate the bar plot shown.
import matplotlib.pyplot as plt
import seaborn as sns
%matplotlib inline
titanic = sns.load_dataset("titanic")
exercise = sns.load_dataset("exercise")
g = sns.catplot("alive", col="deck",
col_wrap=3, data=titanic[titanic.deck.notnull()],
kind="count", height=2.5, aspect=.8)
The important argument to note here is kind="count".
I am trying to do a simple plot of a data from a text file. Below is the file:
Date,Open,High,Low,Close
03-10-16,774.25,776.065002,769.578768,772.559998
04-10-16,776.03,778.710022,772.890015,776.429993
05-10-16,779.30,782.070007,775.650024,776.469971
06-10-16,779.00,780.479989,775.539978,776.859985
07-10-16,779.65,779.659973,770.757867,775.080017
Below is the python code i m trying to execute:
import matplotlib.pyplot as plt
import pandas as pd
df = pd.read_csv('financial.txt', index_col=0)
df.plot(x=df.index, y=df.columns)
plt.show()
Error:
KeyError: "Index(['03-10-16', '04-10-16', '05-10-16', '06-10-16', '07-10-16'], dtype='object', name='Date') not in index"
I am not sure why i am getting that error? Although i have achieved what i wanted by using csv but not sure why i am getting that error.
Checked for the same error online as well but didn't get much.I have checked this.
Key Error:3 while using For to plot using matplotlib.pyplot.scatter
Any light on the error is much appreciated.
Thanks.
This modification will work. You misunderstood how to use df.plot(). Please refer this page visualization. This code below is just a basic visualization, you can change to df.plot.box() or df.plot.area() to get more advanced visualization.
import matplotlib.pyplot as plt
import pandas as pd
df = pd.read_csv('financial.txt', index_col=0)
df.plot()
plt.show()
I am new using python and try to do some plots. I realized, that a plot of a bump function is incorrect. I have no idea how python came to this result.
This is my 'code'
import matplotlib.pyplot as plt
import matplotlib as mpl
import numpy as np
import matplotlib.pyplot as plt
class MainBody():
x = np.linspace(0.0001,99.9999,1000)
result = np.exp((-1.0)/(x*(100.0-x)))
plt.plot(x,result)
plt.show()
I got this result
but I should get this
I know that Python is powerful but I think such simple things should work without occuring such errors, where is my mistake?
Thank you
Matthias
Use plt.ylim to set the y-limits. Otherwise, by default, matplotlib will try to show the entire dataset, whose y-limits go roughly from 0 to 1:
import matplotlib.pyplot as plt
import numpy as np
x = np.linspace(0.0001,99.9999,1000)
result = np.exp((-1.0)/(x*(100.0-x)))
plt.plot(x,result)
plt.ylim(0.9975, 0.9999)
plt.show()