python script for pie chart
- User
python script for pie chart
- Assistant
Sure! Here's an example of a Python script that creates a pie chart using the matplotlib library:
import matplotlib.pyplot as plt # Data for the pie chart labels = ['Apple', 'Banana', 'Orange', 'Grapes'] sizes = [25, 30, 15, 10] colors = ['red', 'yellow', 'orange', 'purple'] # Create the pie chart plt.pie(sizes, labels=labels, colors=colors, autopct='%1.1f%%', startangle=90) # Add a title plt.title('Fruit Distribution') # Display the chart plt.axis('equal') # Equal aspect ratio ensures that pie is drawn as a circle plt.show()In this example, we define the labels for each slice of the pie chart, the corresponding sizes for each slice, and the colors to be used for each slice. The
autopctparameter specifies the format for displaying the percentage values, and thestartangleparameter determines the angle at which the first slice starts.The
plt.axis('equal')line ensures that the pie chart is drawn as a circle instead of an ellipse. Finally, we useplt.show()to display the chart.You can modify the
labels,sizes, andcolorslists to customize the pie chart based on your data. - User
MATLAB script for line chart
- Assistant
Certainly! Here's an example of a MATLAB script that creates a line chart:
% Data for the line chart x = 1:10; % x-axis values y = [2, 4, 6, 8, 7, 5, 3, 2, 4, 6]; % y-axis values % Create the line chart plot(x, y, '-o', 'LineWidth', 2, 'MarkerSize', 8, 'MarkerFaceColor', 'red') % Add labels and title xlabel('X-axis') ylabel('Y-axis') title('Line Chart') % Adjust axis limits (optional) xlim([0, 11]) ylim([0, 10]) % Display the grid (optional) grid on % Display the chartIn this example, we define two vectors:
xfor the x-axis values andyfor the corresponding y-axis values. Theplotfunction is then used to create the line chart. The'-'specifies a solid line, and the'o'specifies circular markers at each data point. The'LineWidth','MarkerSize', and'MarkerFaceColor'parameters are used to customize the appearance of the line and markers.We then add labels to the x-axis and y-axis using the
xlabelandylabelfunctions, respectively. Thetitlefunction is used to add a title to the chart.Optionally, you can adjust the axis limits using the
xlimandylimfunctions to set specific minimum and maximum values for the x-axis and y-axis, respectively. To display a grid, you can use thegrid oncommand.Finally, you can use
endto end the script or simply run the code using the MATLAB command prompt or the "Run" button in the MATLAB editor.