box plot not appearing in Google Colab - python

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

Related

Using missing_kwds with geopandas changes the shape of the displayed map

I'm using Geopandas (0.11.1) to plot data on maps. I'm facing an issue with missing_kwds. As some of my values are undefined, I want them to be colored in a specific way. I do that using the missing_kwds option of the plot method.
However, when using it, the shape of the map slightly changes, which is disgraceful when switching quickly from one to the other.
Here is an example.
A map without using missing_kwds :
import geopandas
import matplotlib.pyplot as plt
df = geopandas.read_file(geopandas.datasets.get_path("naturalearth_lowres"))
df.plot()
plt.savefig('world1.png')
A map using missing_kwds :
import geopandas
import matplotlib.pyplot as plt
import numpy as np
df = geopandas.read_file(geopandas.datasets.get_path("naturalearth_lowres"))
df.loc[df.name=="China", 'pop_est'] = np.nan
df.plot(column="pop_est", missing_kwds=dict(color="lightgray"))
plt.savefig('world2.png')
Those are the two resulting maps.
world1.png:
world2.png:
In case the difference isn't clear, here is a GIF that illustrates the shape changes.
Does anyone have an idea how I could solve this issue?
Add plt.gca().set_aspect('equal') after df.plot().

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.

How do I display drop shadows for line charts with Matplotlib?

I am trying to make a drop shadow for some data I have. View the current image here
I can't increase the drop shadow's width. Is there a way to do it?
Here's what I currently have:
plt.plot(times, past_values,color='red',path_effects=[path_effects.SimpleLineShadow(shadow_color="red"),path_effects.Normal()])
You need to pass the linewidth argument to SimpleLineShadow.
plt.plot(times, past_values,color='red',path_effects=[path_effects.SimpleLineShadow(shadow_color="red", linewidth=5),path_effects.Normal()])
You can simple add shadow=True
Here is an example
plt.plot(times, past_values,color='red',shadow=True)
Try it it may work
import matplotlib.pyplot as plt
import numpy as np
from matplotlib.patheffects import PathPatchEffect, SimpleLineShadow, Normal
sns.set()
plt.plot([1,2,3],[1,4,9],color='red',path_effects=[SimpleLineShadow(shadow_color="red", linewidth=10),Normal()])

How to display multiple table/chart in jupyter cells using panda?

Emphasis : I am looking for a way to display multiple tables or charts from a cell.
I have the following code where I have a table and a chart (and a few more) that I would like to display all those visuals in the out put shell after running it.
I tried importing matplotlib and plt.show() but they don't seem to work.
import pandas as pd
import matplotlib.pyplot as plt
%matplotlib inline
tab = [[49.0,35.5,26.7,17.5], [46.7,34.0,26.0,17.1]]
tab = list(map(list, zip(*tab)))
tab_DataFrame = pd.DataFrame(tab, columns=["Mass(g)", "Volume(mL)"])
tab_DataFrame.head()
plt.show()
ax1 = tab_DataFrame.plot.line(x="Mass(g)", y="Volume(mL)",style='-o')
plt.show()
Thanks in advance.

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