Skip to content
TheChatArchiveThe Chat Archive
gpt-3.5-turbo-0613· openaiHugging Face Suzhen/CodeChat

python script for pie chart

  1. User

    python script for pie chart

  2. 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 autopct parameter specifies the format for displaying the percentage values, and the startangle parameter 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 use plt.show() to display the chart.

    You can modify the labels, sizes, and colors lists to customize the pie chart based on your data.

  3. User

    MATLAB script for line chart

  4. 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 chart
    

    In this example, we define two vectors: x for the x-axis values and y for the corresponding y-axis values. The plot function 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 xlabel and ylabel functions, respectively. The title function is used to add a title to the chart.

    Optionally, you can adjust the axis limits using the xlim and ylim functions to set specific minimum and maximum values for the x-axis and y-axis, respectively. To display a grid, you can use the grid on command.

    Finally, you can use end to end the script or simply run the code using the MATLAB command prompt or the "Run" button in the MATLAB editor.