Increasing the space between the bars in histplot [duplicate] - matplotlib

This question already has an answer here:
How to make Seaborn histogram have skinny bars / bins
(1 answer)
Closed 2 months ago.
I have plotted a simple histogram using Seaborn but the bars are stuck together and I would like to increase the space between them.
import seaborn as sns
sns.histplot([-2.0, -1.0, -2.0, 2.0, 3.0, 0.0, 4.0, -2.0, -2.0, -3.0, -2.0, 4.0])
I tried a couple of suggestions such as adding rwidth argument or using rwidth within hist_kws argument but neither worked for histplot.

IIUC you want to get some spaces between your bins, without actually changing their sizes. I would play with style and linewidth in this case:
sns.set_style("white")
sns.histplot([-2.0, -1.0, -2.0, 2.0, 3.0, 0.0, 4.0, -2.0, -2.0, -3.0, -2.0, 4.0], linewidth=5.0)
Output:

Related

How to generate a mesh file with extracting nodes and elements

I need to generate a mesh file, where I need to extract the following information :
X Y and Z coordinates of each node + the nodetags
list of all the elements + elementtags
I would like to give each edge(the elements and the nodes of the edges) of my domain an index, in order to use it in my code for the management of BC, IC and parameters...)
Is there any preexisting code that would help me to do that ?
I tried gmsh, but I can't really understand the syntax of the .msh file, which is different from the explanation they propose in : 9.1 MSH file format
I've created meshio for this purpose. Here's how to write a file:
points = numpy.array([
[0.0, 0.0, 0.0],
[0.0, 1.0, 0.0],
[0.0, 0.0, 1.0],
])
cells = {
"triangle": numpy.array([
[0, 1, 2]
])
}
meshio.write_points_cells(
"foo.vtk",
points,
cells,
# Optionally provide extra data on points, cells, etc.
# point_data=point_data,
# cell_data=cell_data,
# field_data=field_data
)
Many different formats are supported.

How to plot irregularly sampled time data in animated graph, using Matplotlib or other?

I have data that is logged in irregular time steps, and I want to show it as a scrolling animation over time. For example, data A may have time points [0.001, 0.004, 0.007, 0.009, ..., 0.97], and data B may have roughly the same, plus or minus 0.02 at each point.
I want to create a scrolling animation of the data being updated over time, but only have it update a line's points/vertices after that vertex's time has passed. I cant think of a good way to have numpy say "for this line, only count data that is up this timestamp". I think if I can get that I can figure something out from the matplotlib examples, but a full solution would be nice as well.
Thank you!
I think one solution would be to map the data into a consistent format:
A = np.array([1, 1.5, 4.5, 5])
B = np.array([1, 2.5, 3.5, 5])
scroll = np.linspace(1,5,11)
A_idx = np.searchsorted(A, scroll)
B_idx = np.searchsorted(B, scroll)
>>> scroll
array([1. , 1.4, 1.8, 2.2, 2.6, 3. , 3.4, 3.8, 4.2, 4.6, 5. ])
>>> A[A_idx]
array([1. , 1.5, 4.5, 4.5, 4.5, 4.5, 4.5, 4.5, 4.5, 5. , 5. ])
>>> B[B_idx]
array([1. , 2.5, 2.5, 2.5, 3.5, 3.5, 3.5, 5. , 5. , 5. , 5. ])
You might need to be careful with wether you use forward looking or backward looking values (here it is forward looking), but it will update when new data is available.

Matplotlib set_yticklabels shifting

Given the following code:
import matplotlib.pyplot as plt
import numpy as np
x = [1.0, 1.1, 2.0, 5.7]
y = np.arange(len(x))
fsize=(2,2)
fig, ax = plt.subplots(1,1,figsize=fsize)
ax.set_yticklabels(['a','b','c','d'])
ax.barh(y,x,align='center',color='grey')
plt.show()
Why are the labels not showing as expected ('a' does not show up and everything is shifted down by 1 place)?
The locator is generating an extra tick on each side (which are not being shown because they is outside the plotted data). Try the following:
>>> ax.get_yticks()
array([-1., 0., 1., 2., 3., 4.])
You have a couple of options. You can either hard-code your tick labels to include the extra ticks (which I think is a bad idea):
ax.set_yticklabels(list(' abcd')) # You don't really need 'e'
Or, you can set the ticks to where you want them to be along with the labels:
ax.set_yticks(y)
ax.set_yticklabels(list('abcd'))
A more formal solution to the tick problem would be to set a Locator object on the y-axis. The tick label problem is formally solved by setting the Formatter for the y-axis. That is essentially what is happening under the hood when you call set_yticks and set_yticklabels anyway, but this way you have full control:
from matplotlib.ticker import FixedLocator, FixedFormatter
...
ax.yaxis.set_major_locator(FixedLocator(y))
ax.yaxis.set_major_formatter(FixedFormatter(list('abcd')))
No, none of that works for me on Windows, Python 3.7.
Just print it as text with the x-axis location slightly to the left of the smallest x value.
import matplotlib.pyplot as plt
import numpy as np
ax.set_yticklabels("")
x = [1.0, 1.1, 2.0, 5.7]
y = np.arange(len(x))
fsize=(2,2)
fig, ax = plt.subplots(1,1,figsize=fsize)
names = ['a','b','c','d']
for i in np.arange(len(x)):
plt.text(-0.05,len(x)-i-1,names[i])
plt.show()

