<p>有时,最好升级当前的开发包。因为您的<code>virtual-env</code>安装了本地<code>matplotlib</code>。寻源激活后,升级<code>matplotlib</code></p>
<hr/>
<p>为此,请使用<em>管理</em>权限打开<code>terminal</code>或<code>command prompt</code>,并尝试使用以下命令升级<em>版本<code>pip</code>和<code>matplotlib</code>:</p>
<ul>
<li><code>python -m pip install upgrade pip</code></li>
<li><code>python -m pip install upgrade matplotlib</code></li>
</ul>
<p>另一方面,使用<code>matplotlib</code>,您可以<em>获取</em>或<em>设置</em>任一轴的当前标记位置和标签(即<code>x-axis</code>或<code>y-axis</code>)</p>
<hr/>
<p>我给你一个非常简单的例子,你的给定数据沿着两个轴以<em>顺序</em>绘制。要保留沿<code>axes</code>的顺序,只需使用:</p>
<ul>
<li><a href="https://matplotlib.org/3.1.1/api/_as_gen/matplotlib.pyplot.xticks.html" rel="noreferrer">matplotlib.pyplot.xticks</a></li>
<li><a href="https://matplotlib.org/3.2.1/api/_as_gen/matplotlib.pyplot.yticks.html" rel="noreferrer">matplotlib.pyplot.yticks</a></li>
</ul>
<p>您可以使用此技术解决升级<code>matplotlib</code>和不升级<code>matplotlib</code>时的问题。特别是对于指定的<code>matplotlib==2.1.1</code>版本</p>
<hr/>
<pre><code>import matplotlib.pyplot as plt
x_axis_values = ['(-68.18100000000001, 89.754]', '(89.754, 130.42]', '(130.42, 165.601]', '(165.601, 205.456]',
'(205.456, 371.968]']
y_axis_values = ['(-0.123, 0.749]', '(0.749, 0.922]', '(0.922, 1.068]', '(1.068, 1.253]', '(1.253, 2.14]']
# Try to sort the values, before passing to [xticks, yticks]
# or in which order, you want them along axes
plt.xticks(ticks=range(len(x_axis_values)), labels=x_axis_values)
plt.yticks(ticks=range(len(y_axis_values)), labels=y_axis_values)
# plt.scatter(x_axis_values, y_axis_values)
plt.xlabel('Values')
plt.ylabel('Indices')
plt.show()
</code></pre>
<p>下面是这个简单示例的输出。您可以看到沿着<code>x-axis</code>和<code>y-axis</code>的值。给定图的目的只是指定<code>values</code>以及<code>axes</code>:</p>
<p><a href="https://i.stack.imgur.com/jMI3v.png" rel="noreferrer"><img src="https://i.stack.imgur.com/jMI3v.png" alt="enter image description here"/></a></p>
<hr/>
<p>对于您给定的代码,我已将您的一些代码更新如下:</p>
<pre><code>import pandas as pd
import matplotlib.pyplot as plt
import matplotlib.colors
import numpy as np
n_tile = 5
np.random.seed(0)
x = np.random.normal(150, 70, 3000, )
y = np.random.normal(1, 0.3, 3000)
r = np.random.normal(0.4, 0.1, 3000)
plot_data = pd.DataFrame({
'x': x,
'y': y,
'r': r
})
plot_data['x_group'] = pd.qcut(plot_data['x'], n_tile, duplicates='drop')
plot_data['y_group'] = pd.qcut(plot_data['y'], n_tile, duplicates='drop')
plot_data_grouped = plot_data.groupby(['x_group', 'y_group'], as_index=False).agg({'r': ['mean', 'count']})
plot_data_grouped.columns = ['x', 'y', 'mean', 'count']
cmap = plt.cm.rainbow
norm = matplotlib.colors.Normalize(vmin=0, vmax=1)
########################################################
########## Updated Portion of the Code ################
x_axis_values = [str(x) for x in plot_data_grouped['x']]
y_axis_values = [str(x) for x in plot_data_grouped['y']]
plt.figure(figsize=(10, 10))
# Unique Values have only length == 5
plt.xticks(ticks=range(5), labels=sorted(np.unique(x_axis_values)))
plt.yticks(ticks=range(5), labels=sorted(np.unique(y_axis_values)))
plt.scatter(x=x_axis_values,
y=y_axis_values,
s=plot_data_grouped["count"],
c=plot_data_grouped['mean'], cmap="RdYlGn", edgecolors="black")
plt.show()
########################################################
</code></pre>
<p>现在您可以看到输出符合要求:</p>
<p><a href="https://i.stack.imgur.com/G3eEW.png" rel="noreferrer"><img src="https://i.stack.imgur.com/G3eEW.png" alt="enter image description here"/></a></p>