Delete individual legends from subplots in panda - legend

I would like to remove legends from individual subplots in panda. I created a bar chart and the #subplots. I would like to keep the titles of each subplot and remove the legends since they show the #same verbiage. I have tried several techniques, and even some that has me calling on each individual #subplot but am sure there is a simple solution. The fourth result image below is the one I need help with.
Here is my code so far:
import matplotlib.pyplot as plt
import pandas as pd
import scipy.stats as st
import numpy as np
mouse_metadata = "Mouse_metadata.csv"
study_results = "Study_results.csv"
mouse_metadata = pd.read_csv(mouse_metadata)
study_results = pd.read_csv(study_results)
study_data_combined = pd.merge(mouse_metadata,study_results, on= "Mouse ID")
pyma_sd = study_data_combined
pyma_sd.head()
pyma_sd_grouped = pyma_sd.groupby(["Drug Regimen"])
pyma_sd_grouped_mean = pyma_sd_grouped["Tumor Volume (mm3)"].mean()
pyma_sd_grouped_median = pyma_sd_grouped["Tumor Volume (mm3)"].median()
pyma_sd_grouped_variance = pyma_sd_grouped["Tumor Volume (mm3)"].var()
pyma_sd_grouped_std = pyma_sd_grouped["Tumor Volume (mm3)"].std()
pyma_sd_grouped_sem = pyma_sd_grouped["Tumor Volume (mm3)"].sem()
pyma_sd_grouped_stats = pd.DataFrame({ "Mean":pyma_sd_grouped_mean,
"Median": pyma_sd_grouped_median,
"Variance": pyma_sd_grouped_variance,
"Standard Error of Mean ": pyma_sd_grouped_sem})
print(" ","Stats of Tumor Volume")
print(pyma_sd_grouped_stats)
chart_pyma_sd_grouped_stats = pyma_sd_grouped_stats.plot(kind='bar', rot=50, figsize = (10, 6),
width = .8)
plt.title("Stats on Drug Regimen")Output 2
plt.xlabel("Drug Regimen")
plt.ylabel("Stats per Drug Regimen")
plt.tight_layout()
plt.show()
axes = pyma_sd_grouped_stats.plot.bar(rot=50, subplots=True, figsize = (10, 6), width = .75,)
axes[1].legend(loc=1)
plt.subplots_adjust(hspace=0.5)
plt.show()
**

Simply supply legend=False in your call to DataFrame.plot.bar.
import matplotlib.pyplot as plt
import pandas as pd
speed = [0.1, 17.5, 40, 48, 52, 69, 88]
lifespan = [2, 8, 70, 1.5, 25, 12, 28]
index = ['snail', 'pig', 'elephant', 'rabbit', 'giraffe', 'coyote', 'horse']
df = pd.DataFrame({'speed': speed, 'lifespan': lifespan}, index=index)
axes = df.plot.bar(rot=0, subplots=True, legend=False)
plt.show()
Compare the image above to the one generated in the doc.

Related

Python pyplot scatter is not using colors

I am trying to plot a scatter chart with pandas and matplotlib.pylot. The dots in the graph are only using one color, while the legend is showing there are three different colors for three different groups of data.
Below is my code and a copy of screen shot. You can see that only all dots are in green color. Could anyone point me why? What did I do wrong?
Thanks a lot in advance.
import pandas as pd
import matplotlib.pyplot as plt
data = {
'x':[1,2,3,4,1,3,7,5],
'y':[10, 20, 30, 40, 20, 30, 40, 80],
'label':['A', 'A','B','B','A','C','C','A']
}
df = pd.DataFrame(data)
plt.figure(figsize=(34,8))
fig,ax = plt.subplots()
#sns.scatterplot(data=df, hue='label', x='x', y='y')
for k, d in df.groupby('label'):
ax.scatter(df['x'], df['y'], label=k)
plt.legend()
plt.show()
You need to add colors mapping. Slight modifications to your code after adding colors dictionary:
import pandas as pd
import numpy as np
import matplotlib.pyplot as plt
import matplotlib.cm as cm
data = {
'x':[1,2,3,4,1,3,7,5],
'y':[10, 20, 30, 40, 20, 30, 40, 80],
'label':['A', 'A','B','B','A','C','C','A']
}
df = pd.DataFrame(data)
#plt.figure(figsize=(34,8))
fig,ax = plt.subplots()
df1 = df.groupby('label')
colors = iter(cm.rainbow(np.linspace(0, 1, len(df1.groups))))
for k, d in df1:
ax.scatter(d['x'], d['y'], label=k, color=next(colors))
plt.legend()
plt.show()
outputs the scatter plot as:
Is this your desired output?

