Last bar of matplotlib barplot being filled half - python

I am using matplotlib to create a graph for my thesis. I am using the following code:
import numpy as np
import numpy as np
import matplotlib as mpl
mpl.use('pgf')
fig_width_pt = 390 # Get this from LaTeX using \showthe\columnwidth
inches_per_pt = 1.0/72.27 # Convert pt to inch
golden_mean = (np.sqrt(5)-1.0)/2.0 # Aesthetic ratio
fig_width = fig_width_pt*inches_per_pt # width in inches
fig_height = fig_width*golden_mean # height in inches
fig_size = [fig_width,fig_height]
pgf_with_latex = { # setup matplotlib to use latex for output
"pgf.texsystem": "pdflatex", # change this if using xetex or lautex
"text.usetex": True, # use LaTeX to write all text
"font.family": "serif",
"font.serif": [], # blank entries should cause plots to inherit fonts from the document
"font.sans-serif": [],
"font.monospace": [],
"axes.labelsize": 10, # LaTeX default is 10pt font.
"text.fontsize": 10,
"legend.fontsize": 8, # Make the legend/label fonts a little smaller
"xtick.labelsize": 8,
"ytick.labelsize": 8,
"figure.figsize": fig_size,
'axes.linewidth': .5,
'lines.linewidth': .5,
'patch.linewidth': .5,
"pgf.preamble": [
r"\usepackage[utf8x]{inputenc}", # use utf8 fonts becasue your computer can handle it :)
]
}
mpl.rcParams.update(pgf_with_latex)
import matplotlib.mlab as mlab
import matplotlib.pyplot as plt
import colorsys
def savefig(filename):
plt.savefig('{}.pgf'.format(filename))
plt.savefig('{}.pdf'.format(filename))
# setup
title = 'Clustering after salting out'
ylabel = '% of total colloids'
xlabel = 'Cluster size'
xticklabels = ('1','2','3','4','5','6','7','5+')
legend = ['10min', '20min','30min']
# read data from files
# skipped this part for legibility
# calculations with data, skipped for legibility
# plot it in a bar plot
N = len(ys[0])
ind = np.arange(0, N+1, 1.2) # the x locations for the groups
width = 0.35 # the width of the bars
# generate colours
hsv_colours = [(x*1.0/N, 0.8, 0.8) for x in range(N)]
rgb_colours = map(lambda x: colorsys.hsv_to_rgb(*x), hsv_colours)
fig, ax = plt.subplots()
rects = [ax.bar([x+i*width for x in ind], y, width, color=rgb_colours[i], yerr=errors_percentage[i]) for i,y in enumerate(ys)]
# add some info
ax.set_ylabel(ylabel)
ax.set_xlabel(xlabel)
ax.set_title(title)
ax.set_xticks(ind+width)
ax.set_xticklabels(xticklabels)
ax.axis([0,7,0,60])
ax.legend(rects, legend)
savefig('tpm_cluster_statistics')
The output produced looks like this:
As you can see, the last bar of the bar plot is not totally filled. Do I need some other setting to get it working?
The goal is to create a PGF file for inclusion in a LaTex document. The PDF file is just for previewing. The partially filled bar is both in the PDF and in the PGF file.
Any help is greatly appreciated!
Edit:
In reply to tcaswell: this is a minimum working example that you can try on your computer:
import numpy as np
import numpy as np
import matplotlib as mpl
mpl.use('pgf')
pgf_with_latex = { # setup matplotlib to use latex for output
"pgf.texsystem": "pdflatex", # change this if using xetex or lautex
"text.usetex": True, # use LaTeX to write all text
}
mpl.rcParams.update(pgf_with_latex)
import matplotlib.mlab as mlab
import matplotlib.pyplot as plt
import colorsys
def savefig(filename):
plt.savefig('{}.pgf'.format(filename))
plt.savefig('{}.pdf'.format(filename))
# setup
title = 'Title'
ylabel = 'x'
xlabel = 'y'
xticklabels = ('1','2','3','4','5','6','7','8')
legend = ['1', '2','3']
#data
ys = [[51.63593099345628, 28.911362284354553, 12.135633551457465, 4.521118381915526, 1.189767995240928, 0.7138607971445567, 0.41641879833432477, 0.4759071980963712], [46.66359871145882, 21.445006902899216, 14.496088357109988, 7.363092498849516, 4.1417395306028535, 3.313391624482283, 0.0, 2.577082374597331], [52.642595499738356, 22.39665096807954, 12.087912087912088, 7.744636316064887, 2.3547880690737837, 1.5698587127158554, 0.3663003663003663, 0.837257980115123]]
# plot it in a bar plot
N = len(ys[0])
ind = np.arange(0, N+1, 1.2) # the x locations for the groups
width = 0.35 # the width of the bars
fig, ax = plt.subplots()
rects = [ax.bar([x+i*width for x in ind], y, width) for i,y in enumerate(ys)]
# add some info
ax.set_ylabel(ylabel)
ax.set_xlabel(xlabel)
ax.set_title(title)
ax.set_xticks(ind+width)
ax.set_xticklabels(xticklabels)
ax.axis([0,7,0,60])
ax.legend(rects, legend)
savefig('tpm_cluster_statistics')
Then the result looks like this:
But when I remove these lines:
mpl.use('pgf')
pgf_with_latex = { # setup matplotlib to use latex for output
"pgf.texsystem": "pdflatex", # change this if using xetex or lautex
"text.usetex": True, # use LaTeX to write all text
}
mpl.rcParams.update(pgf_with_latex)
And just show the output using plt.show(), the result does look correct.

