plt.plot(industry_df["trade_date"], industry_df["pct_change"], label=industry, linewidth=10) 这个设置只是设置了name中一个线条的粗细,如何设置全部线条的粗细
时间: 2024-10-17 08:12:38 浏览: 35
在你提供的代码片段中,`label=industry` 是设置折线图的标签,`linewidth=10` 是设置单条折线的线宽为10。如果你想要一次性设置整个图中所有线条的粗细,应该在 `plot()` 函数外部或者在创建所有线条之前,设置一个全局的线条宽度。
如果你有一个 DataFrame `industry_df` 并且想要所有折线都具有相同的宽度,可以这样做:
```python
# 先定义线条宽度
line_width = 10
# 然后遍历DataFrame的每一列,假设每一列都是一个需要绘制的折线
for col_name in industry_df.columns:
if "pct_change" in col_name: # 检查是否是需要绘制变化率的列
plt.plot(industry_df["trade_date"], industry_df[col_name], label=col_name, linewidth=line_width)
# 如果你想统一设置所有线条,不论标签如何,可以在绘图之前设置
plt.plot(..., linewidth=line_width)
# 后续添加标题、图例等其他元素
plt.title('Industry Changes with Same Line Width')
plt.legend()
# 显示图形
plt.show()
```
在这个代码中,`line_width` 变量被设置为全局的线条宽度值,然后在每次 `plot()` 调用时都会应用这个宽度。
相关问题
# 读取数据 df = pd.read_excel('E:/应统案例大赛/附件1-股票交易数据/yuceclose5.xlsx', usecols=[ 1, 2, 3, 4, 5]) # 计算涨跌幅 change = df.pct_change() # 绘制图表 change.plot() plt.xlabel('date') plt.ylabel('Change') plt.title('Stock Prices Change') plt.legend(loc='upper left') plt.autoscale(enable=True, axis='x', tight=True) plt.show()修改代码,使横坐标为对应的表格中的日期
可以通过以下方式修改代码来实现:
```python
import pandas as pd
import matplotlib.pyplot as plt
# 读取数据
df = pd.read_excel('E:/应统案例大赛/附件1-股票交易数据/yuceclose5.xlsx', usecols=[1, 2, 3, 4, 5], index_col=0, parse_dates=True)
# 计算涨跌幅
change = df.pct_change()
# 绘制图表
change.plot()
plt.xlabel('Date')
plt.ylabel('Change')
plt.title('Stock Prices Change')
plt.legend(loc='upper left')
plt.autoscale(enable=True, axis='x', tight=True)
plt.show()
```
这里添加了 `index_col=0` 和 `parse_dates=True` 参数来将第一列日期作为索引,并将其解析为日期格式。然后将横坐标标签改为 `Date`。这样就可以使横坐标为对应的表格中的日期了。
帮我改进这个代码,让生成的饼图尺寸更大:# 定义一个函数,用于提取行业代码的第一个字母 def get_industry(x): return x[0] # 提取行业代码的第一个字母并添加到新的一列 df['industry'] = df['行业代码'].apply(get_industry) # 计算各行业的占比 industry_counts = df['industry'].value_counts() industry_pcts = industry_counts / industry_counts.sum() # 绘制饼图 plt.pie(industry_pcts, labels=industry_pcts.index, autopct='%1.1f%%') plt.axis('equal') plt.show()
你可以在绘制饼图前设置 figure 的大小,如下所示:
```
import matplotlib.pyplot as plt
# 定义一个函数,用于提取行业代码的第一个字母
def get_industry(x):
return x[0]
# 提取行业代码的第一个字母并添加到新的一列
df['industry'] = df['行业代码'].apply(get_industry)
# 计算各行业的占比
industry_counts = df['industry'].value_counts()
industry_pcts = industry_counts / industry_counts.sum()
# 设置 figure 的大小
fig, ax = plt.subplots(figsize=(8, 8))
# 绘制饼图
ax.pie(industry_pcts, labels=industry_pcts.index, autopct='%1.1f%%')
ax.axis('equal')
plt.show()
```
这样可以将饼图的尺寸调大,可以根据需要调整 figsize 的大小。
阅读全文
相关推荐














