Matplotlib adding too many labels to bar chart [duplicate] - python

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.

Related

Reduce spacing between bars in seaborn hist plot [duplicate]

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')

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

pandas color scheme not working properly with my data (python) [duplicate]

This question already has answers here:
Pandas DataFrame Bar Plot - Plot Bars Different Colors From Specific Colormap
(3 answers)
Closed 4 years ago.
I would like to change the default color scheme of my pandas plot. I tried with different color schemes through cmap pandas parameter, but when I change it, all bars of my barplot get the same color.
The code I tried is the following one:
yearlySalesGenre = df1.groupby('Genre').Global_Sales.sum().sort_values()
fig = plt.figure()
ax2 = plt.subplot()
yearlySalesGenre.plot(kind='bar',ax=ax2, sort_columns=True, cmap='tab20')
plt.show(fig)
And the data that I plot (yearlySalesGenre) is a pandas Series type:
Genre
Strategy 174.50
Adventure 237.69
Puzzle 243.02
Simulation 390.42
Fighting 447.48
Racing 728.90
Misc 803.18
Platform 828.08
Role-Playing 934.40
Shooter 1052.94
Sports 1249.47
Action 1745.27
Using tab20 cmap I get the following plot:
I get all bars with the first color of all the tab20 scheme. What I am doing wrong?
Note that if I use the default color scheme of pandas plot, it properly displays all bars with different colors, but the thing is that I want to use a particular color scheme.
As posted, it's a duplicated answer. Just in case, the answer is that pandas makes color schemes based on different columns, not in rows. So to use different colors you can transpose the data + some other stuff (duplicated link), or directly use the matplotlib.pyplot plotting that allows more flexibility (in my case):
plt.bar(range(len(df)), df, color=plt.cm.tab20(np.arange(len(df))))
Maybe this is what you want:
df2.T.plot( kind='bar', sort_columns=True, cmap='tab20')
I think the problem you have is that you only have one series. Pandas plot bar will plot separate series (columns) each with its own color, and separate each each bar based on the index.
By using .T, the series in your data become multiple columns but within only one index. I am sure you can play with the legend to get a better display.

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