im trying to save the matplotlib to a vector image like .svg. it has hatches for bargraphs and fillbetween
when i insert to word, the svg image looks fine, but when i save or print to pdf in ms word, the hatch fills get pixelated. is there a fix to this so the hatch is still a vector?
sample code
import numpy as np
import matplotlib.pyplot as plt
# Sample data
x = np.linspace(0, 10, 100)
y1 = np.sin(x) + 1
y2 = np.cos(x) + 1
# Bar chart data
categories = ['A', 'B', 'C', 'D']
values = [5, 7, 3, 8]
# Create figure and subplots
fig, axs = plt.subplots(1, 2, figsize=(10, 4)) # Two subplots side by side
# ---- Line Graph with fill_between hatch ----
axs[0].plot(x, y1, color='blue', label="Line 1")
axs[0].plot(x, y2, color='red', label="Line 2")
# Transparent fill for the area between lines
axs[0].fill_between(x, y1, y2, color='blue', alpha=0.3)
# Hatch overlay
axs[0].fill_between(x, y1, y2, facecolor="none", hatch="///", edgecolor="blue", linewidth=0.5)
axs[0].set_title("Line Graph with Hatch")
axs[0].set_xlabel("X-axis")
axs[0].set_ylabel("Y-axis")
axs[0].legend()
axs[0].grid(True)
# ---- Bar Chart with Diagonal Hatches ----
bars = axs[1].bar(categories, values, color='none', edgecolor='black', hatch='///')
axs[1].set_title("Bar Chart with Hatch")
axs[1].set_xlabel("Categories")
axs[1].set_ylabel("Values")
axs[1].grid(axis='y', linestyle="--", alpha=0.6)
fig.savefig("test.svg", format="svg", bbox_inches="tight")
# Show the figure
plt.tight_layout()
plt.show()
after inserting to word and save as pdf, this is the output
anyway to fix this?
