Reduce spacing between bars in seaborn hist plot [duplicate] - python

This question already has an answer here:
python matplot.hist - remove gaps between bars
(1 answer)
Closed 4 months ago.
I am plotting some data with
g = seaborn.displot(data, x=var, stat='probability')
# Set appropriate ticks
plt.xticks(np.arange(0, int(data[var].max() + 1), 1.0))
plt.show()
which is giving me the desired plot:
But I would like to compress the axis to remove the unnecessary space and I'm struggling to work out how.

You can increase the margins that determine the space added at both sides of the data limit to get the view limit. Default is 0.05.
plt.margins(x=0.5)
As pointed out by JohanC in the comment below: if your data are discrete values you may specify discrete=True or cast your data to strings to plot them as categories (in the latter case you don't need to set the ticks manually).
sns.displot(data[var].astype(str), stat='probability')

Related

Pandas & Matplot -> Independent marker out of scale [duplicate]

This question already has answers here:
pyplot scatter plot marker size
(6 answers)
Closed 3 years ago.
I have a scatter chart where i the X-axis is the latitude, the Y-axis is the longitude. Each dot represents a restaurant. The marker size should represent the gross income of that restaurant.
In some areas these values vary wildly, in order of about 100x times, so these guys (rich ones) completely "hide" small nearby restaurants...
So I thought of using a log scale on the marker size... here is the code:
groups.plot.scatter(x='lon', y='lat', s=groups.weight.apply(lambda x: math.log(x)))
plt.plot(sLon, sLat, marker='o', color='red', markersize=math.log(aux.__len__()))
The thing is: i know for a fact that aux.__len__() equals several of the weights on the groups. Here is a image:
The red dot should be very close in size to the ones on its right...
so my question is: Why is the plot from the second command not scaled as the rest?
Its different because you are using scatter and plot which use different sizes. The markersize of plot scales linearly and is more sensitive than the s for a scatter which scales with the sqrt.
See this link for a similar discussion:

How to reduce the number of values on x-axis in a matplotlib graph [duplicate]

This question already has an answer here:
pyplot, why isn't the x-axis showing?
(1 answer)
Closed 3 years ago.
I am trying to plot a graph using matplotlib library.
This is my code:
df = pd.DataFrame()
df = milo_data2.loc[milo_data2['id'] == device]
plt.figure()
plt.title(device)
plt.ylabel('Counter')
plt.plot(df['timestamp'],df['counter'])
The graph looks like
The values on the x-axis are crowded and not readable.(The bold black line is the group of values overlapping each other) How do I reduce the number of values on the x-axis so that I can see some values on x-axis to get an estimate.
You can manually set the ticks to display. For instance, you can leave every tenth tick:
ticks = list(df['timestamp'])
plt.xticks([ticks[i] for i in range(len(ticks)) if i % 10 == 0], rotation='vertical')
For more information see documentation

Matplotlib adding too many labels to bar chart [duplicate]

This question already has answers here:
reducing number of plot ticks
(10 answers)
Pandas: bar plot xtick frequency
(1 answer)
Closed 4 years ago.
I'm having a slightly frustrating situation with pandas/matplotlib (not sure which one's at fault here). If I create a simple random series and plot it as a line chart, the formatting is pretty clean:
test1 = pd.Series(index = pd.date_range(start='2000-06-30', end='2018-06-30', freq='3M'),
data = np.random.rand(73))
test1.plot(kind='line')
Most satisfactory.
If I change the format to bar, though, the x-axis formatting goes to hell in a handbasket:
Matplotlib seems to feel obliged to label every single bar. Is there a logical reason for this or an easy fix?
Thanks in advance.
Matplotlib is trying to use each time as its own bar, and is trying to label each bar appropriately. To change this behaviour, you should change the xticks or the xticklabels. For example, one quick way to just remove the labels entirely is to do something like
subplot = test1.plot(kind='bar')
ax = subplot.axes
#ax.set_xticks([]) # Alternatively, you can manually adjust the ticks
ax.set_xticklabels([]) # or their labels
f = ax.get_figure()
f.show()
which will produce
You can reduce the number of ticks to something like using every nth tick and its label in the same way.

How to remove top value of y-axis only from Python's Matplotlib figure? [duplicate]

This question already has answers here:
Changing the tick frequency on the x or y axis
(13 answers)
Closed 7 years ago.
I am trying to plot some data using Matplotlib. My code works fine but there is a clash between my figure title and the top value of the y-axis ticks. So my question is, would anybody know how to remove the top value only from the y-axis whilst keeping everything else about the plot the same?
My code
cyp_minus=pandas.read_csv(cyp_minus_file,sep='\t')
cyp_plus=pandas.read_csv(cyp_plus_file,sep='\t')
plt.figure(9)
plt.plot(cyp_minus['Dose']*1000,cyp_minus['atRA_minus_tet'],label='Control')
plt.plot(cyp_plus['Dose']*1000,cyp_plus['Cyp26(atRA_plus_tet)'],label='RARa Overexpression')
x=plt.xlabel('[atRA] (nM)',fontsize=15)
plt.ylabel('[Cyp26A1]/beta-actin',fontsize=15)
plt.title('Effect of RARa overexpression on Cyp26A1 dose-response curve',fontsize=15)
plt.legend(loc=2,fontsize=15)
plt.rc('xtick', labelsize=20)
plt.rc('ytick', labelsize=20)
plt.savefig('Cyp26A1 DR-curve for presentation.tiff',dpi=500,bbox_extra_artists=[x], bbox_inches='tight')
Thanks
You can make your title go up so it doesn't overlap with the y top value:
plt.title('Effect of RARa overexpression on Cyp26A1 dose-response curve',fontsize=15, y=1.5) # Change y value accordinly
As an alternative to the accepted answer, if you really do want to remove the text from a tick label, you can do so like this:
yticks = plt.gca().get_yticks().tolist() # get list of ticks
yticks[-1] = '' # set last tick to empty string
ax.set_yticklabels(yticks) # set the labels

Creating ticks in a matplotlib heatmap based on range of values [duplicate]

This question already has answers here:
How to relabel axis ticks for a matplotlib heatmap
(3 answers)
Closed 1 year ago.
I am creating a heatmap in matplotlib where on the x and y axis is some parameter of a measurement and the color represents the value of the measurement. Matplotlib automatically gives the axes ticks based on the index of the value. For example if on the x axis I am measuring at 50 different values the ticks will be from 0 to 50. However the real value of this parameter is for example from -30 to 80 and I would like matplotlib to create the ticks based on this minimum and maximum.
I have tried using set_xticks but this requires the positions of the ticks as well as their labels. I am thinking that I should be able to just give matplotlib a min of -34 and max of 67 and have it create nice looking ticks placed at the proper positions but I haven't been able to find how.
After some digging in examples on the matplotlib website I found this option in imshow called extent in which you can replace the default zero-based coordinates with your own values for the min and max of both axes.
Wouldn't pyplot.xlim() or pyplot.figure.set_xlim() work in this case? Just say something like:
import matplotlib.pyplot as plt
plt.xlim(-30,80)
plt.ylim(0,100) #Or whatever
As far as I know the set_xticks function is too sophisticated for this. With that one you can specify what to put as your tick labels etc. For example if you want to associate a numerical series with a series of letters. For example:
x = [-8,-6,-4,-2,0,2,4,6,8]
labels = ['K2','K4','K6','K8','M0','M2','M4','M6','M8']
plt.xticks(x, labels)
Is one I used personally to translate integers into stellar spectral types (which is relevant, since I'm an astronomer ;p).
Hope this helps.
`

Categories

Resources