Python function for plotting multiple figures - python-3.x

The following function creates rows = 1 and columns = 9 plots.
def plot_percentiles(df_list, site_names=['a','b','c', 'd', 'e', 'f', 'i', 'j', 'k'],
xlabel=r"PA [$m^2 m^{-3}$]",ylim=(0,50),xlim=(0,0.6)):
figure, ax = plt.subplots(1,9, figsize=[10,3], squeeze=True)
figure.tight_layout()
for i, df in enumerate(df_list):
ax[i].fill_betweenx(x1=df["10th percentile"], x2=df["90th percentile"], y=df["Height"],
color="darkgreen", alpha=.5, linewidth=0)
ax[i].fill_betweenx(x1=df["25th percentile"], x2=df["75th percentile"], y=df["Height"],
color="darkgreen", alpha=.5, linewidth=0)
ax[i].plot(df["Median"], df["Height"], color = "darkgreen", linewidth=1)
ax[i].set_ylabel("Height [m]", fontsize=10)
ax[i].set_xlabel(xlabel, fontsize=10)
ax[i].set(ylim=ylim, xlim=xlim)
ax[i].set_title(site_names[i], fontsize=12)
ax[i].set_facecolor('white')
plt.show()
How do I change it to create rows = 3, columns = 3 plots? Simply changing figure, ax = plt.subplots(3,3, figsize=[10,3], squeeze=True) doesn't work.

Related

How to set markers with errorbars in different colours?

