How to plot to subplots during a loop - python

How can I create one plot for this loop?
I want to create some subplots according to i values taken from the loop. Do I need to create a new For/Loop which goes to each subplot? How can I do it?. This is my code:
fig, axes = plt.subplots(nrows=4, ncols=3)
fig.subplots_adjust(hspace=0.5)
fig.suptitle('Main plots')
for i in range(1,13):
month = [i]
DF_sub = DF[DF['months'].isin(month)]
out = pd.cut(DF_sub['new'], bins=[0, 0.25, 0.5, 0.75, 1], include_lowest=True)
out_norm = out.value_counts(sort=False, normalize=True)
ax = out_norm.plot.bar(rot=0, color="b", figsize=(6,4))
plt.title('Subplot -' + str(i))
Up to now, I just get the last one, but I am missing the previos i-s values from the loop

You need to pass the ax parameter to plot.bar to specify on which of your axes, returned from plt.subplots, the bar chart should be plotted, i.e.:
fig, axes = plt.subplots(nrows=4, ncols=3)
fig.subplots_adjust(hspace=0.5)
fig.suptitle('Main plots')
for i in range(1,13):
month = [i]
ax = axes[i - 1]
DF_sub = DF[DF['months'].isin(month)]
out = pd.cut(DF_sub['new'], bins=[0, 0.25, 0.5, 0.75, 1], include_lowest=True)
out_norm = out.value_counts(sort=False, normalize=True)
out_norm.plot.bar(rot=0, color="b", figsize=(6,4), ax=ax)
plt.title('Subplot -' + str(i))
If you don't pass the ax parameter, the bar chart will automatically be plotted on the currently active axis, which is the one most recently created unless set otherwise.

Related

How do I move one of the y-axis' to the right hand side in a line plot by using only Python's Matplotlib Library? [duplicate]

I'm currently trying to change the secondary y-axis values in a matplot graph to ymin = -1 and ymax = 2. I can't find anything on how to change the values though. I am using the secondary_y = True argument in .plot(), so I am not sure if changing the secondary y-axis values is possible for this. I've included my current code for creating the plot.
df.plot()
df.plot(secondary_y = "Market")
From your example code, it seems you're using Pandas built in ploting capabilities. One option to add a second layer is by using matplotlib directly like in the example "two_scales.py".
It uses
import matplotlib.pyplot as plt
fig, ax1 = plt.subplots()
ax1.plot(df["..."])
# ...
ax2 = ax1.twinx()
ax2.plot(df["Market"])
ax2.set_ylim([0, 5])
where you can change the y-limits.
Setting ylim on plot does not appear to work in the case of secondary_y, but I was able to workaround with this:
import pandas as pd
df = pd.DataFrame({'one': range(10), 'two': range(10, 20)})
ax = df['one'].plot()
ax2 = df['two'].plot(secondary_y=True)
ax2.set_ylim(-20, 50)
fig = ax.get_figure()
fig.savefig('test.png')
This is a solution for showing as much y-axes as data columns the dataframe has
colors = ['tab:blue',
'tab:orange',
'tab:green',
'tab:red',
'tab:purple',
'tab:brown',
'tab:pink',
'tab:gray',
'tab:olive',
'tab:cyan']
#X axe and first Y axe
fig, ax1 = plt.subplots()
x_label = str( dataFrame.columns[0] )
index = dataFrame[x_label]
ax1.set_xlabel(x_label)
ax1.set_xticklabels(dataFrame[x_label], rotation=45, ha="right")
firstYLabel = str( dataFrame.columns[1] )
ax1.set_ylabel(firstYLabel, color = colors[0])
ax1.plot(index, dataFrame[firstYLabel], color = colors[0])
ax1.tick_params(axis='y', labelcolor = colors[0])
#Creates subplots with independet y-Axes
axS =[]
def newTwix(label, ax1, index, dataFrame):
print(label)
actualPos = len(axS)
axS.append(ax1.twinx())
axS[actualPos].set_ylabel(label, color = colors[actualPos%10 + 1])
axS[actualPos].plot(index, dataFrame[label], color=colors[actualPos%10 + 1])
axS[actualPos].tick_params(axis='y', labelcolor=colors[actualPos%10 + 1])
identation = 0.075 #would improve with a dynamic solution
p = 1 + identation
for i in range(2,len(dataFrame.columns)):
newTwix(str(dataFrame.columns[i]), ax1, index, dataFrame)
if (len(axS) == 1):
axS[len(axS)-1].spines.right.set_position(("axes", p))
else:
p = int((p + identation)*1000)/1000
axS[len(axS)-1].spines.right.set_position(("axes", p))
fig.tight_layout() # otherwise the right y-label is slightly clipped
plt.subplots_adjust(left=0.04, right=0.674, bottom=0.1)
mng = plt.get_current_fig_manager()
mng.full_screen_toggle()
plt.show()
multiple y-axes with independent scales

setting axis labels in axes from matplotlib

