[英]Setting Colors For Positive and Negative Values in Scatter Plot with Matplotlib with 2 Custom Colors
[英]Matplotlib and Pandas change colors of negative values
我想将条形图的负值颜色设置为“深蓝色”并保持正值“深红色”。 实现这一目标的最佳方法是什么? 我的 csv 文件的 X 值是纬度点,我的 Y 值是温度异常。 如我的链接图中所示,我的 x 轴似乎也有问题。 它为从 -90 到 90 的每个纬度点绘制刻度,这会导致很多重叠。 我希望以 15 为增量,但我不知道该怎么做。
编辑:解决了。
import pandas as pd
import matplotlib.pyplot as plt
from matplotlib.pyplot import figure
import numpy as np
filename = "2015_tAnoms.csv"
df = pd.read_csv(filename, delimiter=',',header='infer')
figure(num=None, figsize=(8, 6), dpi=300, facecolor='w', edgecolor='k')
ax = plt.gca()
ax.set_facecolor((0,0,0))
ax.set_ylim([-1,3])
ax.grid(color='lightgray', alpha=0.6, which='major', axis='y', linestyle='-')
plt.bar(df['Latitude'],df['TempAnom'],color=np.where(df['TempAnom'] < 0, 'deepskyblue', 'crimson'))
plt.margins(x=0)
ax.xaxis.set_ticks(np.arange(-90,91,15))
plt.xlabel("Latitude")
plt.ylabel("Temperature Anomaly (°C)")
plt.title("2015 Temperature Anomalies by Latitude Band")
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.