Inverse method of ".remove()" in matplotlib - python

I found out how to remove existing lines in a plot in the following code:
import matplotlib.pyplot as plt
fig=plt.Figure()
axes=fig.add_subplot(111)
line,=axes.plot([0,1,2,3])
line.remove()
After doing this, I wanted to know if I could show the removed line again. Thanks!

Related

Odd behavior of plotting in Pandas

I would like to know if the behavior of the following code is expected.
The first figure (Series) is saved as I would expect. The second (DataFrame) is not.
If this is not a bug, how can I achieve my (obvious) goal?
import numpy as np
import pandas as pd
import matplotlib.pyplot as plt
fig = plt.figure()
pd.Series(np.random.randn(100)).plot()
fig.savefig('c:\\temp\\plt_series.png')
fig = plt.figure()
pd.DataFrame(np.random.randn(100,2)).plot()
fig.savefig('c:\\temp\\plt_df.png')
After saving the figure, close the current plot using plt.close() to close the current figure, otherwise the old one is still active even if the next plot is being generated. You can also use plt.close('all') to be sure all open figures are closed.

My plot bar graph isn't showing up. What's wrong with my code

import matplotlib as mpl
import matplotlib.pyplot as plt
%matplotlib inline
%matplotlib notebook
plt.figure(figsize=(12, 6))
CasData.pivot(index='year', columns='CasualtyNumber', values='People').plot(kind='bar')
plt.title('Casualties per year')
plt.xlabel('Year', fontsize=5)
plt.ylabel('Number of Casualties')
plt.show()
My plot bar graph using matplotlib.pyplot isn't showing.
I don't know why but my bar graph isn't showing. I've tried different ways.
If someone could help me out please. I'd appreciate it. Thank you.
Remove the line %matplotlib notebook.
It is overriding the previous line (these two lines are setting the backend). inline returns static plots, notebook is used for interactivity.
You also do not need the plt.show() line. This is taken care of by the inline backend.
This answer explains more about the backends: https://stackoverflow.com/a/43028034/6709902
I'm not really sure about your code as it seems incomplete but if you're using pivot I assumed you're pulling the data from a ".csv" file.
import matplotlib as mpl
import matplotlib.pyplot as plt
import pandas as pd
%matplotlib notebook
CasData = pd.read_csv('data.csv')
CasData.pivot_table(index='year', columns='CasualtyNumber', values='People').plot(kind='bar')
plt.title('Casualties per year')
plt.xlabel('Year',fontsize='5')
plt.ylabel('Number of Casualties')
plt.show()
You need to provide the data in order to plot something and I don't
see you providing any.

box plot not appearing in Google Colab

Trying to create a simple Box Plot using Google Colab for my Intro Python class. It is not appearing as I would like it. You can see my code and output below. I read in a file on NBA statistics, and my box plot would be based on a variable called "SHOT_CLOCK".
So far what I have:
import pandas as pd
from matplotlib import pyplot as plt
df = pd.read_csv('file path')
plt.boxplot(df['SHOT_CLOCK'], vert=False)
plt.title('Box Plot for SHOT_CLOCK')
plt.xlabel('Shot Clock')
plt.show()
Output:
Edit
In your example you are passing a Series object, try this way
plt.figure()
plt.title('Box Plot for SHOT_CLOCK')
plt.xlabel('Shot Clock')
df.boxplot(column='SHOT_CLOCK')
Once you add the following Import to your code it will work:
import matplotlib.pyplot as plt
plt.style.use('classic')
%matplotlib inline

error in python plot: unwanted line in the plot

I am very new to python. I tried to make a plot by loading a .txt file that contains two arrays of numbers. The plot looks fine, but there is an additional line which I am unable to get rid of. I have attached my code here. Please help!
Thanks.
import numpy as np
import matplotlib.pyplot as plt
from numpy import genfromtxt
data= genfromtxt ('PVC_Cs137.txt')
plt.plot(data)
plt.xlim(0,2500)
plt.ylim(0,30000)
plt.xlabel("Channel number")
plt.ylabel("Counts")
plt.show()
Link to the data
I believe that this should work for you:
data= genfromtxt ('PVC_Cs137.txt')
plt.plot(data[:,0], data[:,1])
plt.xlim(0,2500)
plt.ylim(0,30000)
plt.xlabel("Channel number")
plt.ylabel("Counts")
plt.show()
This explicitly tells matplotlib that you want to plot the first index of your data against the second index

Making a chart bigger in size

I'm trying to get a bigger chart. However, the figure method from matplotlib does not seem to be working properly.
I get a message, which is not an error:
import pandas.io.data as web
import pandas as pd
import matplotlib.pyplot as plt
%matplotlib inline
...
plt.figure(figsize=(20,10))
df2['media']= df2['SPY']*.6 + df2['TLT']*.4
df2.plot()
plt.show()
What's wrong with my code?
You can skip the first plt.figure() and just use the argument figsize:
df2.plot(figsize=(20,10))
See docs.

Categories