I'm trying the following code to plot some graphs:
fig = plt.figure(figsize=(10, 20))
for idx, col in enumerate(['Pclass', 'Sex']):
ax = fig.add_subplot(2, 1, idx+1)
_ = ax.set(ylabel='Counts')
_ = sns.countplot(x=col, hue='Survived', data=full, ax=ax)
The output I'm getting is:
As you can see the y label is set as the seaborn countplot default label 'count', but I want to change it to 'Counts'. I've tried the axes method set_ylabel and set with ylabel argument and got no changes in the graphs. What am I doing wrong?
Can you try the following, changing the ylabel after plotting
fig = plt.figure(figsize=(10, 20))
for idx, col in enumerate(['Pclass', 'Sex']):
ax = fig.add_subplot(2, 1, idx+1)
sns.countplot(x=col, hue='Survived', data=full, ax=ax)
ax.set_ylabel('Counts')

How remove data points from text annotate?

The following script generates a scatter plot with annotated data points. I'd like remove circle markers from the plot and just show the labels.
fig, ax = Plot.subplots()
ax.scatter(Y0_mean, Y1_mean)
for i, txt in enumerate(features.playerCountry.unique()):
country_name = countries_code[countries_code.CountryCode == txt]
['ctr'].values[0].lower()
ax.annotate(country_name, (Y0_mean[i], Y1_mean[i]), xytext=(Y0_mean[i],
Y1_mean[i]), size=5)
ax.legend(fontsize=8)
fig.savefig(figPath + 'LocationAwareMeanFeatures_ctr'+str(lr), dpi=300)
There are 2 options. 1) don't call ax.scatter. This does mean you have to set the axes limits yourself in order to see the points.
y=[2.56422, 3.77284,3.52623,3.51468,3.02199]
x=[0.15, 0.3, 0.45, 0.6, 0.75]
n=[58,651,393,203,123]
fig, ax = plt.subplots()
# ax.scatter(x, y)
for i, txt in enumerate(n):
ax.annotate(txt, (x[i],y[i]))
ax.set_ylim(2.5,4)
plt.show()
or option 2) Call ax.scatter but remove the LineCollections that are added by doing:
y=[2.56422, 3.77284,3.52623,3.51468,3.02199]
x=[0.15, 0.3, 0.45, 0.6, 0.75]
n=[58,651,393,203,123]
fig, ax = plt.subplots()
points = ax.scatter(x, y)
for i, txt in enumerate(n):
ax.annotate(txt, (x[i],y[i]))
points.remove()
plt.show()
Both methods give the same result (provided you set the same axis limits in option 1 as you get in option 2):

Set up labels and legend using plt.subplots()

I am using plt.sublot to create a figure with 12 subplots that share the same data so I want to show the labels and legend for one of them. I am accessing and plotting the data from a dictionary that contains pandas dataframes each with 20 columns(labels). Here my code:
fig, axes = plt.subplots(nrows=3, ncols=4, sharex=True, sharey=True)
plt.subplots_adjust(left = 0.06, bottom = 0.1, right = 0.8, top=0.9,
wspace=0.15, hspace=0.15)
fig.suptitle('HMC Water Balance', fontsize = 20, y= 0.95, x=0.45)
axes[0,0].plot(HMC_hydrographs['outlet'])
axes[0,1].plot(HMC_hydrographs['Outlet00'])
axes[0,2].plot(HMC_hydrographs['Outlet01'])
axes[0,3].plot(HMC_hydrographs['Outlet02'], label =
'Q_total','Q_reset','Q_river_initial', ...'20th_column_name')
ax = axes[0,3]
ax.legend(loc=0, prop={'size':8})
axes[1,0].plot(HMC_hydrographs['Outlet03'])
ax = axes[1,0]
ax.set_ylabel('Flux (m$^3$/s)', labelpad=10, fontsize = 18)
axes[1,1].plot(HMC_hydrographs['Outlet04'])
axes[1,2].plot(HMC_hydrographs['Outlet05'])
axes[1,3].plot(HMC_hydrographs['Outlet06'])
axes[2,0].plot(HMC_hydrographs['Outlet07'])
axes[2,1].plot(HMC_hydrographs['Outlet08'])
ax = axes[2,1]
ax.set_xlabel('Time (days)', fontsize = 18)
ax.xaxis.set_label_coords(1.1,-0.2)
axes[2,2].plot(HMC_hydrographs['Outlet09'])
axes[2,3].plot(HMC_hydrographs['Outlet10'])
I get the error:
File "<ipython-input-249-7e4552c68d90>", line 8
axes[0,3].plot(HMC_hydrographs['Outlet02'], label =
'Q_total','Q_reset','Q_river_initial')
^
SyntaxError: positional argument follows keyword argument
For what I understand the label argument takes only one argument but I have more than one label!
Please help me understand how to call the labels so they show like when I plot a single graph like:
fig = plt.figure()
ax = HMC_hydrographs['Outlet01'].plot()
Individual plot showing the correct labels
Not sure of the reasoning behind it but the way I managed to get the legend to show was to specify the labels directly into the legend argument, not using the 'label' argument. Here is the code:
fig, axes = plt.subplots(nrows=3, ncols=4, sharex=True, sharey=True)
plt.subplots_adjust(left = 0.06, bottom = 0.1, right = 0.8, top=0.9,
wspace=0.15, hspace=0.15)
fig.suptitle('HMC Water Balance', fontsize = 20, y= 0.95, x=0.45)
axes[0,0].plot(HMC_hydrographs['outlet'])
axes[0,1].plot(HMC_hydrographs['Outlet00'])
axes[0,2].plot(HMC_hydrographs['Outlet01'])
axes[0,3].plot(HMC_hydrographs['Outlet02'])
ax = axes[0,3]
ax.legend(hydro_header, bbox_to_anchor=(1.05, 1), loc=2,
borderaxespad=0.)
axes[1,0].plot(HMC_hydrographs['Outlet03'])
ax = axes[1,0]
ax.set_ylabel('Flux (m$^3$/s)', labelpad=10, fontsize = 18)
axes[1,1].plot(HMC_hydrographs['Outlet04'])
axes[1,2].plot(HMC_hydrographs['Outlet05'])
axes[1,3].plot(HMC_hydrographs['Outlet06'])
axes[2,0].plot(HMC_hydrographs['Outlet07'])
axes[2,1].plot(HMC_hydrographs['Outlet08'])
ax = axes[2,1]
ax.set_xlabel('Time (days)', fontsize = 18)
ax.xaxis.set_label_coords(1.1,-0.2)
axes[2,2].plot(HMC_hydrographs['Outlet09'])
axes[2,3].plot(HMC_hydrographs['Outlet10'])
hydro_header contains a list with the column names(labels) for my plots that I got by using:
hydro_header = list(HMC_hydrographs['outlet'])
Final figure with the subplots and legend

