This question already has answers here:
matplotlib xticks labels overlap
(1 answer)
How to prevent x-axis labels from overlapping
(4 answers)
Closed 1 year ago.
I have 115 columns, I want to plot the column names on the x-axis. But the column names are overlapping with each other. The y-axis has single row values.
features = fdf.iloc[0] # single row
features.sort_values(ascending=False).plot(kind='bar')
plt.xticks(rotation=90)
plt.show()
This is the graph, as you can see that the x-axis labels are not clear.
You have few options :
make the figure wider using figsize argument in plot
features.sort_values(ascending=False).plot(kind='bar', figsize = (15, 7))
Reduce the size of xticks label on x axis using fontsize argument in `xticks'
plt.xticks(rotation=90, fontsize = 'xx-small')
read about more options for fontsize here
Related
This question already has answers here:
Trying to add color gradients to Matplotlib chart
(1 answer)
Python: Barplot colored according to a third variable
(1 answer)
Changing color scale in seaborn bar plot
(5 answers)
Closed 2 months ago.
I have monthly rain data from a list, where values are on the Y-axis and months on the X-axis.
This is my color palette:
sns.color_palette("crest", as_cmap=True)
This is my code to barplot the data:
plt.figure(figsize=(8,4), tight_layout=True)
colors = sns.color_palette("crest")
plt.bar(bocas_rain['Date'], bocas_rain['Pr'], color=colors)
plt.xlabel('Months')
plt.ylabel('Rain in mm')
plt.title('Rain in Bocas')
plt.show()
The result I am getting is this:
How can I make the highest values of my data match the dark blue colors from the palette?
This question already has answers here:
How can I remove the top and right axis in matplotlib?
(10 answers)
Closed 7 months ago.
enter image description here
the values of bars overlapping the borders. How to change the border so that bars with values should fit in properly
#adding following lines solved the problem
fig = plt.figure(figsize=(6, 6))
ax = fig.add_subplot(111)
ax.spines['right'].set_visible(False)
ax.spines['top'].set_visible(False)
This question already has answers here:
Improve subplot size/spacing with many subplots
(8 answers)
Closed 1 year ago.
Hi I'm very new to Python, and I'm trying to fix the labels because they overlap, (as seen in the picture). I figured the hspace and the wspace is the columns, but I'm not sure exactly how to adjust everything else in the labels, I don't want to mess with the x axis. Is there a way to make this plot look clearer?
Here's what I have:
_, axes = plt.subplots(nrows=6, ncols=6, sharex=True)
plt.suptitle('mean activity duration by time of day')
plt.subplots_adjust(hspace=0.5, wspace=0.5)
for ax, (activity, df) in zip(axes.ravel(), df_all.groupby('e')):
(df.groupby('f')
.d
.mean()
.plot(ax=ax,
kind='bar',
title=activity,
xlabel='time'))
6 x 6 bar graph:
Use constrained_layout.
use a larger figure size so your titles are not larger than your axes
use a smaller font size for the titles.
You can use tight_layout if you prefer, but constrained_layout is more flexible.
You can try to use plt.tight_layout, adjusts subplot params so that the subplot(s) fits in to the figure area
This question already has answers here:
secondary_y=True changes x axis in pandas
(2 answers)
Plot multiple Y axes
(3 answers)
Closed 4 years ago.
I want to add secondary y-axis. I have my data in CSV with three column date, lscc and cc. I want to add LSCC as first y-axis and CC as secondry. so far I have done this
df=pd.read_csv("E29Lsccvalue.csv", index_col='Date' )
plt.ylabel("")
plt.xlabel("Low level Similarity Class Cohesion (LSCC) Evolution")
df.plot(kind="line", marker='o',legend=None)
plt.xticks(rotation=90)
plt.show()
thanks
Within matplotlib I have used twinx() when I want to utilize the existing X-axis I have created, yet plot more data on top with a different Y axis. In your case with df as the first plot object:
axCC = df.twinx() # second axis sharing the same X axis of the original
Then you can include plots, labels, and other parameters referenced to this axis through calls such as:
axCC.set_ylabel("ExampleLabel",color="tab:red")
axCC.plot(xData,yData,color="blue")
Etc, etc.
A fully functional example with more detail is shown here
Although no reproducible date is provided, I guess you can achieve the desired result by doing this:
ax = df.plot(secondary_y='CC')
eventually adding all your ax customization required
edit: dotted line customization
Suppose you need a dotted vertical line at a certain position on your x-axis (in this example, at position 2 from your pandas index), use axvline and ':' as linestyle (dots)
ax = a.plot(secondary_y='Price')
ax.axvline(a.index.values[2], linestyle=':')
This question already has answers here:
Creating figure with exact size and no padding (and legend outside the axes)
(2 answers)
How to put the legend outside the plot
(18 answers)
Closed 4 years ago.
I am trying to put a legend below a graph but keeping the figure size fixed.
Is this possible?
I saw How to put the legend out of matplotlib plot and https://stackoverflow.com/a/4701285/7746941 but the first one does not address fitting the legend within a predefined figure size while the second one does not do this generically (there is an example where the axes width is shrunk by 0.8 to accommodate the legend) .
Below is my current solution that anchors the legend at the bottom of the graph but the legend does not fit the figure.
I cannot figure out how to determine the height of the legend box to move the axis up by that amount.
import pandas as pd
df = pd.DataFrame({'a': [1,2,3], 'b': [4,5,6]})
ax = df.plot(figsize=(4,4))
tight_box = ax.transAxes.inverted().transform(ax.get_tightbbox(ax.figure.canvas.get_renderer()))
leg = ax.legend(bbox_to_anchor=(0,tight_box[0][1],1,0), loc='upper center')