how to plot error bars in polar coordinates in python?

  • Last Update :
  • Techknowledgy :

On limitation of errorbar is that the caps are drawn with hline and vline collections so the caps to not properly rotate in polar coordinates (there is an issue open for this, https://github.com/matplotlib/matplotlib/issues/441 ). An approximate workaround is to just make the caps have zero size:

import numpy as np
import pylab as plt

fig = plt.figure()
ax = plt.axes(polar = True)

r = np.array([5.31, 5.29, 5.25, 5.19, 5.09, 4.92, 4.67, 4.27, 3.75, 3.56])
theta = 2 * np.pi / 360 * np.array(list(range(0, 100, 10)))

ax.plot(theta, r, "ro")
ax.errorbar(theta, r, yerr = 1, xerr = .1, capsize = 0)

plt.show()

If you want the theta error bars to be circular you will have to implement that your self. The easiest way is

th_err = 1

for th, _r in zip(theta, r):
   local_theta = np.linspace(-th_err, th_err, 15) + th
local_r = np.ones(15) * _r
ax.plot(local_theta, local_r, color = 'k', marker = '')

plt.show()

I would have recommended something like this:

import numpy as np
import pylab as plt

fig = plt.figure()
ax = plt.axes(polar = True)

r = 1e04 * np.array([5.31, 5.29, 5.25, 5.19, 5.09, 4.92, 4.67, 4.27, 3.75, 3.56])
theta = 2 * np.pi / 360 * np.array(list(range(0, 100, 10)))

ax.plot(theta, r, "ro")
ax.errorbar(theta, r, xerr = 0.5, yerr = 0.4)

plt.show()

Suggestion : 2

In this section, we will create a dates plot with error bars using Matplotlib. We use plt.errorbar() method to plot error bars.,In this section, we will create a chart plot with error bars using Matplotlib. We use plt.errorbar() method to plot error bars in bar charts.,In this section, we will create a scatter plot with error bars using Matplotlib. We use plt.errorbar() method to plot error bars in scatter plot.,We use plt.errorbar() method to plot error bars and become it more interactive.

The syntax to plot error bars is as below:

matplotlib.pyplot.errorbar(x, y, yerr = None, xerr = None, fmt = '', ecolor = None, elinewidth = None, capsize = None, barsabove = False, lolims = False, uplimes = False, xlolims = False, xuplims = False, errorevery = 1, capthick = None, *, data = None, ** kwargs)

Let’s understand the concept with the help of an example as below:

# Import Library

import matplotlib.pyplot as plt

# Define Data

x = [1, 2, 3, 5]
y = [9, 15, 20, 25]

# Plot error bar

plt.errorbar(x, y, xerr = 0.9)

# Display graph

plt.show()

Understand the concept with the help of an example:

# Import Library

import matplotlib.pyplot as plt

# Define Data

x = [1, 2, 3, 5]
y = [9, 15, 20, 25]

# Plot error bar

plt.errorbar(x, y, xerr = 0.9, fmt = 'o', color = 'orange',
   ecolor = 'lightgreen', elinewidth = 5, capsize = 10)

# Display graph

plt.show()

Let’s understand this concept with the help of an example:

# Import Library

import matplotlib.pyplot as plt

# Define Data

x = [6, 15, 2.3, 9]
y = [9, 15, 20, 25]

# Define Error

x_error = [2.3, 5.1, 1, 3.1]

# Plot Bar chart

plt.bar(x, y)

# Plot error bar

plt.errorbar(x, y, xerr = x_error, fmt = 'o', ecolor = 'red', color = 'yellow')

# Display graph

plt.show()

The syntax to plot error bars on y values is as given below:

matplotlib.pyplot.errorbar(x, y, yerr = None)

Suggestion : 3

3D voxel / volumetric plot with cylindrical coordinates , Creating boxes from error bars using PatchCollection , 3D voxel / volumetric plot with rgb colors , Using a ttf font file in Matplotlib

import numpy as np
import matplotlib.pyplot as plt

# Fixing random state
for reproducibility
np.random.seed(19680801)

# Compute pie slices
N = 20
theta = np.linspace(0.0, 2 * np.pi, N, endpoint = False)
radii = 10 * np.random.rand(N)
width = np.pi / 4 * np.random.rand(N)
colors = plt.cm.viridis(radii / 10.)

ax = plt.subplot(projection = 'polar')
ax.bar(theta, radii, width = width, bottom = 0.0, color = colors, alpha = 0.5)

plt.show()

Suggestion : 4

A polar chart represents data along radial and angular axes. With Plotly Express, it is possible to represent polar data as scatter markers with px.scatter_polar, and as lines with px.line_polar.,The radial and angular coordinates are given with the r and theta arguments of px.scatter_polar. In the example below the theta data are categorical, but numerical data are possible too and the most common case.,If Plotly Express does not provide a good starting point, you can use the more generic go.Scatterpolar class from plotly.graph_objects. All the options are documented in the reference page.,You can plot less than a whole circle with the range_theta argument, and also control the start_angle and direction:

import plotly.express as px
df = px.data.wind()
fig = px.scatter_polar(df, r = "frequency", theta = "direction")
fig.show()
import plotly.express as px
df = px.data.wind()
fig = px.scatter_polar(df, r = "frequency", theta = "direction",
   color = "strength", symbol = "strength", size = "frequency",
   color_discrete_sequence = px.colors.sequential.Plasma_r)
fig.show()
import plotly.express as px
df = px.data.wind()
fig = px.line_polar(df, r = "frequency", theta = "direction", color = "strength", line_close = True,
   color_discrete_sequence = px.colors.sequential.Plasma_r,
   template = "plotly_dark", )
fig.show()
import plotly.express as px
fig = px.scatter_polar(r = range(0, 90, 10), theta = range(0, 90, 10),
   range_theta = [0, 90], start_angle = 0, direction = "counterclockwise")
fig.show()
import plotly.graph_objects as go

fig = go.Figure(data =
   go.Scatterpolar(
      r = [0.5, 1, 2, 2.5, 3, 4],
      theta = [35, 70, 120, 155, 205, 240],
      mode = 'markers',
   ))

fig.update_layout(showlegend = False)
fig.show()
import plotly.graph_objects as go

import pandas as pd

df = pd.read_csv("https://raw.githubusercontent.com/plotly/datasets/master/polar_dataset.csv")

fig = go.Figure()
fig.add_trace(go.Scatterpolar(
   r = df['x1'],
   theta = df['y'],
   mode = 'lines',
   name = 'Figure 8',
   line_color = 'peru'
))
fig.add_trace(go.Scatterpolar(
   r = df['x2'],
   theta = df['y'],
   mode = 'lines',
   name = 'Cardioid',
   line_color = 'darkviolet'
))
fig.add_trace(go.Scatterpolar(
   r = df['x3'],
   theta = df['y'],
   mode = 'lines',
   name = 'Hypercardioid',
   line_color = 'deepskyblue'
))

fig.update_layout(
   title = 'Mic Patterns',
   showlegend = False
)

fig.show()