for col in columns: ax.plot(df[col], color=random.choice(['r', 'g', 'b', 'y', 'm', 'c']))再加20个颜色
时间: 2023-08-20 19:26:22 浏览: 124
当然,您可以在 `random.choice()` 方法的参数中添加更多的颜色选项,例如:
```python
for col in columns:
ax.plot(df[col], color=random.choice(['r', 'g', 'b', 'y', 'm', 'c', 'k', 'w', 'orange', 'pink', 'brown', 'gray', 'purple', 'olive', 'navy', 'teal', 'maroon', 'gold', 'lime', 'indigo', 'turquoise', 'tan', 'salmon', 'plum', 'sienna', 'orchid', 'peru', 'lavender', 'steelblue', 'rosybrown', 'darkkhaki', 'slategray', 'crimson', 'chocolate', 'cadetblue', 'forestgreen', 'deeppink', 'darkorange', 'coral', 'cornflowerblue']))
```
在这个例子中,颜色选项列表中添加了更多的颜色,您也可以根据需要自行添加其他颜色。
相关问题
for i, col in enumerate(columns): line, = ax.plot(df[col], color=random.choice(['r', 'g', 'b', 'y', 'm', 'c'])) ax.set_xlabel('X Label') ax.set_ylabel('Y Label') offset = (i % 5) * 10 # 根据下标计算位置偏移量 ax.annotate(col, xy=(df.index[-1], df[col].iloc[-1]), xytext=(10 + offset, 10 + offset), textcoords='offset points', color=line.get_color(), fontsize=12, ha='left', va='bottom')增加小的图例
要增加小的图例,可以在循环中添加以下代码:
```
handles, labels = [], []
for col in columns:
line, = ax.plot(df[col], color=random.choice(['r', 'g', 'b', 'y', 'm', 'c']))
handles.append(line)
labels.append(col)
offset = (labels.index(col) % 5) * 10 # 根据下标计算位置偏移量
ax.annotate(col, xy=(df.index[-1], df[col].iloc[-1]), xytext=(10 + offset, 10 + offset), textcoords='offset points', color=line.get_color(), fontsize=12, ha='left', va='bottom')
ax.legend(handles, labels, loc='upper left', bbox_to_anchor=(0, 1))
```
这里使用了 `ax.legend()` 函数来添加图例。`handles` 和 `labels` 分别存储了每条线的对象和标签,然后将它们传递给 `ax.legend()` 函数即可。`loc` 参数设置图例的位置,`bbox_to_anchor` 参数设置图例的偏移量,这里设为左上角对齐。
ax.annotate(col, xy=(df.index[-1], df[col].iloc[-1]), xytext=(10, 10), textcoords='offset points', color=line.get_color(), fontsize=12, ha='left', va='bottom')标注位置错开
如果标注位置与线有重叠,可以适当调整 `xytext` 参数的值,使标注位置错开。例如,可以将 `xytext` 的参数值改为 `(15, 15)` 来向右上方偏移标注位置:
```
for col in columns:
line, = ax.plot(df[col], color=random.choice(['r', 'g', 'b', 'y', 'm', 'c']))
ax.set_xlabel('X Label')
ax.set_ylabel('Y Label')
ax.annotate(col, xy=(df.index[-1], df[col].iloc[-1]), xytext=(15, 15), textcoords='offset points', color=line.get_color(), fontsize=12, ha='left', va='bottom')
```
如果需要更大的偏移量,可以适当增大 `xytext` 参数的值。
阅读全文