add a line to matplotlib subplots - python-3.x

I would like to do a subplot of two figures with matplotlib and add a horizontal line in both. This is probably basic, but I don't know how to specify that one of the lines should be drawn in the first figure, they both end up in the last one. e.g.
import pandas as pd
import matplotlib.pyplot as plt
import numpy as np
%matplotlib inline
s1= pd.Series(np.random.rand(10))
s2= pd.Series(np.random.rand(10))
fig, axes = plt.subplots(nrows=2,ncols=1)
f1= s1.plot(ax=axes[0])
l1=plt.axhline(0.5,color='black',ls='--')
l1.set_label('l1')
f2= s1.plot(ax=axes[1])
l2=plt.axhline(0.7,color='red',ls='--')
l2.set_label('l2')
plt.legend()
axhline does not have "ax" as an argument, as the pandas plot function does. So this would work:
l1=plt.axhline(0.5,color='black',ls='--',ax=axes[0])
I read the examples in matplotlib and I tried with this other option that does not work either (probably for good reasons)
axes[0].plt.axhline(0.5,color='black',ls='--')
How should I do to draw lines in subplots? Ideally with a legend Thanks!

with the help of #Nick Becker I answered my own "syntax" question.
import pandas as pd
import matplotlib.pyplot as plt
import numpy as np
%matplotlib inline
s1= pd.Series(np.random.rand(10))
s2= pd.Series(np.random.randn(10))
fig, axes = plt.subplots(nrows=2,ncols=1)
f1= s1.plot(ax=axes[0],label='s1')
l1=axes[0].axhline(0.5,color='black',ls='--')
l1.set_label('l1')
axes[0].legend(loc='best')
f2= s1.plot(ax=axes[1],label='s2')
l2=axes[1].axhline(0.5,color='black',ls='--')
l2.set_label('l2')
axes[1].legend(loc='best')

Related

How to set x ticks for seaborn (python) line plot

I made a line plot chart for years 1960-2014 using seaborn but the xticks aren't correct. I want only intervals to appear (like 1960, 1970, 1980, etc).How do i adjust the xticks? I tried rotating it but it didn't seem to work. Here is my code:
#plot figure using sns
g=sns.relplot(x="Year", y="Indicator_Value",
data=Emissions_C_df,
kind="line",
style="Indicator_Name",
hue="Indicator_Name",
)
plt.show()
You can use a MaxNLocator from matplotlib.ticker for the major ticks (decades) and manually set specific minor ticks with a FixedLocator.
import seaborn as sns
import pandas as pd
import numpy as np
import matplotlib.pyplot as plt
from matplotlib.ticker import FixedLocator, MaxNLocator
a = np.arange(50)
d = {'Year': 1953+a, 'Indicator_Value': a}
df = pd.DataFrame(data=d)
g = sns.relplot(x="Year", y="Indicator_Value",
data=df,
kind="line")
ax = plt.gca()
ax.xaxis.set_major_locator(MaxNLocator(steps=[10]))
ax.xaxis.set_minor_locator(FixedLocator(range(1960,1970)))
plt.show()

How can I fix this error when trying to hide y axis labels on matplotlib axis

I'm attempting to hide the y-axis labels on a matplotlib axis. There seems to be an error that some of the labels are hidden but others are not. Is this a bug and is there a way to turn off all the labels? Is it the way that I'm plotting?
Note: I'm only showing half my plot the other half is a cartopy map.
import matplotlib.pyplot as plt
import cartopy.crs as ccrs
from cartopy.io.img_tiles import Stamen
import cartopy.io.shapereader as shpreader
from cartopy.feature import ShapelyFeature
import os
import pickle
import pandas as pd
import geopandas as gpd
from shapely.geometry import Point
from pathlib import Path
fig=plt.figure(figsize=(16,9), dpi=300, constrained_layout=True)
gs0=fig.add_gridspec(1,2)
gs00=gs0[0].subgridspec(10,1)
gs01=gs0[1].subgridspec(1,1)
data_dir='/data/files/'
ext_=f'*.pkl'
p=Path(data_dir).glob(ext_)
s=[str(i) for i in p]
for i,j in enumerate(s):
ax=fig.add_subplot(gs00[i,0])
f=pickle.load(open(j, 'rb'))
ax.loglog(f[0], f[2])
ax.margins(x=0)
ax.set_yticklabels([])
ax2=fig.add_subplot(gs01[0,0], projection=ccrs.PlateCarree())
I think you can use plt.tick_params()
See explainations from this topic Remove xticks in a matplotlib plot?
Modification for y-axis:
from matplotlib import pyplot as plt
plt.plot(range(10))
plt.tick_params(
axis='y', # changes apply to the y-axis
which='both', # both major and minor ticks are affected
left=False, # ticks along the left edge are off
labelleft=False) # labels along the left edge are off
plt.show()

Seaborn factorplot

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".

Scatter plot is not sort in matplotlib from csv file

My Code:
import pandas as pd
import matplotlib.pyplot as plt
df=pd.read_csv("linear_regression_dataset.csv", sep=";")
plt.scatter(df.Deneyim,df.Maas)
plt.xlabel("deneyim")
plt.ylabel("maas")
plt.show()
Is there a solution proposal?
The graphic I want:
sort the dataframe first and then you can plot
import pandas as pd
import matplotlib.pyplot as plt
df=pd.read_csv("linear_regression_dataset.csv", sep=";")
df['Mass']= df['Mass'].astype(int)
df.sort_values('Maas',inplace=True)
plt.scatter(df.Deneyim,df.Maas)
plt.xlabel("deneyim")
plt.ylabel("maas")
plt.show()

Matplotlib >1 Line Style, Same Line, Different Colors per Line

Given the following data frame and line graph:
import matplotlib.pyplot as plt
from cycler import cycler
import numpy as np
fig, ax=plt.subplots(1)
d=pd.DataFrame({'a':[1,2,3,4],
'b':[2,3,4,5],
'c':[3,4,5,6]})
colors=['r','g','b']
ax.set_prop_cycle(cycler('color', [colors]))
ax.plot(d[:3],'-ko',d[2:],'--ko')
plt.show()
You'll notice that I am trying to assign one color per line but it is not working. I also tried using the colors argument in ax.plot.
It seems like this should be straight forward.
Thanks in advance for any help on this.
There are two problems in your code.
the 'k' in '-ko' and '--ko' sets the colour to black, so we need to remove that
colors is already a list, but you have put it inside square brackets again in the call to set_prop_cycle, and thus made it into a nested list: [['r','g','b']]. Remove the square brackets there and it all works fine: ax.set_prop_cycle(cycler('color', colors))
So, your code will look like:
import matplotlib.pyplot as plt
import pandas as pd
from cycler import cycler
import numpy as np
fig, ax=plt.subplots(1)
d=pd.DataFrame({'a':[1,2,3,4],
'b':[2,3,4,5],
'c':[3,4,5,6]})
colors=['r','g','b']
ax.set_prop_cycle(cycler('color', colors))
ax.plot(d[:3],'-o',d[2:],'--o')
plt.show()

Resources