who to plot stats.probplot in a grid?

I have a data frame with four columns I would like to plot the normality test for each column in a 2*2 grid, but it only plot one figure, and the else is empty.
import random
import pandas as pd
import numpy as np
from scipy import stats
import matplotlib.pyplot as plt
fig, axs = plt.subplots(2,2, figsize=(15, 6), facecolor='w', edgecolor='k')
fig.subplots_adjust(hspace = .5, wspace=.001)
data = {'col1': [random.randrange(1, 50, 1) for i in range(1000)], 'col2': [random.randrange(1, 50, 1) for i in range(1000)],'col3':[random.randrange(1, 50, 1) for i in range(1000)]
,'col4':[random.randrange(1, 50, 1) for i in range(1000)]}
df = pd.DataFrame(data)
for ax, d in zip(axs.ravel(), df):
ax=stats.probplot(df[d], plot=plt)
#ax.set_title(str(d))
plt.show()
is there a way to construct the subplot and the stats.probplot within a loop?
In your code, you need to change the for loop to this:
for ax, d in zip(axs.ravel(), df):
stats.probplot(df[d], plot=ax)
#ax.set_titl(str(d))
plt.show()
I hope this will help you move on.

pandas plot multiple df in canvas

I have difficulty with setting two different data plots inside one axis in tkinter canvas. Currently is being displayed only last plot, second is hidden.
Update:
Test example is working, but not in my original setup.
Problem is moved here.
Test example:
from tkinter import Tk, Canvas
import pandas as pd
import numpy as np
import matplotlib.pyplot as plt
from matplotlib.backends.backend_tkagg import FigureCanvasTkAgg
window = Tk()
window.geometry('800x600')
df = pd.DataFrame()
df['x'] = np.linspace(1, 10, 10)
df['y'] = np.random.randint(1, 10, 10)
df2 = pd.DataFrame()
df2['x'] = np.linspace(1, 10, 50)
df2['y'] = np.random.randint(1, 10, 50)
fig, ax = plt.subplots()
df.plot(x='x', y='y', kind='bar', ax=ax, width=1., figsize=(3, 2.5), legend=None)
df2.plot(x='x', y='y', kind='line', ax=ax, legend=None)
Canvas(window, background='white') # create canvas field
canvas_plot = FigureCanvasTkAgg(fig, window) # Draw area
canvas_plot.get_tk_widget().grid(column=1, row=6, padx=1, pady=10, rowspan=2, columnspan=3)
canvas_plot.draw() # draw canvas
window.mainloop()
Question:
How to make this plots to be displayed in one figure?

How to use same labels for shared x axes in Matplotlib?

I'm trying to translate two separate figure into one figure with sharing x axis. But there are something that I'm missing. For styling and ticks I normally use the codes
plt.xlabel(r'$\mathrm{2\theta\ (deg)}$')
plt.ylabel(r'$\mathrm{Intensity\ (a.u)}$')
plt.xlim(15,80)
plt.legend(loc=4,prop={'size':10})
params = {'legend.fontsize': 18,
'axes.labelsize': 18,
'axes.titlesize': 18,
'xtick.labelsize' :12,
'mathtext.fontset': 'cm',
'mathtext.rm': 'serif', }
matplotlib.rcParams.update(params)
plt.tick_params(
axis='both', which='both',
right='off', left='off',
top = 'off', bottom = 'off',
labelleft='off')
Now, I need to apply it for this shared figure. These includes following:
No ticks will be on figure.
Axes labels will be shared.
It would be better to load text files inside a loop.
What do I need to know for these improvements?
import matplotlib.pyplot as plt
from numpy import loadtxt
import matplotlib
f = plt.figure()
plt.subplots_adjust(hspace=0.001)
data = loadtxt("ES1.txt",float)
POS = data[:,0]
ESD = data[:,1]
ax1 = plt.subplot(311)
ax1.plot(POS, ESD, color="blue")
data = loadtxt("ES2.txt",float)
POS = data[:,0]
ESD = data[:,1]
ax2 = plt.subplot(312, sharex=ax1)
ax2.plot(POS, ESD, color="red")
yticklabels = ax1.get_yticklabels() + ax2.get_yticklabels()
plt.setp(yticklabels, visible=False)
plt.savefig('shared_xrd' + '.png', dpi=600, bbox_inches='tight')
It could be that the following code is more like you want it.
import matplotlib.pyplot as plt
import numpy as np
import matplotlib
params = {'legend.fontsize': 18,
'axes.labelsize': 18,
'axes.titlesize': 18,
'xtick.labelsize' :12,
'mathtext.fontset': 'cm',
'mathtext.rm': 'serif',
"xtick.bottom" : False,
"ytick.left" : False,
}
matplotlib.rcParams.update(params)
f, axes = plt.subplots(nrows=2, sharex=True)
plt.subplots_adjust(hspace=0.001, bottom=0.2)
colors=["blue", "red"]
for i in [0,1]:
data = np.loadtxt("ES{}.txt".format(i+1))
POS = data[:,0]
ESD = data[:,1]
axes[i].plot(POS, ESD, color=colors[i], label="data{}".format(i))
axes[i].legend(loc=4,prop={'size':10})
# make ticks invisble
axes[0].set_yticks([])
axes[1].set_yticks([])
plt.xlabel(r'$\mathrm{2\theta\ (deg)}$')
plt.xlim(15,80)
#create subplot just for placing the ylabel centered on all plots
shadowaxes = f.add_subplot(111, xticks=[], yticks=[], frame_on=False)
shadowaxes.set_ylabel(r'$\mathrm{Intensity\ (a.u)}$')
plt.savefig(__file__ + '.png', dpi=600, bbox_inches='tight')
plt.show()

