Skip to content
TheChatArchiveThe Chat Archive
Qwenqwen-max-2025-01-25· Alibaba (Qwen)Hugging Face LMArena Human Preference 140K

write a publication ready matplotlib parameter template with some random examples

From a Chatbot Arena battleThe same person sent this prompt to two anonymous models at once. Compare it with Kimikimi-k2-0711-preview’s answer.
  1. User

    write a publication ready matplotlib parameter template with some random examples

  2. Qwen Assistant

    Certainly! Below is a publication-ready matplotlib parameter template that you can use to create high-quality plots suitable for academic papers or professional presentations. The template includes some random examples to demonstrate how to apply the settings.


    Publication-Ready Matplotlib Template

    import matplotlib.pyplot as plt
    import numpy as np
    from matplotlib.ticker import MaxNLocator
    
    # Set global parameters for publication-quality plots
    plt.rcParams.update({
        'font.size': 12,                # Default font size
        'axes.labelsize': 14,           # Font size for axis labels
        'axes.titlesize': 16,           # Font size for plot titles
        'xtick.labelsize': 12,          # Font size for x-axis tick labels
        'ytick.labelsize': 12,          # Font size for y-axis tick labels
        'legend.fontsize': 12,          # Font size for legend
        'figure.figsize': (8, 6),       # Figure size in inches (width, height)
        'figure.dpi': 300,              # Resolution of the figure (DPI)
        'lines.linewidth': 2,           # Line width for plots
        'lines.markersize': 6,          # Marker size for scatter plots
        'axes.grid': True,              # Enable grid by default
        'grid.alpha': 0.5,              # Transparency of the grid
        'grid.linestyle': '--',         # Style of the grid lines
        'savefig.bbox': 'tight',        # Save figures with tight bounding box
        'savefig.format': 'pdf',        # Default format for saving figures
        'savefig.transparent': True,    # Save figures with transparent background
    })
    
    # Example 1: Simple Line Plot
    x = np.linspace(0, 10, 100)
    y1 = np.sin(x)
    y2 = np.cos(x)
    
    plt.figure()
    plt.plot(x, y1, label='sin(x)', color='blue')
    plt.plot(x, y2, label='cos(x)', color='red', linestyle='--')
    plt.xlabel('x')
    plt.ylabel('y')
    plt.title('Sine and Cosine Functions')
    plt.legend()
    plt.savefig('line_plot.pdf')  # Save the figure
    plt.show()
    
    # Example 2: Scatter Plot with Error Bars
    np.random.seed(42)
    x_scatter = np.arange(10)
    y_scatter = np.random.normal(loc=5, scale=1, size=10)
    errors = np.random.uniform(0.1, 0.5, size=10)
    
    plt.figure()
    plt.errorbar(x_scatter, y_scatter, yerr=errors, fmt='o', capsize=5, label='Data Points', color='green')
    plt.xlabel('X-axis')
    plt.ylabel('Y-axis')
    plt.title('Scatter Plot with Error Bars')
    plt.legend()
    plt.savefig('scatter_plot.pdf')
    plt.show()
    
    # Example 3: Bar Chart
    categories = ['A', 'B', 'C', 'D']
    values = [3, 7, 2, 5]
    
    plt.figure()
    bars = plt.bar(categories, values, color='orange', edgecolor='black')
    plt.xlabel('Categories')
    plt.ylabel('Values')
    plt.title('Bar Chart Example')
    plt.gca().yaxis.set_major_locator(MaxNLocator(integer=True))  # Ensure integer ticks on y-axis
    plt.savefig('bar_chart.pdf')
    plt.show()
    
    # Example 4: Histogram
    data = np.random.randn(1000)
    
    plt.figure()
    plt.hist(data, bins=30, color='purple', alpha=0.7, edgecolor='black')
    plt.xlabel('Value')
    plt.ylabel('Frequency')
    plt.title('Histogram of Random Data')
    plt.savefig('histogram.pdf')
    plt.show()
    
    # Example 5: Contour Plot
    x_contour = np.linspace(-3, 3, 100)
    y_contour = np.linspace(-3, 3, 100)
    X, Y = np.meshgrid(x_contour, y_contour)
    Z = np.sin(np.sqrt(X**2 + Y**2))
    
    plt.figure()
    contour = plt.contourf(X, Y, Z, levels=20, cmap='viridis')
    plt.colorbar(contour, label='Z Value')
    plt.xlabel('X-axis')
    plt.ylabel('Y-axis')
    plt.title('Contour Plot')
    plt.savefig('contour_plot.pdf')
    plt.show()
    

    Explanation of Key Parameters

    1. Font Sizes: Adjusted for readability in publications (font.size, axes.labelsize, etc.).
    2. Figure Size and Resolution: figure.figsize ensures proper dimensions, and figure.dpi sets high resolution.
    3. Line and Marker Styles: lines.linewidth and lines.markersize enhance visual clarity.
    4. Grid Settings: axes.grid, grid.alpha, and grid.linestyle make plots easier to interpret.
    5. Saving Figures: savefig.bbox='tight' avoids cropping issues, and savefig.transparent=True allows seamless integration into documents.
    6. Color Maps: Used viridis for perceptually uniform colors in contour plots.

    How to Use

    1. Copy the template into your Python script or Jupyter Notebook.
    2. Replace the example data with your own dataset.
    3. Customize titles, labels, and other details as needed.
    4. Save the plots using plt.savefig() in your preferred format (e.g., PDF, PNG).

    This template ensures consistency and professionalism across all your plots, making them ready for publication or presentation.

    Let me know if you need further assistance!

Responses were produced in different, uncontrolled contexts (system prompts, settings, prior turns). This is not a benchmark.