I don't know what really causes it, but switching to just PDF output without the mpl.use('pgf') fixes the issue. For now I'll just stick to PDF, which is fine too. Thanks for all the help!

Related

How to adjust Matplotlib colorbar range in xarray plot?

I have a plot that looks like this
I cannot understand how to manually change or set the range of data values for the colorbar. I would like to experiment with ranges based on the data values shown in the plots and change the colorbar to (-4,4). I see that plt.clim, vmin and vmax are functions to possibly use.
Here is my code:
import cdsapi
import xarray as xr
import matplotlib.pyplot as plt
import numpy as np
import cartopy.crs as ccrs
# Also requires cfgrib library.
c = cdsapi.Client()
url = c.retrieve(
'reanalysis-era5-single-levels-monthly-means',
{
'product_type': 'monthly_averaged_reanalysis',
'format': 'grib',
'variable': ['100m_u_component_of_wind','100m_v_component_of_wind'],
'year': ['2006','2007','2008','2009','2010','2011','2012','2013','2014','2015','2016','2017','2018','2019','2020','2021'],
'month': ['01','02','03','04','05','06','07','08','09','10','11','12'],
'time': '00:00',
'grid': [0.25, 0.25],
'area': [70.00, -180.00, -40.00, 180.00],
},
"C:\\Users\\U321103\\.spyder-py3\\ERA5_MAPPING\\100m_wind_U_V.grib")
path = "C:\\Users\\U321103\\.spyder-py3\\ERA5_MAPPING\\100m_wind_U_V.grib"
ds = xr.load_dataset(path, engine='cfgrib')
wind_abs = np.sqrt(ds.u100**2 + ds.v100**2)
monthly_means = wind_abs.mean(dim='time')
wind_abs_clim = wind_abs.sel(time=slice('2006-01','2020-12')).groupby('time.month').mean(dim='time') # select averaging period
wind_abs_anom = ((wind_abs.groupby('time.month') / wind_abs_clim))-1 #deviation from climo
fg = wind_abs_anom.sel(time=slice('2021-01',None)).groupby('time.month').mean(dim='time').plot(col='month',
col_wrap=3,transform=ccrs.PlateCarree(),
cbar_kwargs={'orientation':'horizontal','shrink':0.6, 'aspect':40,'label':'Percent Deviation'},robust=False,subplot_kws={'projection': ccrs.Mercator()})
fg.map(lambda: plt.gca().coastlines())
I was able to reproduce your figure and found that I could add vmin and vmax as shown below. For some reason that meant I also had to specify the colormap, otherwise I ended up with viridis. But the code below works for me (with a bit of refactoring as I got it working — the only material change here is in the plotting section at the bottom).
First, loading the data:
import cdsapi
c = cdsapi.Client()
params = {
'product_type': 'monthly_averaged_reanalysis',
'format': 'grib',
'variable': ['100m_u_component_of_wind', '100m_v_component_of_wind'],
'year': [f'{n}' for n in range(2006, 2022)],
'month': [f'{n:02d}' for n in range(1, 13)],
'time': '00:00',
'grid': [0.25, 0.25],
'area': [70.00, -180.00, -40.00, 180.00],
}
path = '100m_wind_U_V.grib'
url = c.retrieve('reanalysis-era5-single-levels-monthly-means',
params,
path,
)
Then there's the data pipeline:
import xarray as xr
import numpy as np
# Also need cfgrib library.
ds = xr.load_dataset(path, engine='cfgrib')
wind_abs = np.sqrt(ds.u100**2 + ds.v100**2)
monthly_means = wind_abs.mean(dim='time')
wind_abs_clim = (wind_abs.sel(time=slice('2006-01','2020-12'))
.groupby('time.month')
.mean(dim='time'))
wind_abs_anom = ((wind_abs.groupby('time.month') / wind_abs_clim)) - 1
Finally the plotting:
import cartopy.crs as ccrs
import matplotlib.pyplot as plt
cbar_kwargs = {'orientation':'horizontal', 'shrink':0.6, 'aspect':40, 'label':'Percent Deviation'}
subplot_kws = {'projection': ccrs.Mercator()}
fg = (wind_abs_anom.sel(time=slice('2021-01', None))
.groupby('time.month')
.mean(dim='time')
.plot(col='month',
col_wrap=3,
transform=ccrs.PlateCarree(),
cmap='RdBu_r', vmin=-3, vmax=3, # <-- New bit.
cbar_kwargs=cbar_kwargs,
robust=False,
subplot_kws=subplot_kws
))
fg.map(lambda: plt.gca().coastlines())
Sometimes I'll use a percentile to control the values for vmin and vmax automatically, like max_ = np.percentile(data, 99), then vmin=-max_, vmax=max_. This deals nicely with outliers that stretch the colormap, but it requires you to be able to calculate those values before making the plot.
If you want to start having more control over the plot, it might be a good idea to stop using the xarray plotting interface and use matplotlib and cartopy directly. Here's what that might look like (replacing all of the plotting code above):
import cartopy.crs as ccrs
import matplotlib.pyplot as plt
sel = wind_abs_anom.sel(time=slice('2021-01', None))
left, *_, right = wind_abs_anom.longitude
top, *_, bottom = wind_abs_anom.latitude # Min and max latitude.
extent = [left, right, bottom, top]
fig, axs = plt.subplots(nrows=2, ncols=3,
figsize=(15, 6),
subplot_kw={'projection': ccrs.PlateCarree()},
)
for ax, (month, group) in zip(axs.flat, sel.groupby('time.month')):
mean = group.mean(dim='time')
im = ax.imshow(mean,
transform=ccrs.PlateCarree(),
extent=extent,
cmap='RdBu_r', vmin=-3, vmax=3)
ax.set_title(f'month = {month}')
ax.coastlines()
cbar_ax = fig.add_axes([0.2, 0.0, 0.6, 0.05]) # Left, bottom, width, height.
cbar = fig.colorbar(im, cax=cbar_ax, extend='both', orientation='horizontal')
cbar.set_label('Percent deviation')
plt.show()
For some reason, when I try to use ccra.Mercator() for the map, the data gets distorted; maybe you can figure that bit out.