pyplot x-axis tick mark spacing is not centered with all columns

I'm struggling with what I hope is a misspecification of the pyplot histogram function. As you see in the image, the x-axis tick marks are not centered consistently on the columns as per the align='mid' parameter. If necessary, I will upload the data file to Dropbox.
Thanks for you help !
import matplotlib.pyplot as plt
import numpy as np
from matplotlib.ticker import FormatStrFormatter
data = DRA_size_males_s
fig, ax = plt.subplots(nrows=1, ncols=1)
ax.hist(data, facecolor='blue', edgecolor='gray', bins=25, rwidth=1.10, align='mid')
bins=[1.4,1.5,1.6,1.7,1.9,2.0,2.1,2.2,2.3,2.4,2.5,2.6,2.7,2.8,2.9,3.1,3.2,3.5,3.6,3.8]
ax.set_xticks(bins)
ax.set_ylabel('Frequency')
ax.set_xlabel('DRA Sizes(mm)')
ax.set_title('Frequencies of DRA Sizes in Males (mm)')
plt.show()
Here is the data array used to create the histogram:
1.4, 1.4, 1.4, 1.5, 1.5, 1.6, 1.7, 1.7, 1.7, 1.9, 1.9, 1.9, 1.9, 2.0, 2.0, 2.0, 2.1, 2.1, 2.1, 2.1, 2.2, 2.2, 2.3, 2.3, 2.3, 2.4, 2.5, 2.6, 2.7, 2.7, 2.8, 2.8, 2.8, 2.9, 2.9, 3.1, 3.1, 3.2, 3.2, 3.5, 3.6, 3.8
The plt.hist's align="mid" argument centers the bars of the histogram in the middle between the bin edges - this is in fact the usual way of plotting a histogram.
In order for the histogram to use predefined bin edges you need to supply those bin edges to the plt.hist function.
import matplotlib.pyplot as plt
import numpy as np
data = [1.4, 1.4, 1.4, 1.5, 1.5, 1.6, 1.7, 1.7, 1.7, 1.9, 1.9, 1.9, 1.9, 2.0,
2.0, 2.0, 2.1, 2.1, 2.1, 2.1, 2.2, 2.2, 2.3, 2.3, 2.3, 2.4, 2.5, 2.6,
2.7, 2.7, 2.8, 2.8, 2.8, 2.9, 2.9, 3.1, 3.1, 3.2, 3.2, 3.5, 3.6, 3.8]
fig, ax = plt.subplots(nrows=1, ncols=1)
bins=[1.4,1.5,1.6,1.7,1.9,2.0,2.1,2.2,2.3,2.4,2.5,2.6,2.7,2.8,2.9,3.1,3.2,3.5,3.6,3.8]
ax.hist(data, bins=bins, facecolor='blue', edgecolor='gray', rwidth=1, align='mid')
ax.set_xticks(bins)
ax.set_ylabel('Frequency')
ax.set_xlabel('DRA Sizes(mm)')
ax.set_title('Frequencies of DRA Sizes in Males (mm)')
plt.show()
Try to use bins with a range of values minus a small offset as in the following example.
In [100]: x = np.array([1, 2, 3, 4, 0, 3, 1, 7, 4, 5, 8, 8, 9, 7, 7, 3])
In [101]: len(x)
Out[101]: 16
In [102]: bins = np.arange(10) - 0.5
In [103]: plt.hist(x, facecolor='blue', edgecolor='gray', bins=bins, rwidth=2, alpha=0.75)
Now, the bin numbers will be center aligned.

Prevent matplotlib from showing scale notifier when axis limit is high [duplicate]

When I try to do a plot against a range with big enough numbers I get an axis with relative shift for all the ticks. For example:
plot([1000, 1001, 1002], [1, 2, 3])
I get these ticks on axis of abscissas:
0.0 0.5 1.0 1.5 2.0
+1e3
The question is how to remove +1e3 and get just:
1000.0 1000.5 1001.0 1001.5 1002.0
plot([1000, 1001, 1002], [1, 2, 3])
gca().get_xaxis().get_major_formatter().set_useOffset(False)
draw()
This grabs the current axes, gets the x-axis axis object and then the major formatter object and sets useOffset to false (doc).
In newer versions (1.4+) of matplotlib the default behavior can be changed via the axes.formatter.useoffset rcparam.
To disable relative shift everywhere, set the rc parameter:
import matplotlib
matplotlib.rc('axes.formatter', useoffset=False)