[英]Why are different colors appearing for circles plotted with the same function and input values?
[英]Multiple csv files plotted on same axis with different colors
请考虑以下csv文件
import pandas as pd
from io import StringIO
from matplotlib import pylab as plt
csv1 = """x,y
0,1
1,0"""
csv2 = """x,y
0,0
1,1"""
csv3 = """x,y
.5,1
.5,0"""
csv4 = """x,y
0,.5
1,.5"""
我可以在不同的轴上绘制它们
fig, axes = plt.subplots(2, 2, sharex=True, sharey=True)
for i, csv in enumerate([csv1, csv2, csv3, csv4]):
r, c = i // 2, i % 2
pd.read_csv(StringIO(csv)).plot.scatter('x', 'y', ax=axes[r, c])
fig.tight_layout()
但是如何在不同颜色的同一轴上绘图?
matplotlib自动使用不同的颜色
fig, axes = plt.subplots()
for i, csv in enumerate([csv1, csv2, csv3, csv4]):
df = pd.read_csv(StringIO(csv))
axes.scatter(df.x, df.y)
fig.tight_layout()
plt.show()
人们需要定义要使用的颜色列表。
colors = ["blue", "orange", "green", "red"]
fig, axes = plt.subplots()
for i, csv in enumerate([csv1, csv2, csv3, csv4]):
df = pd.read_csv(StringIO(csv)).plot.scatter('x', 'y', ax=axes, color=colors[i])
fig.tight_layout()
plt.show()
如果你将fig, axes = plt.subplots(2, 2, sharex=True, sharey=True)
更改为fig, ax = plt.subplots()
。 然后制作一个您想要使用的颜色列表,并使用color=colors[i]
将其传递给您的绘图:
import pandas as pd
from io import StringIO
from matplotlib import pylab as plt
csv1 = """x,y
0,1
1,0"""
csv2 = """x,y
0,0
1,1"""
csv3 = """x,y
.5,1
.5,0"""
csv4 = """x,y
0,.5
1,.5"""
colors = ['red','green','blue','black']
fig, ax = plt.subplots()
for i, csv in enumerate([csv1, csv2, csv3, csv4]):
r, c = i // 2, i % 2
pd.read_csv(StringIO(csv)).plot.scatter('x', 'y', ax=ax, color=colors[i])
这导致以下图表:
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.