setting error bar thickness in seaborn - python

I would like to get fine error bars using seaborn's regplot (finer than the correlation line).
The code below (adapted from here) sorts this out, but in a rather cumbersome way. Is there a more direct way to reach this, maybe via kws?
with matplotlib.rc_context({"lines.linewidth": 1}):
sns.regplot('A', 'B', data=my_dataframe, x_jitter=10., ci=68, \
ax=ax, x_estimator=np.mean, \
scatter_kws={"s":150}, \
line_kws={"linewidth": 2 })

There is a pull request incoming that I am working on that will allow you to specify the linewidth and allow you to determine if there should be caps on the error bars. See
https://github.com/mwaskom/seaborn/pull/898
If you clone my repository as a temporary fix, you should be able to specify it right now by adding the keyword conf_lw but I hope to have this integrated with unit tests written shortly.

Related

Unexpected behavior of pyplot in the seaborn library. Bug?

I'm trying to understand the pointplot function (Link to pointplot doc) to plot error bars.
Setting the 'errorbar' argument to 'sd' should plot the standard deviation along with the mean. But calculating the standard deviation manually results in a different value.
I used the example provided in the documentation:
import seaborn as sns
df = sns.load_dataset("penguins")
ax = sns.pointplot(data=df, x="island", y="body_mass_g", errorbar="sd")
data = ax.lines[1].get_ydata()
print(data[1] - data[0]) # prints 248.57843137254895
sd = df[df['island'] == 'Torgersen']['body_mass_g'].std()
print(sd) # prints 445.10794020256765
I expected both printed values to be the same, since both data[1] - data[0] and sd should be equal to the standard deviation of the variable 'body_mass_g' for the category 'Torgersen'. Other standard deviation provided by sns.pointplot are also not as expected.
I must be missing something obvious here but for the life of me I can't figure it out.
Appreciate any help. I tested the code locally and in google colab with the same results.
My PC had an outdated version of seaborn (0.11.2), where the argument 'errorbar' was named 'ci'. Using the correct argument resolves the problem. Strangly google Colab also uses version 0.11.2, contrary to their claim that they auto update their packages.

Plotnine : Secondary y-axis (dual axes)

I am using python's wonderful plotnine package. I would like to make a plot with dual y-axis, let's say Celsius on the left axis and Fahrenheit on the right.
I have installed the latest version of plotnine, v0.10.1.
This says the feature was added in v0.10.0.
I tried to follow the syntax on how one might do this in R's ggplot (replacing 'dot' notation with underscores) as follows:
import pandas as pd
from plotnine import *
df = pd.DataFrame({
'month':('Jan','Feb','Mar','Apr','May','Jun','Jul','Aug','Sep','Oct','Nov','Dec'),
'temperature':(26.0,25.8,23.9,20.3,16.7,14.1,13.5,15.0,17.3,19.7,22.0,24.2),
})
df['month'] = pd.Categorical(df['month'], categories=('Jan','Feb','Mar','Apr','May','Jun','Jul','Aug','Sep','Oct','Nov','Dec'), ordered=True)
p = (ggplot(df, aes(x='month', y='temperature'))
+ theme_light()
+ geom_line(group=1)
+ scale_y_continuous(
name='Celsius',
sec_axis=sec_axis(trans=~.*1.8+32, name='Fahrenheit')
)
)
p
This didn't like the specification of the transformation, so I tried a few different options. Removing this altogether produces the error:
NameError: name 'sec_axis' is not defined
The documentation does not contain a reference for sec_axis, and searching for 'secondary axis' doesn't help either.
How do you implement a secondary axis in plotnine?
This github issue thread that was mentioned in the question does not say in any way that the secondary axis feature has been implemented. It was added to v0.10.0 milestones list before it was released. Here, milestones list means a todo list of what was planned to be implemented before the version releases. However, upon the actual release, the changelog does not mention the secondary axis feature, which means that it was only planned to be implemented and was not actually implemented. Long story short, the planned feature didn't make it into development and release.
So, I'm sorry to say that currently as of v0.10.0 and now v0.10.1 it seems that this feature isn't there yet in plotnine.

How to fetch shapely TopologyException point within contourf call

