Text Properties
Text properties play a key role in making the generated graphs legible and understandable. Using these properties, we can customizing labels, titles, annotations, and legend text.
Types of text properties
Axis Labels: xlabel and ylabel parameters set descriptive labels for the x and y axes, improving axis interpretation.
sns.scatterplot(data=data, x="variable_x", y="variable_y")
plt.xlabel("X Axis Label", fontsize=12, fontweight='bold', halign='right', offset=(0, 10))
plt.ylabel("Y Axis Label", fontsize=12, fontweight='bold', valign='bottom', offset=(10, 0))
plt.show()
Plot Titles: title parameter adds a title to the plot, summarizing its content or purpose.
sns.scatterplot(data=data, x="variable_x", y="variable_y")
plt.title("Plot Title", fontsize=16, fontweight='bold', color='darkblue', halign='center')
plt.show()
Annotations: annotate function adds text annotations to specific data points, providing additional context.
plt.annotate("Important Point", xy=(x_value, y_value), xytext=(x_text, y_text), fontsize=10, ha='left', va='top')
Rotating Text: In some cases, rotating text labels can improve readability, especially for axis labels with long text.
sns.scatterplot(data=data, x="variable_x", y="variable_y")
plt.xticks(rotation=45)
plt.yticks(rotation=45)
plt.show()
Some additional parameters related to text properties are –
- halign: It determines the horizontal alignment of text elements, such as left, center, or right alignment.
- valign: It controls the vertical alignment of text elements, such as top, center, or bottom alignment.
- fontsize: It specifies the size of the font used for text elements.
- offset: It shifts the position of text elements by a specified amount in pixels.
Example
import matplotlib.pyplot as plt
import seaborn as sns
import pandas as pd
data = {'Year': [2010, 2011, 2012, 2013, 2014],
'Sales': [500, 600, 800, 1000, 1200]}
df = pd.DataFrame(data)
sns.lineplot(x='Year', y='Sales', data=df, color='green')
plt.title('GFG Sales Over Years', fontsize=18, weight='bold', color='blue')
plt.xlabel('Year', fontsize=14, style='italic', color='purple')
plt.ylabel('Sales ($)', fontsize=14, style='italic', color='orange')
plt.xticks(fontsize=12, color='red', rotation=45, ha='right')
plt.yticks(fontsize=12, color='brown', rotation=0)
plt.show()
Output
7. Some other properties
These are some other properties that can help to create a more elaborate and detailed plots.
Text: It refers to the label associated with a particular element in the plot, such as axis labels, titles, annotations, or legend labels.
plt.title("Plot Title")
Group: It refers to grouping data points or elements based on a categorical variable, allowing for visual comparison between different groups.
sns.scatterplot(data=data, x="variable_x", y="variable_y", hue="category")
Grid: The grid property refers to the grid lines or background grid displayed on the plot.
sns.scatterplot(data=data, x="variable_x", y="variable_y")
plt.grid(True)
Example
import seaborn as sns
import matplotlib.pyplot as plt
x_values = [1, 2, 3, 4, 5]
y_values_1 = [10, 15, 20, 25, 30]
y_values_2 = [5, 10, 15, 20, 25]
plt.plot(x_values, y_values_1, label='Group 1')
plt.plot(x_values, y_values_2, label='Group 2')
plt.xlabel('X Axis Label')
plt.ylabel('Y Axis Label')
plt.title('GFG Line')
plt.legend()
plt.grid(True)
plt.show()
Output
Properties of Mark objects in Python Seaborn
Seaborn is a famous Python library that is used for statistical data visualisation. This library is built on top of matplotlib. With seaborn it is easy to create informative and attractive statistical graphs. Working with Seaborn, it is important to understand the mark object properties. This article will look into different things that make up a mark in Seaborn such as coordinates, colour, style, size, text and other properties.
Contact Us