pyplot bar charts with individual data points

2024/11/16 0:26:39

I have data from a control and treatment group. Is matplotlib able to create a bar chart where the bar height is the mean of each group overlaid with the individual data points from that group? I'd like to visualize the spread of the actual data points, similar to what is displayed here.

I've thought about using a combination of boxplots and scatter, but my attempts have not succeeded.

Answer

Here is a solution doing exactly what you mention: overlay a bar graph with a scatter plot.

Of course you can further play around to tweak the plot: plot title, axis labels, colors, width, marker shape of the scatter plot ...

import matplotlib.pyplot as plt
np.random.seed(123)w = 0.8    # bar width
x = [1, 2] # x-coordinates of your bars
colors = [(0, 0, 1, 1), (1, 0, 0, 1)]    # corresponding colors
y = [np.random.random(30) * 2 + 5,       # data seriesnp.random.random(10) * 3 + 8]fig, ax = plt.subplots()
ax.bar(x,height=[np.mean(yi) for yi in y],yerr=[np.std(yi) for yi in y],    # error barscapsize=12, # error bar cap width in pointswidth=w,    # bar widthtick_label=["control", "test"],color=(0,0,0,0),  # face color transparentedgecolor=colors,#ecolor=colors,    # error bar colors; setting this raises an error for whatever reason.)for i in range(len(x)):# distribute scatter randomly across whole width of barax.scatter(x[i] + np.random.random(y[i].size) * w - w / 2, y[i], color=colors[i])plt.show()

It will yield this graph

scatter-bar graph

https://en.xdnf.cn/q/71731.html

Related Q&A

python only works with sudo

My python 2.7 script works on my Ubuntu system if I call it using sudo python [filename].pyor from a bash script using sudo ./[bashscriptname].shBut if I call it from Pycharm I get oauth errors, and fr…

Forbidden (CSRF token missing or incorrect) Django error

I am very new to Django. The name of my project is rango and I have created a URL named /rango/tagger that is supposed to send an object. In my java-script, I have tried to communicate with this route …

How to update artists in scrollable, matplotlib and multiplot

Im trying to create a scrollable multiplot based on the answer to this question: Creating a scrollable multiplot with pythons pylabLines created using ax.plot() are updating correctly, however Im unabl…

Sending Keys Using Splinter

I want to test an autocomplete box using Splinter. I need to send the down and enter keys through to the browser but Im having trouble doing this. I am currently finding an input box and typing tes int…

Split lists within dataframe column into multiple columns [duplicate]

This question already has answers here:Split a Pandas column of lists into multiple columns(13 answers)Closed 3 years ago.I have a Pandas DataFrame column with multiple lists within a list. Something l…

Drawing with turtle(python) using PyCharm

Im running the latest PyCharm Pro version and trying to run the below code from a scratch file but it doesnt seem to work import turtlewn = turtle.Screen() alex = turtle.Turtle() alex.forward(150) a…

How to adapt my current splash screen to allow other pieces of my code to run in the background?

Currently I have a splash screen in place. However, it does not work as a real splash screen - as it halts the execution of the rest of the code (instead of allowing them to run in the background).This…

Reversed array slice including the first element [duplicate]

This question already has answers here:Python reverse-stride slicing(8 answers)Closed 5 years ago.Lets say I have:>>> a = [1, 2, 3, 4]And I want to get a reversed slice. Lets say I want the 1s…

Problem using py2app with the lxml package

I am trying to use py2app to generate a standalone application from some Python scripts. The Python uses the lxml package, and Ive found that I have to specify this explicitly in the setup.py file that…

How to run TensorFlow on AMD/ATI GPU?

After reading this tutorial https://www.tensorflow.org/guide/using_gpu I checked GPU session on this simple code import numpy as np import matplotlib.pyplot as plt import tensorflow as tfa = tf.constan…