How to set a secondary y-axis in Python

I'm currently trying to change the secondary y-axis values in a matplot graph to ymin = -1 and ymax = 2. I can't find anything on how to change the values though. I am using the secondary_y = True argument in .plot(), so I am not sure if changing the secondary y-axis values is possible for this. I've included my current code for creating the plot.
df.plot()
df.plot(secondary_y = "Market")
From your example code, it seems you're using Pandas built in ploting capabilities. One option to add a second layer is by using matplotlib directly like in the example "two_scales.py".
It uses
import matplotlib.pyplot as plt
fig, ax1 = plt.subplots()
ax1.plot(df["..."])
# ...
ax2 = ax1.twinx()
ax2.plot(df["Market"])
ax2.set_ylim([0, 5])
where you can change the y-limits.
Setting ylim on plot does not appear to work in the case of secondary_y, but I was able to workaround with this:
import pandas as pd
df = pd.DataFrame({'one': range(10), 'two': range(10, 20)})
ax = df['one'].plot()
ax2 = df['two'].plot(secondary_y=True)
ax2.set_ylim(-20, 50)
fig = ax.get_figure()
fig.savefig('test.png')
This is a solution for showing as much y-axes as data columns the dataframe has
colors = ['tab:blue',
'tab:orange',
'tab:green',
'tab:red',
'tab:purple',
'tab:brown',
'tab:pink',
'tab:gray',
'tab:olive',
'tab:cyan']
#X axe and first Y axe
fig, ax1 = plt.subplots()
x_label = str( dataFrame.columns[0] )
index = dataFrame[x_label]
ax1.set_xlabel(x_label)
ax1.set_xticklabels(dataFrame[x_label], rotation=45, ha="right")
firstYLabel = str( dataFrame.columns[1] )
ax1.set_ylabel(firstYLabel, color = colors[0])
ax1.plot(index, dataFrame[firstYLabel], color = colors[0])
ax1.tick_params(axis='y', labelcolor = colors[0])
#Creates subplots with independet y-Axes
axS =[]
def newTwix(label, ax1, index, dataFrame):
print(label)
actualPos = len(axS)
axS.append(ax1.twinx())
axS[actualPos].set_ylabel(label, color = colors[actualPos%10 + 1])
axS[actualPos].plot(index, dataFrame[label], color=colors[actualPos%10 + 1])
axS[actualPos].tick_params(axis='y', labelcolor=colors[actualPos%10 + 1])
identation = 0.075 #would improve with a dynamic solution
p = 1 + identation
for i in range(2,len(dataFrame.columns)):
newTwix(str(dataFrame.columns[i]), ax1, index, dataFrame)
if (len(axS) == 1):
axS[len(axS)-1].spines.right.set_position(("axes", p))
else:
p = int((p + identation)*1000)/1000
axS[len(axS)-1].spines.right.set_position(("axes", p))
fig.tight_layout() # otherwise the right y-label is slightly clipped
plt.subplots_adjust(left=0.04, right=0.674, bottom=0.1)
mng = plt.get_current_fig_manager()
mng.full_screen_toggle()
plt.show()
multiple y-axes with independent scales

Categories

Resources