python - 有什么办法可以缩短这段代码吗?茎子图
问题描述
茎图中的 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)
推荐阅读
- java - @controller 不工作,但 @restcontroller 在 Spring Boot 中工作
- php - Laravel 根据关系获取数据
- php - 为什么这段代码显示 $req 变量未定义?
- scala - 使用相同的方法生成派生特征,但在 Scala 中的每个方法中没有第一个参数
- c - c - 如何将命令行参数重定向到函数?
- php - 从一个 php 函数更新全局值并使其可用于同一类中的另一个函数
- javascript - discord.js ubuntu 未连接到网关
- python - 对 JSON 对象的 SQL 查询
- css - Angular Prime-ng p-tree,如何更改缩进?
- python - 这段代码有问题吗?还是我的电脑?