Python中散点图根据特定条件着色
我现在的目标是为欧洲地区创建子散点图。
fig, axes = plt.subplots(2,2,figsize=(10,8))
fig.tight_layout(h_pad=5.0,w_pad=3.0)
color = ['red', 'blue', 'orange']
# red = if temperature above 10
# blue = if temperature below 6
# orange = if temperature between 6 and 10 (inclusive)
# first figure for 'No EU and No Coastline'
lat1 = visualize1['latitude']
axes[0][0].scatter(city_count1List,lat1.values)
axes[0][0].set_title('No EU and No Coastline')
axes[0][0].set_xlabel('City')
axes[0][0].set_ylabel('Latitude')
# second figure for 'No EU and Yes Coastline'
lat2 = visualize2['latitude']
axes[0][1].scatter(city_count2List,lat2.values)
axes[0][1].set_title('No EU and Yes Coastline')
axes[0][1].set_xlabel('City')
axes[0][1].set_ylabel('Latitude')
# third figure for 'Yes EU and No Coastline'
lat3 = visualize3['latitude']
axes[1][0].scatter(city_count3List,lat3.values)
axes[1][0].set_title('Yes EU and No Coastline')
axes[1][0].set_xlabel('City')
axes[1][0].set_ylabel('Latitude')
# fourth figure for 'Yes EU and Yes Coastline'
lat4 = visualize4['latitude']
axes[1][1].scatter(city_count4List,lat4.values)
axes[1][1].set_title('Yes EU and Yes Coastline')
axes[1][1].set_xlabel('City')
axes[1][1].set_ylabel('Latitude')
plt.show()
但我想做的是根据该地区的温度使图具有不同的颜色。这是正在绘制的图表之一的示例。
如果温度高于 10,则绘图将为红色。
如果温度在 6 到 10(含)之间,绘图将为橙色。
如果温度低于 6,则绘图将为蓝色。
有什么方法可以用上面的代码来做到这一点吗?
My goal right now is to create sub scatter plots for regions in Europe.
fig, axes = plt.subplots(2,2,figsize=(10,8))
fig.tight_layout(h_pad=5.0,w_pad=3.0)
color = ['red', 'blue', 'orange']
# red = if temperature above 10
# blue = if temperature below 6
# orange = if temperature between 6 and 10 (inclusive)
# first figure for 'No EU and No Coastline'
lat1 = visualize1['latitude']
axes[0][0].scatter(city_count1List,lat1.values)
axes[0][0].set_title('No EU and No Coastline')
axes[0][0].set_xlabel('City')
axes[0][0].set_ylabel('Latitude')
# second figure for 'No EU and Yes Coastline'
lat2 = visualize2['latitude']
axes[0][1].scatter(city_count2List,lat2.values)
axes[0][1].set_title('No EU and Yes Coastline')
axes[0][1].set_xlabel('City')
axes[0][1].set_ylabel('Latitude')
# third figure for 'Yes EU and No Coastline'
lat3 = visualize3['latitude']
axes[1][0].scatter(city_count3List,lat3.values)
axes[1][0].set_title('Yes EU and No Coastline')
axes[1][0].set_xlabel('City')
axes[1][0].set_ylabel('Latitude')
# fourth figure for 'Yes EU and Yes Coastline'
lat4 = visualize4['latitude']
axes[1][1].scatter(city_count4List,lat4.values)
axes[1][1].set_title('Yes EU and Yes Coastline')
axes[1][1].set_xlabel('City')
axes[1][1].set_ylabel('Latitude')
plt.show()
The result I get is what I want in terms of formatting.
But what I want to do is make the plots different colors depending on the temperature of the region. Here's an example of one of the charts that are being graphed.
If the temperature is above 10, then the plot will be red.
If the temperature is between 6 and 10 (inclusive), the plot will be orange.
If the temperature is below 6, then the plot will be blue.
Is there some way I can do this with the code above?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
根据条件创建了一个分类颜色列应用颜色
Created a categorical color column based on the condition & applied color
我没有时间手动复制您在图片中发布的数据,因此我将生成随机数据。
有许多应用条件颜色的方法。这是我的方法:
I don't have time to manually copy the data you posted in the pictures, so I'm going to generate random data.
There are many ways to apply conditional colors. This is my approach: