Margin in Pygal Plot
Margin is used to create space around any object. In this case, the margin is created around the plot using the attributes margin_top, margin_bottom, margin_right, and margin_left. A proper margin ensures that the plot is well-positioned within its container. Let’s see the examples.
Customize Top Margin in Pygal Plot
In Pygal, we can create the margin at the top of the plot using the attribute ‘margin_top’. In the below example, we have given the margin at the top of 100 pixels. Therefore, a space between the title and plot is created as seen in the output.
Python3
import pygal # Create a bar chart with custom Top margin bar_chart = pygal.Bar(margin_top = 100 ) # Set the title of bar chart bar_chart.title = "Weekly sales data" # Set the labels of x-axis bar_chart.x_labels = [ 'Monday' , 'Tuesday' , 'Wednesday' , 'Thrusday' , 'Friday' , 'Saturday' , 'Sunday' ] # Add data to bar chart bar_chart.add( 'Sales_Data' , [ 5 , 8 , 12 , 6 , 10 , 9 , 4 ]) # Render the bar chart to SVG file bar_chart.render_to_file( 'bar_chart.svg' ) |
Output:
Customize Bottom Margin in Pygal Plot
In Pygal, we can create the margin at the bottom of the plot using the attribute ‘margin_bottom’. In the below example, we have given the margin at the bottom of 100 pixels. Therefore, a space between the plot and the bottom of its parent container is created as seen in the output.
Python3
import pygal # Create a bar chart with custom Bottom margin bar_chart = pygal.Bar(margin_bottom = 100 ) # Set the title of bar chart bar_chart.title = "Weekly sales data" bar_chart.x_title = "Week days" # Set the labels of x-axis bar_chart.x_labels = [ 'Monday' , 'Tuesday' , 'Wednesday' , 'Thrusday' , 'Friday' , 'Saturday' , 'Sunday' ] # Add data to bar chart bar_chart.add( 'Sales_Data' , [ 5 , 8 , 12 , 6 , 10 , 9 , 4 ]) # Render the bar chart to SVG file bar_chart.render_to_file( 'bar_chart_margin.svg' ) |
Output:
Customize Right Margin in Pygal Plot
In Pygal, we can create the margin at the left of the plot using the attribute ‘margin_left‘. In the below example, we have given the margin at the left of 100 pixels. Therefore, a space between the legend label and the plot is created as seen in the output.
Python3
import pygal # Create a bar chart with custom Left margin bar_chart = pygal.Bar(margin_left = 200 ) # Set the title of bar chart bar_chart.title = "Weekly sales data" bar_chart.x_title = "Week days" # Set the labels of x-axis bar_chart.x_labels = [ 'Monday' , 'Tuesday' , 'Wednesday' , 'Thrusday' , 'Friday' , 'Saturday' , 'Sunday' ] # Add data to bar chart bar_chart.add( 'Sales_Data' , [ 5 , 8 , 12 , 6 , 10 , 9 , 4 ]) # Render the bar chart to SVG file bar_chart.render_to_file( 'bar_chart_margin.svg' ) |
Output:
Customize Right Margin in Pygal Plot
In Pygal, we can create the margin at the right of the plot using the attribute ‘margin_right’. In the below example, we have given the margin at the right of 400 pixels. Therefore, a space between the plot and the left border of its parent container is created which results in the squeezed of the bar plot as seen in the output.
Python3
import pygal # Create a bar chart with custom right margin bar_chart = pygal.Bar(margin_right = 400 ) # Set the title of bar chart bar_chart.title = "Weekly sales data" bar_chart.x_title = "Week days" # Set the labels of x-axis bar_chart.x_labels = [ 'Monday' , 'Tuesday' , 'Wednesday' , 'Thrusday' , 'Friday' , 'Saturday' , 'Sunday' ] # Add data to bar chart bar_chart.add( 'Sales_Data' , [ 5 , 8 , 12 , 6 , 10 , 9 , 4 ]) # Render the bar chart to SVG file bar_chart.render_to_file( 'bar_chart_margin.svg' ) |
Output:
Spacing and Margin in Pygal
Python is a popular scripting language used for data analysis due to its vast library collection using which we can analyze the data. One of them is the Pygal library which is specifically tailored to create interactive and visually appealing charts. Pygal provides an extensive range of chart types, customization options, and interactive features.