import matplotlib.pyplot as plt
import pandas as pd
import pylab as pl
import numpy as np
%matplotlib inline
!wget -O FuelConsumption.csv https://s3-api.us-geo.objectstorage.softlayer.net/cf-courses-
data/CognitiveClass/ML0101ENv3/labs/FuelConsumptionCo2.csv
df = pd.read_csv("FuelConsumption.csv")
df.head()
df1 = df[['ENGINESIZE','CYLINDERS','FUELCONSUMPTION_COMB','CO2EMISSIONS']]
df1.head(9)
plt.scatter(df1.ENGINESIZE , df1.CO2EMISSIONS , color = "black")
plt.xlabel=("enginesize")
plt.ylabel=("emission")
plt.show()
when i try to run this code i get scatter graph without axis label.
how can i get axis label if anyone can assist me ?
You need to pass the label names to xlabel and ylabel.
If it's throwing error - TypeError: 'str' object is not callable then restart the ipython kernel
import matplotlib.pyplot as plt
import pandas as pd
import pylab as pl
import numpy as np
%matplotlib inline
df = pd.read_csv("FuelConsumption.csv")
df.head()
df1 = df[['ENGINESIZE','CYLINDERS','FUELCONSUMPTION_COMB','CO2EMISSIONS']]
df1.head(9)
plt.scatter(df1.ENGINESIZE , df1.CO2EMISSIONS , color = "black")
plt.xlabel("enginesize")
plt.ylabel("emission")
plt.show()
Related
Referred to this SO post here: Matplotlib set_color_cycle versus set_prop_cycle
But I was unable to set all 20 lines' colors to be different, picture of the graph here:
Here is my code:
import matplotlib as mpl
import matplotlib.pyplot as plt
import seaborn as sns
import numpy as np
import pandas as pd
from cycler import cycler
df = pd.read_csv(r'data.csv', index_col="Date", parse_dates=True)
df.rolling(window=30).max()[30:].head(20)
ax = df.plot()
ax.set(title='Qingdao Port', ylabel='Monthly Average Prices')
ax.set_prop_cycle('color',plt.cm.jet(np.linspace(0,1,20)))
plt.show()
Do help me out here!
User DavidG has helped me with the issue, for future reference the updated code will be included here:
import matplotlib as mpl
import matplotlib.pyplot as plt
import seaborn as sns
import numpy as np
import pandas as pd
from cycler import cycler
fig, ax = plt.subplots()
ax.set_prop_cycle('color',plt.cm.tab20(np.linspace(0,1,20)))
df = pd.read_csv(r'data.csv', index_col="Date", parse_dates=True)
df.rolling(window=30).max()[30:].head(20)
df.plot(ax=ax)
ax.set(title='Qingdao Port', ylabel='Monthly Average Prices')
plt.show()
I was using the ipywidgets dropdown to create plots for the columns listed in the dropdown. I have two issues. Would any one help?
I used the clear_ouput() to clear out the graph before the next selection. But it did not work;
When I first time clicked the first item in the dropdown list ("quarter"), it did not response (No graph showed). I have to select other items first before I can generate the graph for "quarter".
Thanks a lot!
import pandas as pd
import numpy as np
import seaborn as sns
import matplotlib.pyplot as plt
import ipywidgets as ipw
url = "https://data.london.gov.uk/download/number-international-visitors-london/b1e0f953-4c8a-4b45-95f5-e0d143d5641e/international-visitors-london-raw.csv"
df_london = pd.read_csv(url)
dropdown_Col = ipw.Dropdown(options = ['quarter', 'market', 'dur_stay', 'mode'], description='Sel Col NM:')
output = ipw.Output()
def Col_Sel(ColNm):
output.clear_output()
with output:
sns.set_style("whitegrid")
sns.relplot(x=ColNm, y='visits', data=df_london, kind='line', ci=None)
def dropdown_Col_eventhandler(change):
Col_Sel(change.new)
dropdown_Col.observe(dropdown_Col_eventhandler, names='value')
display(dropdown_Col)
display(output)
I added plt.show() and now The clear_output works.
import pandas as pd
import numpy as np
import seaborn as sns
import matplotlib.pyplot as plt
import ipywidgets as ipw
url = "https://data.london.gov.uk/download/number-international-visitors-london/b1e0f953-4c8a-4b45-95f5-e0d143d5641e/international-visitors-london-raw.csv"
df_london = pd.read_csv(url)
dropdown_Col = ipw.Dropdown(options = ['quarter', 'market', 'dur_stay', 'mode'], description='Sel Col NM:')
output = ipw.Output()
def Col_Sel(ColNm):
output.clear_output()
with output:
sns.set_style("whitegrid")
sns.relplot(x=ColNm, y='visits', data=df_london, kind='line', ci=None)
def dropdown_Col_eventhandler(change):
Col_Sel(change.new)
dropdown_Col.observe(dropdown_Col_eventhandler, names='value')
display(dropdown_Col)
display(output)
I have the following pandas plot:
Is it possible to add '%' sign on the y axis not as a label but on the number. Such as it would show instead of 0.0 it would be 0.0% and so on for all the numbers?
Code:
import pandas as pd
from pandas import datetime
from pandas import DataFrame as df
import matplotlib
from pandas_datareader import data as web
import matplotlib.pyplot as plt
import datetime
end = datetime.date.today()
start = datetime.date(2020,1,1)
data = web.DataReader('fb', 'yahoo', start, end)
data['percent'] = data['Close'].pct_change()
data['percent'].plot()
Here is how you can use matplotlib.ticker:
import matplotlib.pyplot as plt
import matplotlib.ticker as mtick
fig = plt.figure()
ax = fig.add_subplot(1,1,1)
ax.yaxis.set_major_formatter(mtick.PercentFormatter())
plt.show()
Output:
You can now control the display format of the y-axis. I think it will be 0.0%.
yvals = ax.get_yticks()
ax.set_yticklabels(["{:,.1%}".format(y) for y in yvals], fontsize=12)
You can also use plt.gca() instead of using ax
import matplotlib.pyplot as plt
import matplotlib.ticker as mtick
plt.gca().yaxis.set_major_formatter(mtick.PercentFormatter(xmax=1.0))
import pandas as pd
import numpy as np
import matplotlib.pyplot as plt
import numpy as np
%matplotlib inline
%pylab inline
pylab.rcParams['figure.figsize'] = (12, 12)
import seaborn as sns
all_sets=pd.read_csv(r"C:\Users\champion\Desktop\ch02\AllSets-x.json")
df_list=[]
for i in all_sets.columns.values:
deck=allset[str(i)]["card"]
df = pd.DataFrame(deck)
df_list.append(df)
all_cards = pd.concat(df_list)
all_cards.cmc.fillna(0)
all_cards.reset_index(inplace=True)
all_cards.columns.values
when I exceuted my code,result didn't show.
And ipython notebook have IN[*] in right.
I try to plot a factorplot with seaborn
Here is my python code :
import numpy as np
import matplotlib.pyplot as plt
import matplotlib.dates as mdates
from matplotlib import style
import pandas as pd
import seaborn as sns
import io
style.use('ggplot')
#load dataset into df2 dataframe
df2 = pd.read_csv('C:/Users/Demonstrator/Downloads/power.csv',delimiter=';')
#drop NaN rows from df2 to build df_no_missing
df_no_missing = df2.dropna().copy()
df_no_missing.head()
df_no_missing['depassement'] = np.where((df_no_missing['P_ACT_KW'] - df_no_missing['P_SOUSCR']) < 0, 0, df_no_missing['P_ACT_KW'] - df_no_missing['P_SOUSCR'])
#build a factorplot
sns.factorplot(data=df_no_missing, x="TIMESTAMP", y="P_ACT_KW")
It is impossible to view a result the process seems busy.