Method to adjust errorbars in seaborn regplot

Background
I am plotting my data using sns.regplot (seaborn 0.11.0, Python 3.8.5). I use the argument 'x_estimator' to plot the mean of each category shown on the x-axis, and for each point on the x-axis I have an errorbar which is bootstrapped using the sns.regplot arguments 'ci' and 'boot'.
Since this plot needs to have a specific dots per inch (DPI) of 800, I needed to readjust the scaling of the original plot to make sure the desired DPI was obtained.
Problem
Due to the rescaling, my errorbars appear to be rather 'wide'. I would like to make them less wide, and if it is possible, I would also like to add caps on the errorbars. I have included my code below using a randomly generated dataset. Running this code, one can see that the plot that I obtain has the correct DPI, but the errorbars are too wide.
Edit for clarification
I am fine with the confidence intervals (CI) in itself. My only worry is that the CIs are a bit too wide. This is probably some formatting issue. I already checked line_kws and scatter_kws but I can't find any formatting options for the CIs. My desired output looks like this: the same bars, but not as 'heavy' as the original ones.
#!/usr/bin/env python3
# -*- coding: utf-8 -*-
#%%
import matplotlib.pyplot as plt
import numpy as np
import pandas as pd
import seaborn as sns
from matplotlib import rcParams
#%%
# seaborn params
sns.set_style("ticks")
sns.set_context("paper")
# plotting params
rcParams['font.family'] = 'Times New Roman'
rcParams['axes.titlesize'] = 6
rcParams['axes.labelsize'] = 5
rcParams['xtick.labelsize'] = 5
rcParams['ytick.labelsize'] = 5
#%%
# some toy data into to pandas dataframe
df = pd.DataFrame({'Y': np.random.normal(0, 1, (800,)),
'X': np.repeat(range(1, 9), 100),
'Condition': np.tile(["A", "B"], 400)},
index=range(800))
#%%
# make a subplot with 1 row and 2 columns
fig, ax_list = plt.subplots(1, 2,
sharex = True,
sharey = True,
squeeze = True)
# A condition
g = sns.regplot(x = "X",
y = "Y",
data = df.loc[df["Condition"] == "A"],
x_estimator = np.mean,
x_ci = "ci",
ci = 95,
n_boot = 5000,
scatter_kws = {"s":15},
line_kws = {'lw': .75},
color = "darkgrey",
ax = ax_list[0])
# B condition
g = sns.regplot(x = "X",
y = "Y",
data = df.loc[df["Condition"] == "B"],
x_estimator = np.mean,
x_ci = "ci",
ci = 95,
n_boot = 5000,
scatter_kws = {"s":15},
line_kws = {'lw': .75},
color = "black",
ax = ax_list[1])
# figure parameters (left figure)
ax_list[0].set_title("A condition")
ax_list[0].set_xticks(np.arange(1, 9))
ax_list[0].set_xlim(0.5, 8.5)
ax_list[0].set_xlabel("X")
ax_list[0].set_ylabel("Y")
# figure parameters (right figure)
ax_list[1].set_title("B condition")
ax_list[1].set_xlabel("X")
ax_list[1].set_ylabel("Y")
# general title
fig.suptitle("Y ~ X", fontsize = 8)
#%%
# set the size of the image
fig.set_size_inches(3, 2)
# play around until the figure is satisfactory (difficult due to high DPI)
plt.subplots_adjust(top=0.85, bottom=0.15, left=0.185, right=0.95, hspace=0.075,
wspace=0.2)
# save as tiff with defined DPI
plt.savefig(fname = "test.tiff", dpi = 800)
plt.close("all")
Try setting ci parameters in sns.regplot to a lower value
I just ran into this problem myself and found a hacky solution.
It looks like keyword arguments for the confidence intervals (CI) are not yet exposed to the user (see here). But we can see that it sets the CI line width to 1.75 * linewidth from mpl.rcParams. So I think you can get what you want by hacking a matplotlib rcParams context manager.
import matplotlib as mpl
import numpy as np
import seaborn as sns
# Insert other code from your question here
# to get your dataframe
df = ...
# Play around with this number until you get the desired line width
line_width_reduction = 0.5
linewidth = mpl.rcParams["lines.linewidth"]
with mpl.rc_context({"lines.linewidth": line_width_reduction * linewidth}):
g = sns.regplot(
x="X",
y="Y",
data=df.loc[df["Condition"] == "A"],
x_estimator=np.mean,
x_ci="ci",
ci=95,
n_boot=5000,
scatter_kws={"s":15},
line_kws={'lw': .75},
color="darkgrey",
ax=ax_list[0]
)

