While working on a Python data visualization project, I had to customize time-series plots using Matplotlib’s plot_date() function. The default line style and color looked too generic, and I wanted a more professional, visually appealing plot that matched the client’s brand theme.
If you’ve ever tried to adjust the appearance of date-based plots in Matplotlib, you’ve probably noticed that plot_date() behaves a bit differently from the regular plot() function.
In this tutorial, I’ll walk you through two simple methods to change linestyle and color in Matplotlib plot_date() plots. I’ll also share a complete working Python example so you can try it out right away.
What is the plot_date() Function in Python Matplotlib?
Before we dive into customization, let’s quickly understand what the plot_date() function does.
The plot_date() function in Matplotlib is used to plot data points against dates on the x-axis. It’s particularly useful when you’re dealing with time-series data such as stock prices, daily sales, or temperature readings.
Here’s the basic syntax:
matplotlib.pyplot.plot_date(x, y, fmt='-', tz=None, xdate=True, ydate=False, **kwargs)- x: The sequence of dates or datetime objects.
- y: The data values corresponding to those dates.
- fmt: A string that defines both line style and color (for example, ‘r–‘ means a red dashed line).
- tz: Time zone information, if applicable.
- xdate/ydate: Flags to interpret x or y values as dates.
Change Linestyle and Color in Python Matplotlib plot_date()
When you’re presenting data to clients or stakeholders, the visual clarity of your charts matters as much as the data itself.
Changing the linestyle can help you differentiate between multiple datasets on the same plot (for example, actual vs. forecasted sales). Adjusting the color improves readability and aligns your charts with your organization’s branding.
Method 1 – Change Linestyle in Matplotlib plot_date() in Python
When you create a date-based plot using plot_date(), the default linestyle is usually a solid line (‘-‘). However, you can easily change it to dashed (‘–‘), dotted (‘:’), or dash-dot (‘-.’) to make your plot more visually distinct.
Below is a complete Python example that demonstrates how to change the linestyle in a Matplotlib plot_date() plot.
Example: Change Linestyle in plot_date() Plot
Before running the code, make sure you have Matplotlib and NumPy installed. You can install them using:
pip install matplotlib numpyNow, here’s the full Python code:
import matplotlib.pyplot as plt
import matplotlib.dates as mdates
import numpy as np
from datetime import datetime, timedelta
# Generate sample date range (for the last 10 days)
dates = [datetime.now() - timedelta(days=i) for i in range(10)]
dates = dates[::-1] # Reverse to get chronological order
# Generate some random data (for example, daily sales in USD)
sales = np.random.randint(200, 500, size=10)
# Create the plot with dashed linestyle
plt.figure(figsize=(10, 5))
plt.plot_date(dates, sales, linestyle='--', marker='o', label='Daily Sales (Dashed Line)')
# Format the x-axis to show dates clearly
plt.gcf().autofmt_xdate()
plt.gca().xaxis.set_major_formatter(mdates.DateFormatter('%b %d'))
# Add labels, title, and legend
plt.title('Daily Sales Trend (Dashed Linestyle Example)', fontsize=14)
plt.xlabel('Date')
plt.ylabel('Sales (USD)')
plt.legend()
# Display the plot
plt.show()You can refer to the screenshot below to see the output.