How to:
display symbols in the legend
colour markers in the same way as the errorbars (argument color gives an error: ValueError: RGBA sequence should have length 3 or 4
remove connecting lines - get only the scatter with errorbars
import pandas as pd
import matplotlib.pyplot as plt
from matplotlib.lines import Line2D # for legend handle
fig, ax = plt.subplots(figsize = (10,10))
times = [1, 2, 3, 4, 5]
rvs = [2, 4, 2, 4, 7]
sigma = [0.564, 0.6, 0.8, 0.8, 0.4]
rv_telescopes = ['A', 'B', 'A', 'C', 'C']
d = {'rv_times': times, 'rv_rvs': rvs, 'rv_sigma': sigma, 'rv_telescopes': rv_telescopes }
df = pd.DataFrame(data=d)
colors = {'A':'#008f00', 'B':'#e36500', 'C':'red'}
plt.errorbar(df['rv_times'], df['rv_rvs'], df['rv_sigma'], marker = '_', ecolor = df['rv_telescopes'].map(colors), color = df['rv_telescopes'].map(colors), zorder = 1, ms = 30)
handles = [Line2D([0], [0], marker='_', color='w', markerfacecolor=v, label=k, markersize=10) for k, v in colors.items()]
ax.legend(handles=handles, loc='upper left', ncol = 2, fontsize=14)
plt.show()
After edit
import matplotlib.pyplot as plt
from matplotlib.lines import Line2D # for legend handle
import pandas as pd
import numpy as np
times = [1, 2, 3, 4, 5]
rvs = [2, 4, 2, 4, 7]
sigma = [0.564, 0.6, 0.8, 0.8, 0.4]
rv_telescopes = ['A', 'B', 'A', 'C', 'C']
d = {'rv_times': times, 'rv_rvs': rvs, 'rv_sigma': sigma, 'rv_telescopes': rv_telescopes}
df = pd.DataFrame(data=d)
colors = {'A': '#008f00', 'B': '#e36500', 'C': 'red'}
fig, ax = plt.subplots(figsize=(10, 10))
ax.errorbar(df['rv_times'], df['rv_rvs'], df['rv_sigma'], color='none', ecolor=df['rv_telescopes'].map(colors) ,linewidth=1)
ax.scatter(df['rv_times'], df['rv_rvs'], marker='_', linewidth=3, color=df['rv_telescopes'].map(colors), s=1000)
for rv_teles in np.unique(df['rv_telescopes']):
color = colors[rv_teles]
df1 = df[df['rv_telescopes'] == rv_teles] # filter out rows corresponding to df['rv_telescopes']
ax.errorbar(df1['rv_times'], df1['rv_rvs'], df1['rv_sigma'],
color=color, ls='', marker='_', ms=30, linewidth=3, label=rv_teles)
ax.legend(loc='upper left', ncol=1, fontsize=14)
plt.show()
plt.errorbar() works very similar to plt.plot() with extra parameters. As such, it primarily draws a line graph, using a single color. The error bars can be given individual colors via the ecolor= parameter. The markers, however, get the same color as the line graph. The line graph can be suppressed via an empty linestyle. On top of that, plt.scatter() can draw markers with individual colors.
In order not the mix the 'object-oriented' with the 'functional interface', the following example code uses ax.errorbar() and ax.scatter().
import matplotlib.pyplot as plt
from matplotlib.lines import Line2D # for legend handle
import pandas as pd
import numpy as np
times = [1, 2, 3, 4, 5]
rvs = [2, 4, 2, 4, 7]
sigma = [0.564, 0.6, 0.8, 0.8, 0.4]
rv_telescopes = ['A', 'B', 'A', 'C', 'C']
d = {'rv_times': times, 'rv_rvs': rvs, 'rv_sigma': sigma, 'rv_telescopes': rv_telescopes}
df = pd.DataFrame(data=d)
colors = {'A': '#008f00', 'B': '#e36500', 'C': 'red'}
fig, ax = plt.subplots(figsize=(10, 10))
ax.errorbar(df['rv_times'], df['rv_rvs'], df['rv_sigma'], color='none', ecolor=df['rv_telescopes'].map(colors))
ax.scatter(df['rv_times'], df['rv_rvs'], marker='_', color=df['rv_telescopes'].map(colors), s=100)
handles = [Line2D([0], [0], linestyle='', marker='_', color=v, label=k, markersize=10) for k, v in colors.items()]
ax.legend(handles=handles, loc='upper left', ncol=1, fontsize=14)
plt.show()
A far easier approach would be to call ax.errorbar() multiple times, once for each color. This would automatically create appropriate legend handles:
for rv_teles in np.unique(df['rv_telescopes']):
color = colors[rv_teles]
df1 = df[df['rv_telescopes'] == rv_teles] # filter out rows corresponding to df['rv_telescopes']
ax.errorbar(df1['rv_times'], df1['rv_rvs'], df1['rv_sigma'],
color=color, ls='', marker='_', ms=30, label=rv_teles)
ax.legend(loc='upper left', ncol=1, fontsize=14)
plt.show()

Plot bar chart with last item in a secondary y-axis

I am using the code below to generate this plot:
However, I'd like to have only the last values of data1 and data 2 (Column F) to use a secondary y-axis, because they are much higher than the previous values. Would anybody know how could I do that? I appreciate the help!
x_label = ['A', 'B', 'C', 'D', 'E', 'F']
x_pos = np.arange(len(x_label))
data1 = [1,3,2,5,8,67]
data2 = [1,3,2,5,12,45]
# Build the plot
fig, ax = plt.subplots()
fontsize = 14
ax.bar(x_pos, data1, align='center', alpha=0.5, ecolor='black',
capsize=3, width=0.2, color='r', label='data1')
ax.bar(x_pos+0.2, data2, align='center', alpha=0.5, ecolor='black',
capsize=3, width=0.2, color='b', label='data2')
ax.set_xticks(x_pos)
ax.set_xticklabels(x_label, fontsize=fontsize)
plt.grid()
plt.show()
Your problem finds two solutions: one is the left-right axis with different limits (first code below). The second consists in using logarithmic scale. Note that this second solution is often preferred.
Solution 1: secondary axis (not the best)
import matplotlib.pyplot as plt
import numpy as np
x_label = ['A', 'B', 'C', 'D', 'E', 'F']
x_pos = np.arange(len(x_label))
data1 = [1,3,2,5,8,67]
data2 = [1,3,2,5,12,45]
# Build the plot
fig, ax = plt.subplots()
fontsize = 14
ax2 = ax.twinx()
# all but F
ax.bar(x_pos[:-1], data1[:-1], align='center', alpha=0.5, ecolor='black',
capsize=3, width=0.2, color='r', label='data1')
ax.bar(x_pos[:-1]+0.2, data2[:-1], align='center', alpha=0.5, ecolor='black',
capsize=3, width=0.2, color='b', label='data2')
# F
ax2.bar([x_pos[-1]], [data1[-1]], align='center', alpha=0.5, ecolor='black',
capsize=3, width=0.2, color='r', label='data1')
ax2.bar([x_pos[-1]+0.2], [data2[-1]], align='center', alpha=0.5, ecolor='black',
capsize=3, width=0.2, color='b', label='data2')
ax.set_xticks(x_pos)
ax.set_xticklabels(x_label, fontsize=fontsize)
plt.grid()
plt.show()
This produces the image below. The issue is that we cannot tell which bars are belonging to the right axis, unless we change the color but in this case it won't correspond anymore with the color code of the left-bars.
Solution 2: using a log scale (much cleaner)
import matplotlib.pyplot as plt
import numpy as np
x_label = ['A', 'B', 'C', 'D', 'E', 'F']
x_pos = np.arange(len(x_label))
data1 = [1,3,2,5,8,67]
data2 = [1,3,2,5,12,45]
# Build the plot
fig, ax = plt.subplots()
fontsize = 14
ax.bar(x_pos, data1, align='center', alpha=0.5, ecolor='black',
capsize=3, width=0.2, color='r', label='data1')
ax.bar(x_pos+0.2, data2, align='center', alpha=0.5, ecolor='black',
capsize=3, width=0.2, color='b', label='data2')
ax.set_xticks(x_pos)
ax.set_xticklabels(x_label, fontsize=fontsize)
ax.set_yscale('log')
plt.grid()
plt.show()
Which produces this image below. Now every bars are shown on the same axis, with a log scale axe. Data with large dynamic range are often shown with log scale.

Plot crosstab results using All row as benchmark lines

I have this sample dataframe:
test = pd.DataFrame({'cluster':['1','1','1','1','2','2','2','2','2','3','3','3'],
'type':['a','b','c','a','a','b','c','c','a','b','c','a']})
I use crosstab to produce a new dataframe and plot results:
pd.crosstab(test.cluster,test.type,normalize='index',margins=True).plot(kind='bar')
I would like to plot the row All as dotted horizontal benchmark lines of the same colour corresponding to each type to improve interpretation of the plot. Will appreciate help of this community!
import numpy as np
import pandas as pd
import matplotlib.pyplot as plt
test = pd.DataFrame(
{'cluster': ['1', '1', '1', '1', '2', '2', '2', '2', '2', '3', '3', '3'],
'type': ['a', 'b', 'c', 'a', 'a', 'b', 'c', 'c', 'a', 'b', 'c', 'a']})
tab = pd.crosstab(test.cluster, test.type, normalize='index', margins=True)
fig, ax = plt.subplots()
# find the default colors
prop_cycle = plt.rcParams['axes.prop_cycle']
colors = prop_cycle.by_key()['color']
# make a bar plot using all rows but the last
tab.iloc[:-1].plot(ax=ax, kind='bar', color=colors)
# draw the horizontal dotted lines
for y, c in zip(tab.loc['All'], colors):
ax.axhline(y=y, color=c, linestyle=':', alpha=0.5)
plt.show()

Label and color glyph in bokeh

I am trying out bokeh. It's quite fun so far. But I am not totally getting the hang of it. My goal is to make a simple but interactive scatter chart.
I have three main issues:
I want to label the scatter plot with names
I want the scatter to be colored in accordance to colors
I would love widgets where I can decide if the colors and names are displayed.
Here is what I have done so far. I tried to use LabelSet but I am stuck. Any help is greatly appreciated!
# interactive widget bokeh figure
from bokeh.io import curdoc
from bokeh.layouts import row, widgetbox
from bokeh.models import ColumnDataSource
from bokeh.models.widgets import Slider, TextInput
from bokeh.plotting import figure
from bokeh.models import Range1d, LabelSet, Label
import numpy as np
# data
x = [-4, 3, 2, 4, 10, 11, -2, 6]
y = [-3, 2, 2, 9, 11, 12, -5, 6]
names = ['A', 'B', 'C', 'D', 'E', 'F', 'G', 'H']
colors =['r', 'y', 'y', 'r', 'g', 'g', 'g', 'g']
p = figure(plot_height=400, plot_width=400, title="a little interactive chart",
tools="crosshair,pan,reset,save,wheel_zoom",
x_range=[-10, 10], y_range=[-10, 10])
labels = LabelSet(x='x', y='y', text='names', level='glyph',
x_offset=5, y_offset=5)
p.add_layout(labels)
p.circle(x, y, fill_color="red", line_color="red", size=6)
# Set up widgets
text = TextInput(title="title", value='a little interavtive chart')
# Set up callbacks
def update_title(attrname, old, new):
p.title.text = text.value
text.on_change('value', update_title)
# # Set up layouts and add to document
inputs = widgetbox(text, names)
curdoc().add_root(row(inputs, p, width=800))
curdoc().title = "Sliders"
Typically you use LabelSet by configuring it with the same data source as some glyph renderer. I find whenever sharing column data sources, its best to just also create them explicitly. Here is an updated version of your code that renders:
# interactive widget bokeh figure
from bokeh.io import curdoc
from bokeh.layouts import row, widgetbox
from bokeh.models import ColumnDataSource, Range1d, LabelSet, Label
from bokeh.models.widgets import Slider, TextInput
from bokeh.plotting import figure
# data
x = [-4, 3, 2, 4, 10, 11, -2, 6]
y = [-3, 2, 2, 9, 11, 12, -5, 6]
names = ['A', 'B', 'C', 'D', 'E', 'F', 'G', 'H']
colors =['r', 'y', 'y', 'r', 'g', 'g', 'g', 'g']
# create a CDS by hand
source = ColumnDataSource(data=dict(x=x, y=y, names=names, colors=colors))
p = figure(plot_height=400, plot_width=400, title="a little interactive chart",
tools="crosshair,pan,reset,save,wheel_zoom",
x_range=[-10, 10], y_range=[-10, 10])
# pass the CDS here, and column names (not the arrays themselves)
p.circle('x', 'y', fill_color="red", line_color="red", size=6, source=source)
# pass the CDS here too
labels = LabelSet(x='x', y='y', text='names', level='glyph',
x_offset=5, y_offset=5, source=source)
p.add_layout(labels)
# Set up widgets
text = TextInput(title="title", value='a little interavtive chart')
# Set up callbacks
def update_title(attrname, old, new):
p.title.text = text.value
text.on_change('value', update_title)
# # Set up layouts and add to document
inputs = widgetbox(text)
curdoc().add_root(row(inputs, p, width=800))
curdoc().title = "Sliders"
I also removed names from the widgetbox because widget boxes can only contain widget models. Maybe you intend to use the names in a Select widget or something?

How to add column next to Seaborn heat map

Given the code below, which produces a heat map, how can I get column "D" (the total column)
to display as a column to the right of the heat map with no color, just aligned total values per cell? I'm also trying to move the labels to the top. I don't mind that the labels on the left are horizontal as this does not occur with my actual data.
import matplotlib.pyplot as plt
import seaborn as sns
import pandas as pd
%matplotlib inline
df = pd.DataFrame(
{'A' : ['A', 'A', 'B', 'B','C', 'C', 'D', 'D'],
'B' : ['A', 'B', 'A', 'B','A', 'B', 'A', 'B'],
'C' : [2, 4, 5, 2, 0, 3, 9, 1],
'D' : [6, 6, 7, 7, 3, 3, 10, 10]})
df=df.pivot('A','B','C')
fig, ax = plt.subplots(1, 1, figsize =(4,6))
sns.heatmap(df, annot=True, linewidths=0, cbar=False)
plt.show()
Here's the desired result:
Thanks in advance!
I think the cleanest way (although probably not the shortest), would be to plot Total as one of the columns, and then access colors of the facets of the heatmap and change some of them to white.
The element that is responsible for color on heatmap is matplotlib.collections.QuadMesh. It contains all facecolors used for each facet of the heatmap, from left to right, bottom to top.
You can modify some colors and pass them back to QuadMesh before you plt.show().
There is a slight problem that seaborn changes text color of some of the annotations to make them visible on dark background, and they become invisible when you change to white color. So for now I set color of all text to black, you will need to figure out what is best for your plots.
Finally, to put x axis ticks and label on top, use:
ax.xaxis.tick_top()
ax.xaxis.set_label_position('top')
The final version of the code:
import matplotlib.pyplot as plt
from matplotlib.collections import QuadMesh
from matplotlib.text import Text
import seaborn as sns
import pandas as pd
import numpy as np
%matplotlib inline
df = pd.DataFrame(
{'A' : ['A', 'A', 'B', 'B','C', 'C', 'D', 'D'],
'B' : ['A', 'B', 'A', 'B','A', 'B', 'A', 'B'],
'C' : [2, 4, 5, 2, 0, 3, 9, 1],
'D' : [6, 6, 7, 7, 3, 3, 10, 10]})
df=df.pivot('A','B','C')
# create "Total" column
df['Total'] = df['A'] + df['B']
fig, ax = plt.subplots(1, 1, figsize =(4,6))
sns.heatmap(df, annot=True, linewidths=0, cbar=False)
# find your QuadMesh object and get array of colors
quadmesh = ax.findobj(QuadMesh)[0]
facecolors = quadmesh.get_facecolors()
# make colors of the last column white
facecolors[np.arange(2,12,3)] = np.array([1,1,1,1])
# set modified colors
quadmesh.set_facecolors = facecolors
# set color of all text to black
for i in ax.findobj(Text):
i.set_color('black')
# move x ticks and label to the top
ax.xaxis.tick_top()
ax.xaxis.set_label_position('top')
plt.show()
P.S. I am on Python 2.7, some syntax adjustments might be required, though I cannot think of any.

Resources