gpt4 book ai didi

python - 有没有办法让这段代码更短?茎子图

转载 作者:行者123 更新时间:2023-12-04 11:02:34 26 4
gpt4 key购买 nike

茎图中的 x 和 y 轴变量是来自 DataFrames 的列


import pandas as pd
import matplotlib.pyplot as plt

fig, ax = plt.subplots(3, 1,figsize=(8,20))
fig.suptitle('Magnitude-Time Plot : R1')
fig.subplots_adjust(hspace=0.5)

# Defining custom 'xlim' and 'ylim' values.
xlim = (1990, 2005)
ylim = (0, 9)

# Setting the values for all axes.
plt.setp(ax, xlim=xlim, ylim=ylim)

# plot with no marker

#subplot(311) : Catalog8 in R1
markerline, stemlines, baseline = ax[0].stem(cata8q1.YearDeci,cata8q1.Magnitude, markerfmt=' ',use_line_collection=True,linefmt='b')
plt.setp(stemlines,linewidth=0.5)
ax[0].set_title('Catalog OLD')

#subplot(312) : catalog 9 _Uniq in R1
markerline, stemlines, baseline = ax[1].stem(cata9uniq1.YearDeci,cata9uniq1.Magnitude, markerfmt=' ',use_line_collection=True,linefmt='r')
plt.setp(stemlines,linewidth=0.5)
ax[1].set_title('Unique events in NEW CATALOG')

#subplot(312) : Catalog NEW in R1
markerline, stemlines, baseline = ax[2].stem(catanewq1.YearDeci,catanewq1.Magnitude, markerfmt=' ',use_line_collection=True)
plt.setp(stemlines,linewidth=0.5)
ax[2].set_title('OLD + unique events in NEW Catalog')

plt.show()

我正在尝试绘制 [3,1] 词干子图,我想用更少的代码行更方便地控制轴属性和图形属性

最佳答案

你可以做这样的事情。

catalogs = [(cata8q1,"Catalog Old", "b"), ...] 

for i, (catalog, title, linefmt) in enumerate (catalogs):
markerline, stemlines, baseline = ax[0].stem(catalog.YearDeci,catalog.Magnitude, markerfmt=' ',use_line_collection=True,linefmt=linefmt)
plt.setp(stemlines,linewidth=0.5)
ax[i].set_title(title)

关于python - 有没有办法让这段代码更短?茎子图,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58709102/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com