有没有设置默认绘图样式的方法?

huangapple go评论68阅读模式
英文:

Is there a way to set default plot styles?

问题

默认绘图样式中的线条太细了。我可以通过以下方式更改线条宽度:

ax = plt.gca()
ax.spines['bottom'].set_linewidth(2)

但接下来我还需要更改刻度线的宽度和文本样式,以使它们看起来相似。此外,我希望刻度线在图内部。我想以相同的样式绘制许多图形。是否有办法在开始时定义绘图样式,然后每次使用plt时都使用相同的绘图样式?

# 更改样式
plt.style.use('classic')  # 但我找不到我喜欢的样式
# 更改刻度线方向和宽度
plt.tick_params(direction='in', width=2)
# 更改坐标轴线宽度
ax = plt.gca()
ax.spines['bottom'].set_linewidth(2)
# 更改x轴标签
plt.xlabel('xlabel', fontsize=16)
英文:

The lines in the default plot style is too thin. I could change the line width by

ax=plt.gca()
ax.spines['bottom'].set_linewidth(2)

But then I also need to change the tick width and text style to make them look similar. Also, I want the tick inside the graph. I want to plot a lot of plots in the same style. Is there anyway that I can define the plotting style at the beginning, then each time I use plt, they use the same plotting styles?

#To change the style
plt.style.use('classic')#But I couldn't find the styles that I like
#To change tick direction and width
plt.tick_params(direction='in',width=2)
#To change axis lines width
ax=plt.gca()
ax.spines['bottom'].set_linewidth(2)
#To change x lable
plt.xlabel('xlabel',fontsize=16)

答案1

得分: 1

你可以在Matplotlib中定义一个自定义的绘图样式,并将其一致应用于所有的绘图。这允许你在一个地方设置各种属性,包括线宽、刻度参数、文本样式等。

创建一个名为 "mystyle.mplstyle" 的文件:

axes.linewidth: 2
xtick.direction: in
ytick.direction: in
xtick.major.width: 2
ytick.major.width: 2
font.size: 16

然后,当你想在代码中使用它时,只需通过以下方式加载这些参数:

import matplotlib.pyplot as plt

plt.style.use('path/to/mystyle.mplstyle')
英文:

you can define a custom plotting style in Matplotlib and apply it consistently to all your plots. This allows you to set various properties, including line width, tick parameters, text style, and more, in one place.

make a file called "mystyle.mplstyle"

  axes.linewidth: 2
  xtick.direction: in
  ytick.direction: in
  xtick.major.width: 2
  ytick.major.width: 2
  font.size: 16

Then when you would like to use it in code you just load this parms through

import matplotlib.pyplot as plt

plt.style.use('path/to/mystyle.mplstyle')

huangapple
  • 本文由 发表于 2023年6月2日 04:02:23
  • 转载请务必保留本文链接:https://go.coder-hub.com/76385323.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定