Matplotlib : How to populate the below chart having all the x-axis labels and grid lines accordingly?

data = {'tenor': ['1w','1m','3m','6m','12m','1y','2y','3y','4y','5y','6y','7y','10y','15y','20y','25y','30y','40y','50y'],'rate_s': [0.02514, 0.026285, 0.0273, 0.0279, 0.029616, 0.026526, 0.026028, 0.024, 0.025958,0.0261375, 0.026355, 0.026, 0.026898, 0.0271745, 0.02741, 0.027, 0.0275, 0.0289,0.0284],'rate_t':[ 0.02314, 0.024285, 0.0253,0.0279, 0.028616, 0.026526,0.027028, 0.024, 0.025958,0.0271375, 0.02355, 0.026, 0.024898, 0.0271745, 0.02641,0.027, 0.0255, 0.0289,0.0284]}
I want to produce the chart in blue with the same format like below. I tried this piece of code but results are not satisfactory (chart in white). It also not showing all x-axis labels. Please suggest.
ax = plt.gca()
df.plot(kind='line',x='tenor',y='rate_s',marker='o',color='green',ax=ax)
df.plot(kind='line',x='tenor',y='rate_y',marker='o', color='red', ax=ax)
ax.minorticks_on()
ax.grid(which='major',linestyle='-', linewidth='0.5', color='blue')
ax.grid(which='minor', linestyle=':', linewidth='0.5', color='black')
plt.show()
This is following the discussions in the comments.
There are a couple parts, the full example is at the bottom.
Style
One of your questions was how to change the style of the plot. This can be done with the following code:
import matplotlib.pyplot as plt
plt.style.use('seaborn-darkgrid')
there are many possible styles, and you can even create your own style if you wish. To see all possible styles see: the documentation. To list all styles use plt.style.available
Custom Ticker
For the custom tickers: you can use FixedLocator or if you know it is log or symlog, then matplotlib has a built-in locator. See the matplotlib doc for scales
You can use FixedLocator to set up the axis, to be separated. i.e. the following code will give you what you want.
import numpy as np
import matplotlib.pyplot as plt
import matplotlib.ticker as ticker
X = np.arange(0, 2000)
Y = np.arange(0, 2000)
def convert(date):
if 'w' in date:
return 7*int(date[:-1])
if 'm' in date:
return 30*int(date[:-1])
if 'y' in date:
return 30*int(date[:-1]) + 360
ticks = [convertdate(d) for d in tenor]
plt.style.use('seaborn-darkgrid')
ax = plt.axes()
t = ticker.FixedLocator(locs=ticks)
ax.xaxis.set_ticklabels(tenor)
ax.xaxis.set_major_locator(t)
# ax.xaxis.set_minor_locator(ticker.MultipleLocator(3))
plt.plot(X, Y, c = 'k')
plt.show()
Which produces:
Specific Case
For your specific case, you probably want the custom tickers to be on a specific interval (i.e. smallest of rate_t, biggest of rate_t).
Thus you would need to change the convert function to be as following:
import numpy as np
import matplotlib.pyplot as plt
import matplotlib.ticker as ticker
x = data['rate_t']
y = data['rate_s']
def get_indices(date):
if 'w' in date:
return 7*int(date[:-1])
if 'm' in date:
return 30*int(date[:-1])
if 'y' in date:
return 30*int(date[:-1]) + 360
def convert(indices):
x = np.linspace(min(data['rate_t']), max(data['rate_t']), indices[-1] + 1)
return x[indices]
indices = [get_indices(d) for d in tenor]
ticks = convert(indices)
plt.style.use('seaborn-darkgrid')
ax = plt.axes()
t = ticker.FixedLocator(locs=ticks)
ax.xaxis.set_ticklabels(tenor)
ax.xaxis.set_major_locator(t)
# ax.xaxis.set_minor_locator(ticker.MultipleLocator(3))
plt.plot(x, y, c = 'k')
plt.show()
(assuming the data['rate_s'] and data['rate_t'] are as is and without processing)
Which would produce this:
Let me know if you have any questions.