Setting xticklabels, x axis formatting in matplotlib

I would like to format my x axis with the legend values at the mid point of each bar whilst retaining the gender group identification. I'd like lower the gender groups to sit below the other xticklabels for clarity.
To this point, I've added xticks but actually labeling them correctly and neatly is proving trickier.
from itertools import chain, cycle
import logging
import matplotlib
import matplotlib.pyplot as plt
import numpy as np
import pandas as pd
from pandas import DataFrame, Series
matplotlib.style.use("ggplot")
m = {"Males" : {"Yes": 2, "No": 8}}
w = {"Females": {"Yes": 3, "No": 7}}
data = {**m, **w}
df = DataFrame(data)
# relative freq table
df_ft = (df / df.sum() * 100).T
ax = plt.subplot(111)
df_ft.plot(ax=ax, kind="bar", ylim=(0, 90),
title="Would you prefer to work at home? (10 males, 10 females)",
rot=0)
plt.ylabel("Relative Frequency (%)")
midp = 0.125 # standard bar width/2
t_l = ax.get_xticks().tolist()
ticks = list(chain.from_iterable((t - midp, t + midp) for t in t_l))
ax.set_xticks(t_l + ticks)
plt.show()
The following might be what you're looking for.
from itertools import chain
import matplotlib
import matplotlib.pyplot as plt
from pandas import DataFrame
matplotlib.style.use("ggplot")
df = DataFrame({'Males': {'Yes': 2, 'No': 8}, 'Females': {'Yes': 3, 'No': 7}})
df_ft = (df / df.sum() * 100).T
ax = plt.subplot(111)
df_ft.plot(ax=ax, kind="bar", ylim=(0, 90),
title="Would you prefer to work at home? (10 males, 10 females)",
rot=0)
plt.ylabel("Relative Frequency (%)")
midp = 0.125 # standard bar width/2
t_l = ax.get_xticks().tolist()
ticks = list(chain.from_iterable((t - midp, t + midp) for t in t_l))
ax.set_xticks(t_l + ticks)
labels = [l for l in ax.get_xticklabels()]
for i,l in enumerate(labels[len(df_ft):]):
l.set_text(df_ft.columns[i % len(df_ft.columns)])
for i,l in enumerate(labels[:len(df_ft)]):
l.set_text("\n"+l.get_text())
ax.set_xticklabels(labels)
plt.savefig(__file__+".png")
plt.show()
Altair would do a great job here.
from altair import *
from pandas import DataFrame
df = DataFrame({'Males': {'Yes': 2, 'No': 8}, 'Females': {'Yes': 3, 'No': 7}})
df = df.stack().reset_index()
df.columns=['response','gender','count']
Vis #1
Chart(df).mark_bar().encode(x='gender',y='count',color='response').configure_cell(width=200, height=200)
Vis 2
Chart(df).mark_bar().encode(x=X('response', axis=False),
y=Y('count', axis=Axis(grid=False)),
color='response',
column=Column('gender', axis=Axis(axisWidth=1.0, offset=-8.0, orient='bottom'),scale=Scale(padding=30.0))).configure_cell(width=200, height=200).configure_facet_cell(strokeWidth=0)