The gridlines appear to be above the bars because of the parameter zorder used when plotting the gridlines and bars. In the code, the gridlines have a zorder value of 0, while the bars have a zorder value of 1. A higher zorder value means the object will be placed above objects with lower zorder values.
To fix this, you can change the zorder value of the gridlines to be higher than that of the bars. For example, you can modify the gridlines code as follows:
By setting zorder=2, the gridlines will be placed above the bars.
User
great! now how to change the color of background? the entire background, not just under bars
Assistant
To change the color of the entire background, you can set the facecolor attribute of the figure. Here's how you can modify your code to change the background color:
python
User
how to add subtitle right under the title?
Assistant
To add a subtitle right under the title, you can create a separate text element and position it accordingly. Here's how you can modify your code to add a subtitle:
python
User
there s no way to attach it to the title so i wouldnt have to manually searching for right coordinates?
Assistant
Apologies for the confusion. To attach the subtitle directly below the title without manually searching for coordinates, you can adjust the y position of the subtitle using the set_y method of the title. Here's the modified code:
python
import pandas as pd
from matplotlib import pyplot as plt
name = ['Всех акций\n MOEX', 'Топ 50\nпо капитализации', 'Топ 10\n по капитализации','Доллара']
price = [200, 140, 80, 30]
bars_color = ['pink','pink','pink','pink']
# Figure Size
fig, ax = plt.subplots(figsize=(16, 9))
# Set background color
fig.patch.set_facecolor('#f2f2f2') # Change the hex code to the desired background color
# Add x, y gridlines
ax.grid(visible=True, color='grey',
linestyle='-.', linewidth=0.5,
alpha=0.2, zorder=2)
# Horizontal Bar Plot
ax.barh(name, price, color=bars_color, alpha=1.0, zorder=1)
# Remove axes splines
for s in ['top', 'bottom', 'left', 'right']:
ax.spines[s].set_visible(False)
# Remove x, y Ticks
ax.xaxis.set_ticks_position('none')
ax.yaxis.set_ticks_position('none')
# Add padding between axes and labels
ax.xaxis.set_tick_params(pad=2)
ax.yaxis.set_tick_params(pad=5)
# Show top values
ax.invert_yaxis()
# Add annotation to bars
for i in ax.patches:
plt.text(i.get_width() + 2, i.get_y() + 0.5,
'+'+ str(round(i.get_width(), 2)) + '%',
fontsize=30, color='green')
# Add Plot Title
ax.set_title('Return YTD',
loc='left', fontweight='bold', fontsize=66, color=(0.37,0.15, 0.19))
# Set size of the country names
plt.tick_params(axis='y', labelsize=15)
plt.subplots_adjust(left=0.168, bottom=0.387, right=0.789, top=0.789, wspace=0.364, hspace=None)
# Show Plot
plt.show()
In the fig.patch.set_facecolor('#f2f2f2') line, replace #f2f2f2 with the hex code of the desired background color.
import pandas as pd
from matplotlib import pyplot as plt
name = ['Всех акций\n MOEX', 'Топ 50\nпо капитализации', 'Топ 10\n по капитализации','Доллара']
price = [200, 140, 80, 30]
bars_color = ['pink','pink','pink','pink']
# Figure Size
fig, ax = plt.subplots(figsize=(16, 9))
# Set background color
fig.patch.set_facecolor('#f2f2f2') # Change the hex code to the desired background color
# Add x, y gridlines
ax.grid(visible=True, color='grey',
linestyle='-.', linewidth=0.5,
alpha=0.2, zorder=2)
# Horizontal Bar Plot
ax.barh(name, price, color=bars_color, alpha=1.0, zorder=1)
# Remove axes splines
for s in ['top', 'bottom', 'left', 'right']:
ax.spines[s].set_visible(False)
# Remove x, y Ticks
ax.xaxis.set_ticks_position('none')
ax.yaxis.set_ticks_position('none')
# Add padding between axes and labels
ax.xaxis.set_tick_params(pad=2)
ax.yaxis.set_tick_params(pad=5)
# Show top values
ax.invert_yaxis()
# Add annotation to bars
for i in ax.patches:
plt.text(i.get_width() + 2, i.get_y() + 0.5,
'+'+ str(round(i.get_width(), 2)) + '%',
fontsize=30, color='green')
# Add Plot Title
ax.set_title('Return YTD',
loc='left', fontweight='bold', fontsize=66, color=(0.37,0.15, 0.19))
# Add Subtitle
ax.text(0.5, 0.9, 'Subtitle', transform=ax.transAxes,
fontsize=30, ha='center', color='gray')
# Set size of the country names
plt.tick_params(axis='y', labelsize=15)
plt.subplots_adjust(left=0.168, bottom=0.387, right=0.789, top=0.789, wspace=0.364, hspace=None)
# Show Plot
plt.show()
In the ax.text() function, you can modify the 0.5, 0.9 coordinates to adjust the position of the subtitle. The transform=ax.transAxes parameter ensures that the coordinates are relative to the axes. Adjust the fontsize, ha (horizontal alignment), and color parameters according to your preferences.
import pandas as pd
from matplotlib import pyplot as plt
name = ['Всех акций\n MOEX', 'Топ 50\nпо капитализации', 'Топ 10\n по капитализации','Доллара']
price = [200, 140, 80, 30]
bars_color = ['pink','pink','pink','pink']
# Figure Size
fig, ax = plt.subplots(figsize=(16, 9))
# Set background color
fig.patch.set_facecolor('#f2f2f2') # Change the hex code to the desired background color
# Add x, y gridlines
ax.grid(visible=True, color='grey',
linestyle='-.', linewidth=0.5,
alpha=0.2, zorder=2)
# Horizontal Bar Plot
ax.barh(name, price, color=bars_color, alpha=1.0, zorder=1)
# Remove axes splines
for s in ['top', 'bottom', 'left', 'right']:
ax.spines[s].set_visible(False)
# Remove x, y Ticks
ax.xaxis.set_ticks_position('none')
ax.yaxis.set_ticks_position('none')
# Add padding between axes and labels
ax.xaxis.set_tick_params(pad=2)
ax.yaxis.set_tick_params(pad=5)
# Show top values
ax.invert_yaxis()
# Add annotation to bars
for i in ax.patches:
plt.text(i.get_width() + 2, i.get_y() + 0.5,
'+'+ str(round(i.get_width(), 2)) + '%',
fontsize=30, color='green')
# Add Plot Title
title = ax.set_title('Return YTD',
loc='left', fontweight='bold', fontsize=66, color=(0.37,0.15, 0.19))
# Adjust Y position of the title
title.set_y(1.05)
# Add Subtitle
ax.text(0.5, title.get_position()[1] - 0.05, 'Subtitle', transform=ax.transAxes,
fontsize=30, ha='center', color='gray')
# Set size of the country names
plt.tick_params(axis='y', labelsize=15)
plt.subplots_adjust(left=0.168, bottom=0.387, right=0.789, top=0.789, wspace=0.364, hspace=None)
# Show Plot
plt.show()
By setting title.set_y(1.05), the title is positioned at 5% above the top of the axes, and then we can use title.get_position() to retrieve the position of the title and adjust the y position of the subtitle accordingly by subtracting a fixed value.