In this example, I used linestyle=’–‘ to create a dashed line connecting the data points. The marker=’o’ argument adds circular markers at each data point, which makes the plot easier to read.
You can experiment with different line styles, such as:
'-'for solid line'--'for dashed line':'for dotted line'-.'for dash-dot line
Each style can be useful depending on the type of data you’re visualizing. For instance, I often use dashed lines to represent forecasted or estimated values in Python charts.
Pro Tip for Python Developer
If you want to apply multiple line styles in the same chart (for example, actual vs. forecasted data), simply call plot_date() multiple times with different linestyle values.
plt.plot_date(dates, sales, linestyle='-', label='Actual Sales')
plt.plot_date(dates, sales + 50, linestyle='--', label='Forecasted Sales')
plt.legend()This approach helps you visually differentiate between datasets while keeping the chart clean and professional.
Method 2 – Change Color in Matplotlib plot_date() in Python
Changing the color of your line in a Matplotlib plot_date() plot is just as easy as changing the linestyle. You can specify the color using either a color name (like ‘red’, ‘blue’, ‘green’) or a hexadecimal code (like ‘#FF5733’).
This is especially useful when you want to align your Python visualization with your company’s style guide or make multiple plots more distinguishable.
Example: Change Line Color in plot_date() Plot
Here’s a complete Python example showing how to change the line color in a plot_date() plot:
import matplotlib.pyplot as plt
import matplotlib.dates as mdates
import numpy as np
from datetime import datetime, timedelta
# Generate sample date range (for the last 7 days)
dates = [datetime.now() - timedelta(days=i) for i in range(7)]
dates = dates[::-1]
# Generate random temperature data (in Fahrenheit)
temperature = np.random.uniform(60, 85, size=7)
# Create the plot with a custom color
plt.figure(figsize=(10, 5))
plt.plot_date(dates, temperature, color='#1f77b4', linestyle='-', marker='s', label='Temperature (Blue Line)')
# Format x-axis dates
plt.gcf().autofmt_xdate()
plt.gca().xaxis.set_major_formatter(mdates.DateFormatter('%b %d'))
# Add labels, title, and legend
plt.title('Weekly Temperature Trend in New York', fontsize=14)
plt.xlabel('Date')
plt.ylabel('Temperature (°F)')
plt.legend()
# Display the plot
plt.show()You can refer to the screenshot below to see the output.

In this example, I used color=’#1f77b4′, which is a shade of blue commonly used in Matplotlib’s default color palette. You can replace this with any color name or hex code that suits your design.
Here are a few examples of color options you can use in Python Matplotlib:
- ‘red’
- ‘green’
- ‘blue’
- ‘orange’
- ‘#FF6347’ (Tomato Red)
- ‘#2E8B57’ (Sea Green)
Combine Linestyle and Color in One plot_date() Call
You can also combine both linestyle and color in a single call to plot_date() for a more customized look.
Here’s an example:
plt.plot_date(dates, temperature, color='darkred', linestyle='-.', marker='^', label='Custom Style')This line will create a dark red dash-dot line with triangle markers.
As a Python developer, I find this flexibility extremely useful when building dashboards or reports where each dataset needs a unique visual identity.
Pro Tip for Better Visualization
If your plot contains multiple datasets, try to use contrasting colors and linestyles so that each line is easily distinguishable. For example, use a solid blue line for one dataset and a dashed orange line for another.
Also, always include a legend and axis labels to make your Python visualization more user-friendly and professional.
Additional Tips for Working with plot_date() in Python
Here are a few additional tips I’ve learned from experience:
- Use autofmt_xdate() to automatically format date labels for readability.
- Set major date formatters using mdates.DateFormatter() to control how dates appear (e.g.,
%b %dfor “Oct 08”). - Adjust figure size with plt.figure(figsize=(width, height)) to make the chart presentation-ready.
- Add gridlines using plt.grid(True) for easier data interpretation.
- Save your plot as an image using plt.savefig(‘plot.png’, dpi=300) for reports or presentations.
These small enhancements can significantly improve the quality and clarity of your Python charts.
When to Use plot_date() Instead of plot()
While you can use the regular plot() function with datetime objects, plot_date() is specifically optimized for handling date-based data.
It automatically formats the x-axis as dates and provides better control over date formatting and time zones. If your dataset involves timestamps or daily/weekly metrics, plot_date() is the perfect choice in Python.
Conclusion
In this tutorial, I showed you how to change the linestyle and change the color in Matplotlib plot_date() plots using Python. Both methods are simple yet powerful ways to enhance your data visualizations.
You learned how to:
- Use the linestyle parameter to modify the line’s appearance.
- Use the color parameter to customize the line’s color.
- Combine both for more professional-looking charts.
As someone who’s been building Python data visualizations for over a decade, I can confidently say that mastering these small customization techniques can make your plots more effective and visually appealing.
You may also read:
- Work with Loglog Log Scale and Adjusting Ticks in Matplotlib
- Set Loglog Log Scale for X and Y Axes in Matplotlib
- Log‑Log Scale in Matplotlib with Minor Ticks and Colorbar
- Matplotlib plot_date for Scatter and Multiple Line Charts

I am Bijay Kumar, a Microsoft MVP in SharePoint. Apart from SharePoint, I started working on Python, Machine learning, and artificial intelligence for the last 5 years. During this time I got expertise in various Python libraries also like Tkinter, Pandas, NumPy, Turtle, Django, Matplotlib, Tensorflow, Scipy, Scikit-Learn, etc… for various clients in the United States, Canada, the United Kingdom, Australia, New Zealand, etc. Check out my profile.