Python: Changing visual parameters of ptitprince repo derived from seaborn and matplotlib

I am using a github repository called ptitprince, which is derived from seaborn and matplotlib, to generate graphs.
For example, this is the code using the ptitprince repo:
# coding: utf8
import pandas as pd
import ptitprince as pt
import seaborn as sns
import os
import matplotlib.pyplot as plt
#sns.set(style="darkgrid")
#sns.set(style="whitegrid")
#sns.set_style("white")
sns.set(style="whitegrid",font_scale=2)
import matplotlib.collections as clt
df = pd.read_csv ("u118phag.csv", sep= ",")
df.head()
savefigs = True
figs_dir = 'figs'
if savefigs:
# Make the figures folder if it doesn't yet exist
if not os.path.isdir('figs'):
os.makedirs('figs')
#automation
f, ax = plt.subplots(figsize=(4, 5))
#f.subplots_adjust(hspace=0,wspace=0)
dx = "Treatment"; dy = "score"; ort = "v"; pal = "Set2"; sigma = .2
ax=pt.RainCloud(x = dx, y = dy, data = df, palette = pal, bw = sigma,
width_viol = .6, ax = ax, move=.2, offset=.1, orient = ort, pointplot = True)
f.show()
if savefigs:
f.savefig('figs/figure20.png', bbox_inches='tight', dpi=500)
which generates the following graph
The raw code not using ptitprince is as follows and produces the same graph as above:
# coding: utf8
import pandas as pd
import ptitprince as pt
import seaborn as sns
import os
import matplotlib.pyplot as plt
#sns.set(style="darkgrid")
#sns.set(style="whitegrid")
#sns.set_style("white")
sns.set(style="whitegrid",font_scale=2)
import matplotlib.collections as clt
df = pd.read_csv ("u118phag.csv", sep= ",")
df.head()
savefigs = True
figs_dir = 'figs'
if savefigs:
# Make the figures folder if it doesn't yet exist
if not os.path.isdir('figs'):
os.makedirs('figs')
f, ax = plt.subplots(figsize=(7, 5))
dy="Treatment"; dx="score"; ort="h"; pal = sns.color_palette(n_colors=1)
#adding color
pal = "Set2"
f, ax = plt.subplots(figsize=(7, 5))
ax=pt.half_violinplot( x = dx, y = dy, data = df, palette = pal, bw = .2, cut = 0.,
scale = "area", width = .6, inner = None, orient = ort)
ax=sns.stripplot( x = dx, y = dy, data = df, palette = pal, edgecolor = "white",
size = 3, jitter = 1, zorder = 0, orient = ort)
ax=sns.boxplot( x = dx, y = dy, data = df, color = "black", width = .15, zorder = 10,\
showcaps = True, boxprops = {'facecolor':'none', "zorder":10},\
showfliers=True, whiskerprops = {'linewidth':2, "zorder":10},\
saturation = 1, orient = ort)
if savefigs:
f.savefig('figs/figure21.png', bbox_inches='tight', dpi=500)
Now, what I'm trying to do is to figure out how to modify the graph so that I can (1) move the plots closer together, so there is not so much white space between them, and (2) shift the x-axis to the right, so that I can make the distribution (violin) plot wider without it getting cut in half by the y-axis.
I have tried to play around with subplots_adjust() as you can see in the first box of code, but I receive an error. I cannot figure out how to appropriately use this function, or even if that will actually bring the different graphs closer together.
I also know that I can increase the distribution size by increasing this value width = .6, but if I increase it too high, the distribution plot begins to being cut off by the y-axis. I can't figure out if I need to adjust the overall plot using the plt.subplots,or if I need to move each individual plot.
Any advice or recommendations on how to change the visuals of the graph? I've been staring at this for awhile, and I can't figure out how to make seaborn/matplotlib play nicely with ptitprince.
You may try to change the interval of X-axis being shown using ax.set_xbound (put a lower value than you currently have for the beginning).