I'm trying to plot netCDF data using cartopy + matplotlib (contourf) call, however on some occasions the data generate invalid polygon instances through contourf and the following error is thrown (Sample), followed by other exceptions:
TopologyException: side location conflict at -83.68749999996696 36.937499999989356
When running the code using contour (not contourf), the plot works, and I can see the invalid geometry in the contours. I am able to address this issue in the contourf call by NAN-ing the value of the point at the location of the side-location conflict.
aLatName = "PRISM_Latitude"
aLonName = "PRISM_Longitude"
tLat = 36.937499999989356
tLon = -83.68749999996696
aLat = np.abs(array[aLatName] - tLat)
aLon = np.abs(array[aLonName] - tLon)
c = np.maximum(aLon, aLat)
([xloc], [yloc]) = np.where(c == np.min(c))
array["data"][yloc, xloc] = np.nan
I'm wondering if there is a way to capture the coordinates of the side location conflict in a try-except block so I can then try the nan approach to fix the issue.
5/30 Edit: I did some rabbit hole digging through the call stack to find out more information and it seems like the error message shown above is not a python exception being thrown but an exception being thrown in C++ and then passed along through the python interpreter, specifically the GEOS library (https://github.com/libgeos/geos/blob/70b1662e9b8f5118f9eef26529e9eeb9eb466544/src/geomgraph/EdgeEndStar.cpp#L312). From what I know about C++ / Python exceptions, I cannot directly "catch" this exception in python unless it exists as a python object already so it looks like this is a dead end, but perhaps this may provide some information to others who may be stuck with a similar problem.

Is statsmodel/exponential smoothing working correctly?

I am performing a time series analysis using statsmodels and the exponential smoothing method. I am trying to reproduce the results from
https://www.statsmodels.org/devel/examples/notebooks/generated/exponential_smoothing.html
with a particular dataframe (with the same format as the example, but only one outcome).
Here are the lines of code:
from statsmodels.tsa.api import ExponentialSmoothing, SimpleExpSmoothing, Holt
fit = ExponentialSmoothing(dataframe, seasonal_periods=4, trend='add', seasonal='mul', initialization_method="estimated").fit()
simulations = fit.simulate(5, repetitions=100, error='mul')
fit.fittedvalues.plot(ax=ax, style='--', color='green')
simulations.plot(ax=ax, style='-', alpha=0.05, color='grey', legend=False)
fit.forecast(8).rename('Holt-Winters (add-mul-seasonal)').plot(ax=ax, style='--', marker='o', color='green', legend=True)
However, when I run it, I get the error
TypeError: __init__() got an unexpected keyword argument 'initialization_method'
but when I check the parameters of ExponentialSmoothing in statsmodel, initialization_method is one of them, so I don't know what happens there.
Moving forward, I removed initialization_method from the parameters of ExponentialSmoothing within the code, then I get another error the line below
AttributeError: 'ExponentialSmoothing' object has no attribute 'simulate'
Again, I go and check if simulate is not deprecated in the latest version of statsmodels and no, it is still an attribute.
I upgraded the statsmodels, I upgraded pip and I still get the same errors.
What is it going on there?
Thanks in advance for any help!
Indeed, there was a bug in the previous version, that was corrected in the new version of statsmodels. One only needs to update to statsmodels 0.12.0 and this issue is solved.

Geofence with pymavlink or dronekit-python

I'm trying to set fence for the copter using dronekit-python.I found command MAV_CMD_NAV_FENCE_CIRCLE_INCLUSION in this document.
But it doesn't work when I use the vehicle.message_factory.command_long_send (which is the function command_long_send in class MAVLink from the file ardupilotmega.py actually ), I cannot find mavutil.mavlink.MAV_CMD_NAV_FENCE_CIRCLE_INCLUSION either (so I use integer 5003 directly).
After reading the source code of ardupilotmega.py, I found that there is a function called fence_point_send, so can anyone tell me how to use it? How can I set the geofence just like what Misson Planner do with python?
You should use this command MAV_CMD_DO_FENCE_ENABLE to enable or disable geo-fence.
geo-fence has two parameters.
FENCE_ALT_MAX: The maximum altitude that the vehicle can reach.
FENCE_RADIUS : The maximum circle radius the vehicle can move in.
To change the parameters in code, you should use a function like PARAM_SET and pass the name and value for the parameter.
P.S: You can do all that using GCS like Mission Planner, APM Planner2 or Mavproxy.

Categories

Resources