Windrose legend bracket format and loc

Below I have plotted a wind rose using Windrose based on this. Firstly, the legend is covering part of the rose but when I try to use loc to set its location the legend disappears.
Secondly, the legend closing brackets are wrong i.e. [0.0 : 1.0[ any idea how I fix this to [0.0 : 1.0]
code:
from windrose import WindroseAxes
import matplotlib.cm as cm
import matplotlib.pyplot as plt
import numpy as np
import pandas as pd
df = pd.read_csv("C:\2007_GG_wind rose.csv")
ws_SAR = df[' SARwind_10m']
wd_SAR = df['wind direction SAR model_int']
ws_mde = df['gg_mde']
wd_mde = df['wind direction MDE ']
ax=WindroseAxes.from_ax()
ax.bar(wd_SAR,ws_SAR,normed=True, opening=0.8, edgecolor='white')
ax.set_legend()
plt.title("SAR 10m U",y=1.08) #y=1.08 raises the title
Copy the original windrose.py from your python folder to your desired working directory. Name the copy somehow else, e.g. windrose_edit.py. Edit the file and look for the get_labels() function. I edited like this, but you can adopt it to your purpose.
def get_labels():
labels = np.copy(self._info['bins'])
labels = ["%.1f : %0.1f" %(labels[i], labels[i+1]-0.1) \
for i in range(len(labels)-1)]
return labels
also you can increase the fontsize of the legend some lines below.
def set_legend(self):
l = self.legend(borderaxespad=-0.10)
plt.setp(l.get_texts(), fontsize=12)
Finally import your edited file for example
import windrose_edit as wind2
and use it with
winddir_ = yourdata
windspeed_ = yourdata
fig = plt.figure(figsize=(12, 8), dpi=100, facecolor='w', edgecolor='w')
rect = [0.1, 0.1, 0.8, 0.8]
ax = wind2.WindroseAxes(fig, rect, facecolor='w')
ax.contourf(winddir_, windspeed_, bins=6, normed=True, cmap=cm.RdYlBu_r)
ax.set_legend()
it's not beautiful although a relatively fast and lazy workaround

Categories